package com.gh.gamecenter.game.columncollection.detail

import android.view.View
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.StaggeredGridLayoutManager
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.dip2px
import com.gh.gamecenter.common.utils.observeNonNull
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.common.view.GridSpacingItemDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.LazyListFragment
import com.gh.gamecenter.databinding.FragmentColumnCollectionDetailBinding
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.entity.SubjectData
import com.gh.gamecenter.subject.tab.SubjectTabFragment

class ColumnCollectionDetailFragment : LazyListFragment<LinkEntity, ColumnCollectionDetailViewModel>() {

    private var mAdapter: ColumnCollectionDetailAdapter? = null
    private var mBinding: FragmentColumnCollectionDetailBinding? = null

    override fun getItemDecoration() = null

    override fun getRealLayoutId() = R.layout.fragment_column_collection_detail

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

    override fun onFragmentFirstVisible() {
        super.onFragmentFirstVisible()

        mListViewModel.getGameColumnCollection()
        mListViewModel.columnCollection.observeNonNull(this) {
            setNavigationTitle(it.name)

            mCachedView?.setBackgroundColor(R.color.background_white.toColor(requireContext()))
            // 非排行榜显示为 gridLayout，排行榜(多专题)直接覆盖此 recyclerView
            if (it.style != "top") {
                val skeletonLayoutId =
                    if (mListViewModel.rowCount() == 1) R.layout.fragment_subject_collection_single_row_skeleton
                    else R.layout.fragment_subject_collection_double_row_skeleton
                mSkeletonScreen = Skeleton.bind(mBinding?.contentContainer?.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()

                val gridLayoutManager =
                    StaggeredGridLayoutManager(mListViewModel.rowCount(), StaggeredGridLayoutManager.VERTICAL)
                gridLayoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE
                mListRv.clipToPadding = false
                mListRv.layoutManager = gridLayoutManager
                if (mListViewModel.rowCount() == 1) {
                    mListRv.setPadding(16F.dip2px(), 0, 16F.dip2px(), 0)
                    mListRv.addItemDecoration(GridSpacingItemDecoration(mListViewModel.rowCount(), 16F.dip2px(), false))
                } else {
                    mListRv.setPadding(16F.dip2px(), 8F.dip2px(), 16F.dip2px(), 0)
                    mListRv.addItemDecoration(GridSpacingItemDecoration(mListViewModel.rowCount(), 12F.dip2px(), false))
                }
            }
        }
    }

    override fun onChanged(list: MutableList<LinkEntity>?) {
        if (mListViewModel.columnCollection.value?.style != "top") {
            super.onChanged(list)
        } else {
            if (list != null && list.isNotEmpty()) {
                showSubjectTab(list)
            }
        }
    }

    private fun showSubjectTab(linkEntityList: MutableList<LinkEntity>) {
        val subjectDataList = arrayListOf<SubjectData>()

        for (link in linkEntityList) {
            subjectDataList.add(
                SubjectData(
                    subjectId = link.link,
                    subjectName = link.name,
                    isOrder = false,
                    requireUpdateSetting = true,
                    filter = "type:全部" // 默认显示大图
                )
            )
        }

        val fragment = childFragmentManager.findFragmentByTag(SubjectTabFragment::class.java.name)
            ?: SubjectTabFragment()
        val bundle = arguments
        mListViewModel.columnCollection.value?.let {
            bundle?.putString(EntranceConsts.KEY_GAME_COLLECTION_ID, it.id)
            bundle?.putString(EntranceConsts.KEY_GAME_COLLECTION_TITLE, it.name)
        }
        bundle?.putParcelableArrayList(EntranceConsts.KEY_DATA, subjectDataList)
        bundle?.putBoolean(EntranceConsts.KEY_IS_COLUMN_COLLECTION, true)

        fragment.arguments = bundle
        mBinding?.placeholder?.visibility = View.VISIBLE
        childFragmentManager.beginTransaction().replace(R.id.placeholder, fragment, SubjectTabFragment::class.java.name)
            .commitAllowingStateLoss()
    }

    override fun provideListAdapter(): ColumnCollectionDetailAdapter {
        if (mAdapter == null) {
            mAdapter = ColumnCollectionDetailAdapter(requireContext(), mListViewModel, mEntrance)
        }
        return mAdapter!!
    }

    override fun onLoadEmpty() {
        super.onLoadEmpty()
        toast(R.string.content_delete_toast)
    }

    override fun isAutomaticLoad(): Boolean {
        return false
    }

    override fun provideListViewModel(): ColumnCollectionDetailViewModel {
        val factory = ColumnCollectionDetailViewModel.Factory(
            arguments?.getString(EntranceConsts.KEY_COLLECTION_ID)
                ?: ""
        )
        return ViewModelProviders.of(this, factory).get(ColumnCollectionDetailViewModel::class.java)
    }

}