package com.gh.gamecenter.gamedetail.libao

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.GlobalActivityManager
import com.gh.gamecenter.common.baselist.LazyListFragment
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.eventbus.EBReuse
import com.gh.gamecenter.common.json.json
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.entity.LibaoEntity
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class LibaoListFragment : LazyListFragment<LibaoEntity, LibaoListViewModel>() {
    private var toolbarTitleTv: TextView? = null
    private var toolbarContainer: RelativeLayout? = null
    private var game: GameEntity? = null
    private var hideToolbar = false

    private var adapter: LibaoListAdapter? = null

    override fun getRealLayoutId() = R.layout.fragment_libao_list

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        game = arguments?.getParcelable(EntranceConsts.KEY_GAME)
        hideToolbar = arguments?.getBoolean(EntranceConsts.KEY_HIDE_TOOLBAR, false) ?: false
    }

    override fun initRealView() {
        super.initRealView()
        toolbarContainer = mCachedView.findViewById(R.id.reuseToolbar)
        toolbarTitleTv = toolbarContainer?.findViewById(R.id.normal_title)
        val toolbarBackBtn = toolbarContainer?.findViewById<View>(com.gh.gamecenter.common.R.id.backBtn)
        toolbarBackBtn?.setOnClickListener {
            requireActivity().onBackPressed()
        }

        toolbarContainer?.goneIf(hideToolbar) {
            toolbarTitleTv?.text = "${game?.name}-礼包"
            toolbarTitleTv?.marqueeOnce()
        }
    }

    override fun onFragmentFirstVisible() {
        super.onFragmentFirstVisible()
        if (!hideToolbar) {
            requireActivity().updateStatusBarColor(com.gh.gamecenter.common.R.color.ui_surface, com.gh.gamecenter.common.R.color.ui_surface)
        }
        SensorsBridge.trackEvent("GameGiftPageView", json {
            "game_id" to game?.id
            "game_name" to game?.name
            "last_page_name" to GlobalActivityManager.getLastPageEntity().pageName
            "last_page_id" to GlobalActivityManager.getLastPageEntity().pageId
        })
    }

    override fun provideListAdapter(): ListAdapter<*> =
        adapter ?: LibaoListAdapter(
            requireContext(),
            mListViewModel ?: provideListViewModel(),
            game
        ).apply { adapter = this }

    override fun provideListViewModel(): LibaoListViewModel =
        viewModelProvider<LibaoListViewModel>(LibaoListViewModel.Factory(game))

    override fun getItemDecoration(): RecyclerView.ItemDecoration? = null

    // 登录事件/礼包状态变更事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(reuse: EBReuse) {
        if (reuse.type == Constants.LOGIN_TAG || (reuse.type == Constants.LIBAO_CHANGED_TAG && !isSupportVisible)) {
            onLoadRefresh()
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        if (!hideToolbar) {
            requireActivity().updateStatusBarColor(com.gh.gamecenter.common.R.color.ui_surface, com.gh.gamecenter.common.R.color.ui_surface)
            toolbarContainer?.findViewById<androidx.appcompat.widget.Toolbar>(R.id.normal_toolbar)?.setBackgroundColor(
                com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
            toolbarContainer?.findViewById<ImageView>(R.id.backBtn)?.setImageResource(com.gh.gamecenter.common.R.drawable.ic_bar_back)
            toolbarTitleTv?.setTextColor(com.gh.gamecenter.common.R.color.text_primary.toColor(requireContext()))
        }
    }

    companion object {
        fun getBundle(gameEntity: GameEntity?, hideToolbar: Boolean) = Bundle().apply {
            putParcelable(EntranceConsts.KEY_GAME, gameEntity)
            putBoolean(EntranceConsts.KEY_HIDE_TOOLBAR, hideToolbar)
        }

        fun newInstance(bundle: Bundle?) = LibaoListFragment().apply {
            if (bundle != null) {
                arguments = bundle
            }
        }
    }

}