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.gamecenter.BuildConfig
import com.gh.gamecenter.common.base.fragment.BaseBottomDialogFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.databinding.DialogGameDetailRecyclerViewBinding
import com.gh.gamecenter.feature.entity.BigEvent
import com.gh.gamecenter.feature.entity.GameEntity
import com.lightgame.utils.AppManager

class GameBigEventDialogFragment : BaseBottomDialogFragment<DialogGameDetailRecyclerViewBinding>() {
    private var gameEntity: GameEntity? = null
    private var bigEvents: List<BigEvent> = arrayListOf()
    private val adapter by lazy { GameBigEventAdapter(requireContext(), gameEntity, bigEvents) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        gameEntity = arguments?.getParcelable(EntranceConsts.KEY_GAME)
        bigEvents = arguments?.getParcelableArrayList(KEY_BIG_EVENT) ?: arrayListOf()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.titleTv.text = "游戏大事件"
        mBinding.closeIv.setOnClickListener {
            dismissAllowingStateLoss()
        }
        mBinding.recyclerView.run {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = this@GameBigEventDialogFragment.adapter
            (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
        }
    }

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

    companion object {
        const val KEY_BIG_EVENT = "big_event"

        @JvmStatic
        fun show(context: Context?, gameEntity: GameEntity?, bigEvents: List<BigEvent>) {
            val fragmentActivity: FragmentActivity = if (context is FragmentActivity) {
                context
            } else if (BuildConfig.DEBUG) {
                throw IllegalStateException("GameBigEventDialogFragment 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 = GameBigEventDialogFragment()
            dialogFragment.arguments = bundleOf(
                EntranceConsts.KEY_GAME to gameEntity,
                KEY_BIG_EVENT to bigEvents
            )
            dialogFragment.show(fragmentActivity.supportFragmentManager, GameBigEventDialogFragment::class.java.name)
        }

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