package com.gh.gamecenter.catalog

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.gamecenter.common.utils.viewModelProviderFromParent
import com.gh.gamecenter.databinding.FragmentSubCatalogBinding
import com.gh.gamecenter.entity.CatalogEntity
import com.gh.gamecenter.common.base.fragment.ToolbarFragment

class SubCatalogFragment : ToolbarFragment() {

    private var mBinding: FragmentSubCatalogBinding? = null
    private var mViewModel: SubCatalogViewModel? = null
    private var mCatalogViewModel: CatalogViewModel? = null
    private var mEntity: CatalogEntity? = null
    private var mCatalogId: String = ""
    private var mCatalogTitle: String = ""
    private var mPrimaryCatalogId: String = ""

    override fun getLayoutId() = 0

    override fun getInflatedLayout() = FragmentSubCatalogBinding.inflate(layoutInflater).apply { mBinding = this }.root

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mCatalogId = arguments?.getString(EntranceConsts.KEY_CATALOG_ID) ?: ""
        mCatalogTitle = arguments?.getString(EntranceConsts.KEY_CATALOG_TITLE) ?: ""
        mPrimaryCatalogId = arguments?.getString(EntranceConsts.KEY_PRIMARY_CATALOG_ID) ?: ""
        mViewModel = viewModelProvider(SubCatalogViewModel.Factory(mCatalogId))
        mCatalogViewModel = viewModelProviderFromParent(CatalogViewModel.Factory(mCatalogId, mCatalogTitle), mCatalogId)
        mViewModel?.getSubCatalogs(mPrimaryCatalogId)
    }

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

        mViewModel?.catalogs?.observe(viewLifecycleOwner, Observer {
            mBinding?.run {
                reuseLoading.root.visibility = View.GONE
                if (it != null) {
                    reuseNoConnection.root.visibility = View.GONE
                    if (it.subCatalog.isNotEmpty()) {
                        rvSubCatalog.visibility = View.VISIBLE
                        reuseNoneData.root.visibility = View.GONE
                        mEntity = it
                        initView()
                    } else {
                        rvSubCatalog.visibility = View.GONE
                        reuseNoneData.root.visibility = View.VISIBLE
                    }
                } else {
                    rvSubCatalog.visibility = View.GONE
                    reuseNoneData.root.visibility = View.GONE
                    reuseNoConnection.root.visibility = View.VISIBLE
                    reuseNoConnection.root.setOnClickListener {
                        reuseLoading.root.visibility = View.VISIBLE
                        mViewModel?.getSubCatalogs(mPrimaryCatalogId)
                    }
                }
            }
        })
    }

    private fun initView() {
        mEntity?.takeIf { mCatalogViewModel != null }?.run {
            mBinding?.rvSubCatalog?.layoutManager = GridLayoutManager(requireContext(), 3)
            mBinding?.rvSubCatalog?.adapter = SubCatalogAdapter(requireContext(), mCatalogViewModel!!, this, subCatalog)
        }
    }

    fun changeSubCatalog(primaryCatalogId: String) {
        mBinding?.run {
            mPrimaryCatalogId = primaryCatalogId
            rvSubCatalog.visibility = View.GONE
            reuseNoneData.root.visibility = View.GONE
            reuseNoConnection.root.visibility = View.GONE
            reuseLoading.root.visibility = View.VISIBLE
            mViewModel?.getSubCatalogs(mPrimaryCatalogId)
        }
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding?.rvSubCatalog?.adapter?.run {
            mBinding?.rvSubCatalog?.recycledViewPool?.clear()
            notifyItemRangeChanged(0, itemCount)
        }
    }
}