package com.gh.vspace

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.exposure.ExposureListener
import com.gh.common.util.NewFlatLogUtils
import com.gh.download.DownloadManager
import com.gh.gamecenter.R
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.databinding.FragmentVdownloadManagerBinding
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.history.IBatchDelete
import com.gh.gamecenter.history.ManageOption
import com.lightgame.download.DataWatcher
import com.lightgame.download.DownloadEntity

class VDownloadManagerFragment :
    ListFragment<GameEntity, VDownloadManagerViewModel>(), IBatchDelete {

    private val mAdapter by lazy {
        VDownloadManagerAdapter(
            requireContext(),
            provideListViewModel()
        )
    }
    private val mViewModel: VDownloadManagerViewModel by lazy { viewModelProvider() }
    private val mBinding by lazy { FragmentVdownloadManagerBinding.inflate(layoutInflater) }
    private val mExposureListener by lazy { ExposureListener(this, mAdapter) }

    private val dataWatcher: DataWatcher = object : DataWatcher() {
        override fun onDataChanged(downloadEntity: DownloadEntity) {
            mAdapter.notifyItemByDownload(downloadEntity)
        }

        override fun onDataInit(downloadEntity: DownloadEntity) {
            mAdapter.notifyItemByDownload(downloadEntity)
        }
    }

    override fun provideListAdapter(): ListAdapter<*> = mAdapter
    override fun provideListViewModel(): VDownloadManagerViewModel = mViewModel

    override fun getLayoutId() = 0
    override fun getInflatedLayout() = mBinding.root

    override fun onCreate(savedInstanceState: Bundle?) {
        mViewModel.type = arguments?.get(VDownloadManagerViewModel.TYPE) as String
        mViewModel.isFromHomeRecent =
            arguments?.getBoolean(EntranceConsts.KEY_IS_FROM_HOME_RECENT) ?: false

        super.onCreate(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        DownloadManager.getInstance().addObserver(dataWatcher)
    }

    override fun onPause() {
        super.onPause()
        DownloadManager.getInstance().removeObserver(dataWatcher)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mCachedView.setBackgroundColor(R.color.background_white.toColor(requireContext()))

        mBinding.headerContainer.run {
            goneIf(mViewModel.type != VDownloadManagerViewModel.TYPE_DOWNLOADED)
            switchIv.visibility = View.GONE
            hintTv.text = "首页展示“最近在玩”板块"
            lottieView.setSwitchAnimation(SPUtils.getBoolean(Constants.SP_HOME_VGAME_AREA_ENABLED, true))
            lottieView.setOnClickListener {
                if (lottieView.isAnimating) return@setOnClickListener
                val status = SPUtils.getBoolean(Constants.SP_HOME_VGAME_AREA_ENABLED, true)
                lottieView.setSwitchAnimation(status)
                lottieView.playAnimation()
                SPUtils.setBoolean(Constants.SP_HOME_VGAME_AREA_ENABLED, !status)
                NewFlatLogUtils.logHaloFunManageRecentGameSwitch(!status)

                DownloadManager.getInstance().notifyDownloadLiveDataChanged()
            }
        }

        if (mViewModel.isTypeDownloaded()) {
            mListRv.addOnScrollListener(mExposureListener)
        }

        VHelper.vGameLiveData.observe(viewLifecycleOwner) {
            onLoadRefresh()
        }
    }

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

    override fun getItemDecoration(): RecyclerView.ItemDecoration? = null

    override fun shouldLoadMore() = false

    override fun isAutomaticLoad(): Boolean = false

    override fun onLoadRefresh() {
        mReuseNoData?.visibility = View.GONE
        mListViewModel.load(LoadType.REFRESH)
    }

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding.headerContainer.lottieView.setSwitchAnimation(
            SPUtils.getBoolean(
                Constants.SP_HOME_VGAME_AREA_ENABLED,
                true
            )
        )
        mAdapter.updatePopupWindow()
    }
}