package com.gh.gamecenter.gamecollection.mine

import android.os.Bundle
import android.view.MenuItem
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.DirectUtils
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.common.base.activity.ToolBarActivity
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.eventbus.EBReuse
import com.gh.gamecenter.common.json.json
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.common.view.VerticalItemDecoration
import com.gh.gamecenter.databinding.FragmentMyGameCollectionListBinding
import com.gh.gamecenter.entity.GamesCollectionEntity
import com.gh.gamecenter.gamecollection.publish.GameCollectionEditActivity
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MyGameCollectionFragment : ListFragment<GamesCollectionEntity, MyGameCollectionViewModel>() {

    private var mAdapter: MyGameCollectionAdapter? = null
    private lateinit var mBinding: FragmentMyGameCollectionListBinding

    override fun getLayoutId(): Int = 0

    override fun getInflatedLayout(): View {
        return FragmentMyGameCollectionListBinding.inflate(layoutInflater, null, false).apply {
            mBinding = this
        }.root
    }

    override fun onMenuItemClick(menuItem: MenuItem?) {
        super.onMenuItemClick(menuItem)
        menuItem?.run {
            if (itemId == R.id.menu_game_collection_square) {
                DirectUtils.directToGameCollectionSquare(requireContext(), "我的游戏单")
            }
        }
    }

    override fun provideListAdapter(): ListAdapter<*> {
        return mAdapter ?: MyGameCollectionAdapter(requireContext(), mListViewModel, mEntrance, "我的游戏单").apply {
            mAdapter = this
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        NewLogUtils.logEnterMyGameCollection()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setNavigationTitle("我的游戏单")
        initMenu(R.menu.menu_my_game_collection)

        mListViewModel.deleteLiveData.observe(viewLifecycleOwner) {
            val index = mAdapter?.entityList?.indexOf(it) ?: -1
            mAdapter?.entityList?.removeAt(index)
            if (mAdapter?.entityList?.isNullOrEmpty() == true) {
                onLoadEmpty()
            } else {
                mAdapter?.notifyItemRemoved(index)
            }

            toast("删除成功")
        }

        mListViewModel.publishLiveData.observe(viewLifecycleOwner) {
            SensorsBridge.trackEvent("GameCollectContributeSuccess", json {
                "source_entrance" to "我的游戏单"
                "game_num" to it.count?.game
                "game_collect_title" to it.title
                "game_collect_id" to it.id
            })
            mListViewModel.load(LoadType.REFRESH)
        }

        mBinding.createBtn.setOnClickListener {
            showRegulationTestDialogIfNeeded {
                startActivity(GameCollectionEditActivity.getCreateIntent(requireContext(), mEntrance, "我的游戏单"))
            }
        }

        mBinding.reuseNoneDataIv.setOnClickListener {
            mBinding.createBtn.performClick()
        }

        mBinding.specificationLinkTv.setOnClickListener {
            startActivity(WebActivity.getWebIntent(requireContext(), "游戏单管理规范", Constants.GAME_COLLECTION_RULE))
        }
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration {
        return VerticalItemDecoration(requireContext(), 16F, true, com.gh.gamecenter.common.R.color.transparent)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(changed: EBReuse) {
        if (changed.type == "createGameCollectionSuccess") {
            mListViewModel.load(LoadType.REFRESH)
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        (requireActivity() as ToolBarActivity).getMenuItem(R.id.menu_game_collection_square)
            .setIcon(R.drawable.ic_menu_game_collection_square)
        mAdapter?.let { it.notifyItemRangeChanged(0, it.itemCount) }
        if (::mBinding.isInitialized) {
            mBinding.run {
                root.setRootBackgroundColor(com.gh.gamecenter.common.R.color.ui_background)
                listContainer.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
                createBtn.background = R.drawable.textview_concern_red_up_round.toDrawable(requireContext())
                specificationLinkTv.setTextColor(com.gh.gamecenter.common.R.color.text_theme.toColor(requireContext()))
                reuseTvNoneData.setTextColor(com.gh.gamecenter.common.R.color.text_instance.toColor(requireContext()))
            }
        }
    }
}