package com.gh.gamecenter.gamedetail.dialog


import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.gh.gamecenter.BuildConfig
import com.gh.gamecenter.common.base.fragment.BaseBottomDialogFragment
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.ExtraTagHandler
import com.gh.gamecenter.databinding.DialogGameDetailScrollableTextBinding
import com.lightgame.utils.AppManager

class GameDetailScrollableTextDialogFragment : BaseBottomDialogFragment<DialogGameDetailScrollableTextBinding>() {
    private var title: String = ""
    private var content: String = ""
    private var topLeftContent: String = ""
    private var topRightContent: String = ""
    private var isHtmlContent: Boolean = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        title = arguments?.getString(KEY_TITLE) ?: ""
        content = arguments?.getString(EntranceConsts.KEY_CONTENT) ?: ""
        topLeftContent = arguments?.getString(KEY_TOP_LEFT_CONTENT) ?: ""
        topRightContent = arguments?.getString(KEY_TOP_RIGHT_CONTENT) ?: ""
        isHtmlContent = arguments?.getBoolean(KEY_IS_HTML_CONTENT) ?: false
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.closeIv.setOnClickListener {
            dismissAllowingStateLoss()
        }
        mBinding.titleTv.text = title
        updateContent()
        mBinding.topLeftTv.goneIf(topLeftContent.isEmpty()) {
            mBinding.topLeftTv.text = topLeftContent
        }
        mBinding.topRightTv.goneIf(topRightContent.isEmpty()) {
            mBinding.topRightTv.text = topRightContent
        }
    }

    private fun updateContent() {
        if (isHtmlContent) {
            mBinding.contentTv.setTextWithInterceptingInternalUrl(
                content.dropFontColorInDarkMode(mBinding.contentTv.context)
                    .fromHtmlCompat(PicassoImageGetter(mBinding.contentTv), ExtraTagHandler())
            )
        } else {
            mBinding.contentTv.text = TextHelper.getHighlightedSpannableStringThatIsWrappedInsideWrapper(
                mBinding.contentTv.context,
                content,
                Constants.DEFAULT_TEXT_WRAPPER_2,
                highlightedTextClickListener = TextHelper.CopyToClipboardHighlightedTextClick()
            )
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        updateContent()
    }

    companion object {
        const val KEY_TITLE = "title"
        const val KEY_TOP_LEFT_CONTENT = "top_left_content"
        const val KEY_TOP_RIGHT_CONTENT = "top_right_content"
        const val KEY_IS_HTML_CONTENT = "is_html_content"

        @JvmStatic
        fun show(
            context: Context?,
            title: String,
            content: String,
            topLeftContent: String = "",
            topRightContent: String = "",
            isHtmlContent: Boolean = false
        ) {
            val fragmentActivity: FragmentActivity = if (context is FragmentActivity) {
                context
            } else if (BuildConfig.DEBUG) {
                throw IllegalStateException("GameDetailScrollableTextDialogFragment context must be FragmentActivity")
            } else {
                val currentActivity = AppManager.getInstance().currentActivity()
                if (currentActivity is FragmentActivity) {
                    currentActivity
                } else {
                    throw IllegalStateException("current activity context must be FragmentActivity")
                }
            }

            // 防止重复弹出
            if (hasDialogInCurrentActivity(fragmentActivity)) return

            val dialogFragment = GameDetailScrollableTextDialogFragment()
            dialogFragment.arguments = bundleOf(
                KEY_TITLE to title,
                EntranceConsts.KEY_CONTENT to content,
                KEY_TOP_LEFT_CONTENT to topLeftContent,
                KEY_TOP_RIGHT_CONTENT to topRightContent,
                KEY_IS_HTML_CONTENT to isHtmlContent
            )
            dialogFragment.show(
                fragmentActivity.supportFragmentManager,
                GameDetailScrollableTextDialogFragment::class.java.name
            )
        }

        private fun hasDialogInCurrentActivity(fragmentActivity: FragmentActivity): Boolean {
            val fragments: List<Fragment> = fragmentActivity.supportFragmentManager.fragments
            fragments.forEach { fragment ->
                if (fragment is GameDetailScrollableTextDialogFragment) return true
            }
            return false
        }
    }
}