package com.gh.gamecenter.mygame

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.base.activity.BaseActivity_TabLayout
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.view.BugFixedPopupWindow
import com.gh.gamecenter.R
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.common.utils.showRegulationTestDialogIfNeeded
import com.gh.gamecenter.common.utils.tryCatchInRelease
import com.gh.gamecenter.common.utils.updateStatusBarColor
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.databinding.PopupMyGameGuideBinding
import com.gh.gamecenter.gamecollection.publish.GameCollectionEditActivity

class MyGameActivity : BaseActivity_TabLayout() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setNavigationTitle("我的游戏")
        setToolbarMenu(R.menu.menu_my_game)
        mDividerLineView?.visibility = View.VISIBLE
        showGuide()
        updateStatusBarColor(R.color.background_white, R.color.background_white)
    }

    override fun initFragmentList(fragments: MutableList<Fragment>?) {
        fragments?.add(MyPlayedGameFragment())
        fragments?.add(MyFollowedGameFragment())
        fragments?.add(MyReservationFragment())
    }

    override fun initTabTitleList(tabTitleList: MutableList<String>?) {
        tabTitleList?.add("玩过")
        tabTitleList?.add("关注")
        tabTitleList?.add("预约")
    }

    override fun onPageSelected(position: Int) {
        super.onPageSelected(position)
        MtaHelper.onEvent("我的光环", "我的游戏", "${mTabTitleList[position]}Tab")
        MtaHelper.onEvent("我的光环_新", "我的游戏", "${mTabTitleList[position]}Tab")
        NewFlatLogUtils.logMyGameTabClick(mTabTitleList[position])
    }

    override fun onMenuItemClick(item: MenuItem?): Boolean {
        if (item?.itemId == R.id.menu_create_game_collection) {
            showRegulationTestDialogIfNeeded {
                startActivity(GameCollectionEditActivity.getIntent(this, mEntrance, "我的游戏"))
            }
        }
        return super.onMenuItemClick(item)
    }

    override fun isAutoResetViewBackgroundEnabled() = true

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        updateStatusBarColor(R.color.background_white, R.color.background_white)
        getMenuItem(R.id.menu_create_game_collection).setIcon(R.drawable.ic_create_game_collection)
    }

    private fun showGuide() {
        AppExecutor.uiExecutor.executeWithDelay({
            tryCatchInRelease {
                if (!SPUtils.getBoolean(Constants.SP_MY_GAME_GUIDE)) {
                    val binding = PopupMyGameGuideBinding.inflate(layoutInflater, null, false)
                    val popupWindow = BugFixedPopupWindow(
                        binding.root,
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT
                    )
                    binding.root.setOnClickListener {
                        SPUtils.setBoolean(Constants.SP_MY_GAME_GUIDE, true)
                        popupWindow.dismiss()
                    }
                    popupWindow.run {
                        isTouchable = true
                        isFocusable = true
                        if (!isFinishing) {
                            showAtLocation(window.decorView, Gravity.TOP, 0, 0)
                        }
                    }
                }
            }
        }, 500)
    }

    companion object {
        @JvmStatic
        fun getIntentWithConfig(context: Context, defaultFragmentPosition: Int): Intent {
            val intent = Intent(context, MyGameActivity::class.java)
            intent.putExtra(PAGE_INDEX, defaultFragmentPosition)
            return intent
        }
    }

}