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.baselist.LoadStatus
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.common.utils.viewModelProviderFromParent
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.mygame.PlayedGameViewModel
import com.gh.gamecenter.qa.editor.GameAdapter

class AddUserPlayedGameFragment : ListFragment<GameEntity, PlayedGameViewModel>() {

    private var mAdapter: GameAdapter? = null
    private lateinit var mViewModel: PlayedGameViewModel
    private lateinit var mChooseGamesViewModel: ChooseGamesViewModel
    private var mIsFirstLoad = true

    override fun getLayoutId(): Int = R.layout.fragment_add_user_played_game

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mChooseGamesViewModel = viewModelProvider(ChooseGamesViewModel.Factory())
        mChooseGamesViewModel.deleteOrAddGameLiveData.observe(this) {
            mAdapter?.entityList?.forEachIndexed { index, gameEntity ->
                if (gameEntity.id == it.id) {
                    mAdapter?.notifyItemChanged(index)
                }
            }
        }
        mListViewModel.loadStatusLiveData.observe(this) {
            if (it == LoadStatus.LIST_OVER || it == LoadStatus.LIST_LOADED) {
                mListLoading?.visibility = View.GONE
            }
        }
    }

    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 provideListAdapter(): ListAdapter<GameEntity> {
        return mAdapter ?: AddSearchAndPlayedGameAdapter(requireContext(), mChooseGamesViewModel).apply {
            mAdapter = this
        }
    }

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

    override fun provideListViewModel(): PlayedGameViewModel {
        val userId = arguments?.getString(EntranceConsts.KEY_USER_ID)
            ?: UserManager.getInstance().userId
        mViewModel = viewModelProviderFromParent(AddUserPlayedGameViewModel.Factory(userId, true), userId)
        return mViewModel
    }

    override fun onLoadEmpty() {
        super.onLoadEmpty()
        if (mIsFirstLoad && parentFragment is NewAddGamesFragment) {
            (parentFragment as NewAddGamesFragment).setCurrentItem(NewAddGamesFragment.HOT_GAMES_TAB_INDEX)
            mIsFirstLoad = false
        }
    }

    fun notifyItemRangeChanged() {
        mAdapter?.let {
            it.notifyItemRangeChanged(0, it.itemCount)
        }
    }

    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()))
        notifyItemRangeChanged()
    }
}