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 androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.BuildConfig
import com.gh.gamecenter.common.base.fragment.BaseBottomDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.goneIf
import com.gh.gamecenter.databinding.DialogGameDetailRecyclerViewBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.gamedetail.entity.GameDetailInfoTag
import com.lightgame.utils.AppManager

class GameFunctionDialogFragment: BaseBottomDialogFragment<DialogGameDetailRecyclerViewBinding>() {
    private var infoTags: List<GameDetailInfoTag.InfoTag> = arrayListOf()
    private var linkEntity: LinkEntity? = null
    private var gameEntity: GameEntity? = null
    private val adapter by lazy { GameFunctionAdapter(requireContext(), infoTags) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        gameEntity = arguments?.getParcelable(EntranceConsts.KEY_GAME_ENTITY)
        infoTags = arguments?.getParcelableArrayList(KEY_INFO_TAG) ?: arrayListOf()
        linkEntity = arguments?.getParcelable(EntranceConsts.KEY_LINK)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.titleTv.text = "功能说明"
        mBinding.closeIv.setOnClickListener {
            dismissAllowingStateLoss()
        }
        mBinding.subtitleTv.goneIf(linkEntity == null) {
            mBinding.subtitleTv.text = linkEntity?.text
            mBinding.subtitleTv.setOnClickListener { _ ->
                linkEntity?.let {
                    DirectUtils.directToLinkPage(requireContext(), it, "游戏详情-功能说明", "")
                    SensorsBridge.trackGameDetailModuleClick(
                        gameEntity?.id,
                        gameEntity?.name,
                        gameEntity?.categoryChinese,
                        "组件内容",
                        "功能标签",
                        "功能标签",
                        null,
                        subModuleName = "跳转入口",
                        linkType = it.type,
                        linkId = it.link,
                        linkText = it.text,
                        gameStatus = null
                    )
                }
            }
        }
        mBinding.recyclerView.run {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = this@GameFunctionDialogFragment.adapter
            (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        adapter.notifyItemRangeChanged(0, adapter.itemCount)
    }

    companion object {
        const val KEY_INFO_TAG = "info_tag"

        @JvmStatic
        fun show(context: Context?, gameEntity: GameEntity?, infoTags: List<GameDetailInfoTag.InfoTag>, linkEntity: LinkEntity?) {
            val fragmentActivity: FragmentActivity = if (context is FragmentActivity) {
                context
            } else if (BuildConfig.DEBUG) {
                throw IllegalStateException("GameFunctionDialogFragment 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 = GameFunctionDialogFragment()
            dialogFragment.arguments = bundleOf(
                EntranceConsts.KEY_GAME_ENTITY to gameEntity,
                KEY_INFO_TAG to infoTags,
                EntranceConsts.KEY_LINK to linkEntity
            )
            dialogFragment.show(fragmentActivity.supportFragmentManager, GameFunctionDialogFragment::class.java.name)
        }

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