package com.gh.gamecenter.gamecollection.choose

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.setRootBackgroundColor
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.entity.SubjectData
import com.gh.gamecenter.feature.entity.GameEntity

class HotGameListFragment: ListFragment<GameEntity, HotGameListViewModel>() {
    private var subjectData: SubjectData? = null
    private val viewModel: HotGameListViewModel by lazy { viewModelProvider(HotGameListViewModel.Factory(subjectData!!)) }
    private val chooseGamesViewModel: ChooseGamesViewModel by lazy { viewModelProvider(ChooseGamesViewModel.Factory()) }
    private val adapter by lazy { AddSearchAndPlayedGameAdapter(requireContext(), chooseGamesViewModel) }

    override fun getLayoutId(): Int = R.layout.fragment_add_user_played_game
    override fun provideListAdapter(): ListAdapter<GameEntity> = adapter

    override fun getItemDecoration(): RecyclerView.ItemDecoration? {
        return null
    }

    override fun provideListViewModel(): HotGameListViewModel {
        subjectData = arguments?.getParcelable(EntranceConsts.KEY_SUBJECT_DATA)
        return viewModel
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mCachedView.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface.toColor(requireContext()))
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mCachedView?.setRootBackgroundColor(com.gh.gamecenter.common.R.color.ui_surface)
        mCachedView?.findViewById<View>(R.id.divider)?.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(requireContext()))
        adapter.let { it.notifyItemRangeChanged(0, it.itemCount) }
    }
}