package com.gh.gamecenter.catalog

import android.os.Bundle
import android.view.View
import com.ethanhua.skeleton.Skeleton
import com.gh.gamecenter.common.constant.Constants
import com.gh.common.exposure.ExposureListener
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.databinding.FragmentListBaseSkeletonBinding

class SpecialCatalogFragment : ListFragment<SpecialCatalogItemData, SpecialCatalogViewModel>() {

    private var mCatalogId = ""
    private var mCatalogTitle = ""
    private var mIsCategoryV2 = false // 是否为新分类2.0
    private val mBinding by lazy { FragmentListBaseSkeletonBinding.inflate(layoutInflater) }
    private var mAdapter: SpecialCatalogAdapter? = null
    private var mLastPageDataMap: HashMap<String, String>? = null

    private lateinit var mExposureListener: ExposureListener

    override fun getLayoutId() = 0

    override fun getInflatedLayout() = mBinding.root

    override fun provideListViewModel() =
        viewModelProvider<SpecialCatalogViewModel>(
            SpecialCatalogViewModel.Factory(
                mCatalogId,
                mCatalogTitle,
                mIsCategoryV2,
                arguments?.getParcelableArrayList(EntranceConsts.KEY_EXPOSURE_SOURCE_LIST)
            )
        )

    override fun provideListAdapter() = mAdapter ?: SpecialCatalogAdapter(
        requireContext(),
        mListViewModel,
        mLastPageDataMap
    ).apply {
        mAdapter = this
        mExposureListener = ExposureListener(this@SpecialCatalogFragment, this)
    }

    override fun getItemDecoration() = null

    override fun isAutomaticLoad(): Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {

        mIsCategoryV2 = arguments?.getBoolean(EntranceConsts.KEY_IS_CATEGORY_V2) ?: false
        mCatalogId = arguments?.getString(EntranceConsts.KEY_CATALOG_ID) ?: ""
        mCatalogTitle = arguments?.getString(EntranceConsts.KEY_CATALOG_TITLE) ?: ""
        mLastPageDataMap = arguments?.getSerializable(EntranceConsts.KEY_LAST_PAGE_DATA) as? HashMap<String, String>

        super.onCreate(savedInstanceState)

        mListViewModel?.catalogEntrance = if (mEntrance.contains("首页")) "首页" else "板块"
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val skeletonLayoutId =
            if (mIsCategoryV2) R.layout.fragment_special_catalog_second_skeleton else R.layout.fragment_special_catalog_first_skeleton
        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(skeletonLayoutId)
            .show()
        onLoadRefresh()

        mListRv.addOnScrollListener(mExposureListener)
    }

    override fun onResume() {
        super.onResume()
        if (mAdapter?.isBannerSizeMoreThanOne == true) {
            mAdapter?.isAutoScroll = true
            mAdapter?.notifyItemChanged(0)
        }
    }

    override fun onPause() {
        super.onPause()
        if (mAdapter?.isBannerSizeMoreThanOne == true) {
            mAdapter?.isAutoScroll = false
            mAdapter?.notifyItemChanged(0)
        }
    }

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