package com.gh.gamecenter.gamecollection.hotlist

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.json.json
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.VerticalItemDecoration
import com.gh.gamecenter.core.utils.TimeElapsedHelper
import com.gh.gamecenter.entity.GamesCollectionEntity

class GameCollectionPlayerCreationFragment :
    ListFragment<GamesCollectionEntity, GameCollectionPlayerCreationViewModel>() {
    private val mElapsedHelper by lazy { TimeElapsedHelper() }
    private lateinit var mViewModel: GameCollectionPlayerCreationViewModel
    private var mAdapter: GameCollectionPlayerCreationAdapter? = null

    override fun provideListAdapter(): GameCollectionPlayerCreationAdapter {
        if (mAdapter == null) {
            mAdapter =
                GameCollectionPlayerCreationAdapter(
                    requireContext(),
                    provideListViewModel(),
                    mEntrance,
                    arguments?.getString(EntranceConsts.KEY_PATH) ?: "",
                    arguments?.getParcelableArrayList(EntranceConsts.KEY_EXPOSURE_SOURCE_LIST) ?: arrayListOf()
                ) {
                    val isWeek = it == 0
                    SensorsBridge.trackEvent("ViewGameCollectHotRankTabClick", json {
                        "position" to 0
                        "tab_content" to if (mViewModel.isWeek) "周榜" else "月榜"
                        "text" to if (isWeek) "周榜" else "月榜"
                    })
                    mAdapter?.setListData(ArrayList())
                    mListLoading?.visibility = View.VISIBLE
                    mViewModel.switchType(isWeek) {
                        mListLoading?.visibility = View.GONE
                    }
                }
        }
        return mAdapter as GameCollectionPlayerCreationAdapter
    }

    override fun provideListViewModel(): GameCollectionPlayerCreationViewModel {
        mViewModel = viewModelProvider()
        return mViewModel
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewModel.topLinkLiveData.observeNonNull(this) {
            mAdapter?.setTipData(it)
        }
    }

    override fun onPause() {
        super.onPause()
        mElapsedHelper.pauseCounting()
        NewFlatLogUtils.logGameCollectionHotListTabView(
            mElapsedHelper.elapsedTime,
            GameCollectionHotListWrapperFragment.PLAYER_CREATION_TAB_NAME,
            "",
            ""
        )
        SensorsBridge.trackEvent("ViewGameCollectHotRankTab", json {
            "stay_length" to mElapsedHelper.elapsedTime
            "position" to 0
            "tab_content" to GameCollectionHotListWrapperFragment.PLAYER_CREATION_TAB_NAME
        })
    }

    override fun onResume() {
        super.onResume()
        mElapsedHelper.resetCounting()
        mElapsedHelper.resumeCounting()
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration {
        mItemDecoration = VerticalItemDecoration(context, 8F, false, false, 0)
        return mItemDecoration
    }

    fun setListRefreshEnable(enable: Boolean) {
        mListRefresh?.isEnabled = enable
    }
}