package com.gh.gamecenter.category

import android.graphics.Color
import android.view.View
import androidx.lifecycle.ViewModelProviders
import com.ethanhua.skeleton.Skeleton
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.view.VerticalItemDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.LazyListFragment
import com.gh.gamecenter.databinding.FragmentListBaseSkeletonBinding
import com.gh.gamecenter.entity.CategoryEntity

class CategoryDirectoryFragment : LazyListFragment<CategoryEntity, CategoryDirectoryListViewModel>() {

    private lateinit var mViewModel: CategoryDirectoryListViewModel
    private lateinit var mAdapter: CategoryDirectoryAdapter
    private var mBinding: FragmentListBaseSkeletonBinding? = null

    override fun getRealLayoutId() = R.layout.fragment_list_base_skeleton

    override fun onRealLayoutInflated(inflatedView: View) {
        super.onRealLayoutInflated(inflatedView)
        mBinding = FragmentListBaseSkeletonBinding.bind(inflatedView)
    }

    override fun onFragmentFirstVisible() {
        mViewModel = ViewModelProviders.of(this).get(CategoryDirectoryListViewModel::class.java)
        mViewModel.categoryId = arguments?.getString(EntranceConsts.KEY_CATEGORY_ID) ?: ""
        mAdapter =
            CategoryDirectoryAdapter(requireContext(), arguments?.getString(EntranceConsts.KEY_CATEGORY_TITLE) ?: "")
        super.onFragmentFirstVisible()
    }

    override fun initRealView() {
        super.initRealView()

        setNavigationTitle(arguments?.getString(EntranceConsts.KEY_CATEGORY_TITLE))

        mSkeletonScreen = Skeleton.bind(mBinding?.listSkeleton)
            .shimmer(true)
            .angle(Constants.SHIMMER_ANGLE)
            .color(R.color.skeleton_shimmer_color)
            .duration(Constants.SHIMMER_DURATION)
            .maskWidth(Constants.MASK_WIDTH)
            .gradientCenterColorWidth(Constants.GRADIENT_CENTER_COLOR_WIDTH)
            .load(R.layout.fragment_category_skeleton)
            .show()
        onLoadRefresh()

        if (arguments?.getBoolean(EntranceConsts.KEY_IS_HOME) == true) {
            mListRv.setPadding(
                0,
                context?.resources?.getDimension(R.dimen.home_recyclerview_padding_top)?.toInt() ?: 0,
                0,
                0
            )
            mListRv.clipToPadding = false
        }

        mListRv.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        mAdapter.recyclerView = mListRv
        mListRefresh?.isEnabled = false
    }

    override fun isAutomaticLoad() = false

    override fun provideListAdapter() = mAdapter

    override fun provideListViewModel() = mViewModel

    override fun getItemDecoration() = VerticalItemDecoration(context, 0F, false)

    override fun onLoadRefresh() {
        mCachedView?.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        super.onLoadRefresh()
    }

    override fun onLoadDone() {
        mCachedView?.setBackgroundColor(Color.TRANSPARENT)
        super.onLoadDone()
    }

    override fun onLoadEmpty() {
        mCachedView?.setBackgroundColor(Color.TRANSPARENT)
        super.onLoadEmpty()
    }

    override fun onLoadError() {
        mCachedView?.setBackgroundColor(Color.TRANSPARENT)
        super.onLoadError()
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mListRv.setBackgroundColor(R.color.background_white.toColor(requireContext()))
        mListRv.adapter?.run {
            mListRv.recycledViewPool.clear()
            notifyItemRangeChanged(0, itemCount)
        }
    }
}