package com.gh.gamecenter.savegame

import android.os.Bundle
import android.view.View
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.common.HaloApp
import com.gh.gamecenter.common.baselist.LazyListFragment
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.databinding.FragmentListBaseBinding
import com.gh.gamecenter.common.utils.SensorsBridge
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.feature.entity.GameEntity

class GameArchiveListInstalledFragment : LazyListFragment<GameEntity, GameArchiveListViewModel>() {

    private var mAdapter: GameArchiveListInstalledAdapter? = null
    private lateinit var mViewModel: GameArchiveListViewModel

    private val mBinding by lazy { FragmentListBaseBinding.inflate(layoutInflater) }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.reuseNoneData.reuseNoneDataDescTv.visibility = View.VISIBLE
        mBinding.reuseNoneData.reuseNoneDataDescTv.text = "快去看看好玩的存档游戏吧！"
        mBinding.reuseNoneData.reuseNoneDataTv.text = "这儿还没有内容噢~"
        NewFlatLogUtils.logGameArchivePageShow("已安装游戏")
        SensorsBridge.trackEvent("CloudSaveGameTopicTabSelected", "tab_name", "已安装游戏")
    }

    override fun initRealView() {
        super.initRealView()
        mCachedView.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
    }


    override fun provideListAdapter(): ListAdapter<GameEntity> {
        return mAdapter ?: GameArchiveListInstalledAdapter(requireContext(), mViewModel).apply {
            mAdapter = this
        }
    }

    override fun provideListViewModel(): GameArchiveListViewModel {
        val factory = GameArchiveListViewModel.Factory(HaloApp.getInstance(), "installed")
        mViewModel = viewModelProvider(factory)
        return mViewModel
    }

    override fun getLayoutId() = 0

    override fun getInflatedLayout() = mBinding.root

    override fun getItemDecoration() = null

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mListRv?.recycledViewPool?.clear()
        mAdapter?.notifyItemRangeChanged(0, mAdapter?.itemCount ?: 0)
    }
}