package com.gh.gamecenter.history

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.gh.gamecenter.common.base.fragment.BaseFragment_TabLayout
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.core.utils.MtaHelper
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.R
import com.gh.gamecenter.collection.*
import com.gh.gamecenter.login.user.UserManager

class HistoryWrapperFragment : BaseFragment_TabLayout() {

    private var mManageMenu: MenuItem? = null

    private lateinit var mViewModel: HistoryWrapperViewModel

    private var mLastPosition = 0
    override fun getLayoutId() = R.layout.fragment_no_padding_tablayout_viewpager

    override fun initTabTitleList(tabTitleList: MutableList<String>) {
        tabTitleList.add(getString(R.string.main_game))
        tabTitleList.add(getString(R.string.game_collection))
        tabTitleList.add(getString(R.string.video))
        tabTitleList.add(getString(R.string.answer))
        tabTitleList.add(getString(R.string.collection_article))
        tabTitleList.add(getString(R.string.collection_info))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDividerLineView?.visibility = View.VISIBLE
        mViewModel = viewModelProvider()

        initMenu(R.menu.menu_manage)
        mManageMenu = getItemMenu(R.id.layout_menu_manage)
        changeMenuTextByOption()

        mViewModel.currentOptionLiveData.observe(this) {
            changeMenuTextByOption()
        }
    }

    override fun onResume() {
        super.onResume()
        setNavigationTitle("浏览记录")
    }

    override fun onMenuItemClick(menuItem: MenuItem?) {
        super.onMenuItemClick(menuItem)
        menuItem?.run {
            if (itemId == R.id.layout_menu_manage) {
                when (mViewModel.currentOptionLiveData.value) {
                    ManageOption.OPTION_MANAGER -> {
                        mViewModel.currentOptionLiveData.value = ManageOption.OPTION_CANCEL_SELECT
                    }
                    ManageOption.OPTION_CANCEL_SELECT -> {
                        mViewModel.currentOptionLiveData.value = ManageOption.OPTION_MANAGER
                    }
                    else -> {
                        //do nothing
                    }
                }
                changeOption()
            }
        }
    }

    override fun initFragmentList(fragments: MutableList<Fragment>) {
        fragments.add(HistoryGameListFragment().with(arguments))
        fragments.add(GamesCollectionFragment().with((arguments?.clone() as? Bundle)?.apply {
            putString(EntranceConsts.KEY_USER_ID, UserManager.getInstance().userId)
            putString(EntranceConsts.KEY_TYPE, GamesCollectionFragment.TYPE_HISTORY)
        }))
        fragments.add(VideoFragment().with((arguments?.clone() as? Bundle)?.apply {
            putString("videoStyle", VideoFragment.VideoStyle.BROWSING_HISTORY.value)
        }))
        fragments.add(AnswerFragment().with((arguments?.clone() as? Bundle)?.apply {
            putString(
                EntranceConsts.KEY_TYPE,
                AnswerFragment.HISTORY
            )
        }))
        fragments.add(CommunityArticleFragment().with((arguments?.clone() as? Bundle)?.apply {
            putString(
                EntranceConsts.KEY_TYPE,
                CommunityArticleFragment.Type.HISTORY.value
            )
        }))
        fragments.add(ArticleFragment().with((arguments?.clone() as? Bundle)?.apply {
            putString(
                EntranceConsts.KEY_TYPE,
                ArticleFragment.HISTORY
            )
        }))
    }

    override fun onPageSelected(position: Int) {
        super.onPageSelected(position)
        MtaHelper.onEvent("我的光环_新", "浏览记录", "${mTabTitleList[position]}Tab")
        mViewModel.currentOptionLiveData.value = ManageOption.OPTION_MANAGER
        changeOption()
        mLastPosition = position
    }

    fun changeOption() {
        val fragments = childFragmentManager.fragments
        mViewModel.currentOptionLiveData.value?.let {
            fragments.forEachIndexed { index, fragment ->
                if (fragment is IBatchDelete) {
                    if (index == mViewPager.currentItem) {
                        fragment.changeOption(it)
                    } else if (index == mLastPosition) {
                        fragment.changeOption(ManageOption.OPTION_MANAGER)
                    }
                }
            }
        }
        changeMenuTextByOption()
    }

    fun changeMenuTextByOption() {
        (mManageMenu?.actionView as? TextView)?.apply {
            text = when (mViewModel.currentOptionLiveData.value) {
                ManageOption.OPTION_MANAGER -> "编辑"
                ManageOption.OPTION_CANCEL_SELECT -> "取消"
                else -> ""
            }
        }
    }

}