package com.gh.gamecenter.history

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.ethanhua.skeleton.Skeleton
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.utils.toDrawable
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.view.CustomDividerItemDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.databinding.FragmentListBaseSkeletonBinding
import com.gh.gamecenter.feature.entity.GameEntity

open class HistoryGameListFragment : ListFragment<GameEntity, HistoryGameListViewModel>(), IBatchDelete {

    private var mAdapter: HistoryGameListAdapter? = null
    private val mBinding by lazy { FragmentListBaseSkeletonBinding.inflate(layoutInflater) }

    override fun getLayoutId() = R.layout.fragment_list_base_skeleton
    override fun getInflatedLayout() = mBinding.root

    override fun provideListAdapter(): HistoryGameListAdapter {
        if (mAdapter == null) {
            mAdapter = HistoryGameListAdapter(requireContext(), mListViewModel)
        }
        return mAdapter as HistoryGameListAdapter
    }

    override fun provideListViewModel(): HistoryGameListViewModel {
        return viewModelProvider()
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mCachedView.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        mSkeletonScreen =
            Skeleton.bind(mBinding.listSkeleton).shimmer(false).load(R.layout.fragment_subject_skeleton).show()
    }

    override fun changeOption(option: ManageOption) {
        mAdapter?.changeOption(option)
    }

    override fun getItemDecoration(): RecyclerView.ItemDecoration {
        mItemDecoration = CustomDividerItemDecoration(
            requireContext(),
            onlyDecorateTheFirstItem = false,
            notDecorateTheFirstItem = false,
            notDecorateTheLastItem = true,
            notDecorateTheFirstTwoItems = false
        ).apply {
            setDrawable(R.drawable.divider_item_line_space_16.toDrawable(requireContext())!!)
        }
        return mItemDecoration
    }
}