package com.gh.gamecenter.mygame

import android.os.Bundle
import android.view.View
import com.gh.common.exposure.ExposureListener
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.viewModelProvider
import com.gh.download.DownloadManager
import com.gh.gamecenter.common.baselist.ListAdapter
import com.gh.gamecenter.common.baselist.ListFragment
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.eventbus.EBDownloadStatus
import com.gh.gamecenter.eventbus.EBPackage
import com.gh.gamecenter.common.eventbus.EBReuse
import com.gh.gamecenter.login.user.UserManager
import com.lightgame.download.DataWatcher
import com.lightgame.download.DownloadEntity
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

open class PlayedGameFragment : ListFragment<GameEntity, PlayedGameViewModel>() {

    protected var mAdapter: PlayedGameAdapter? = null
    protected lateinit var mViewModel: PlayedGameViewModel

    protected var mExposureListener: ExposureListener? = null

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

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

        mListRv.addOnScrollListener(mExposureListener!!)
    }

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

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

    override fun provideListViewModel(): PlayedGameViewModel {
        val userId = arguments?.getString(EntranceConsts.KEY_USER_ID)
            ?: UserManager.getInstance().userId
        mViewModel = viewModelProvider(PlayedGameViewModel.Factory(userId))
        return mViewModel
    }

    override fun provideListAdapter(): ListAdapter<GameEntity> {
        return mAdapter ?: PlayedGameAdapter(requireContext(), mViewModel, false).apply {
            mAdapter = this
            mExposureListener = ExposureListener(this@PlayedGameFragment, this)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(changed: EBReuse) {
        if (Constants.LOGIN_TAG == changed.type) { // 登入
            onLoadRefresh()
        }
    }

    override fun onRefresh() {
        mAdapter?.clearPositionAndPackageMap()

        super.onRefresh()
    }

    // 下载被删除事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(status: EBDownloadStatus) {
        if ("delete" == status.status) {
            mAdapter?.notifyItemAndRemoveDownload(status)
        }
    }

    // 安装/卸载 事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(busFour: EBPackage) {
        if (busFour.isInstalledOrUninstalled()) {
            mAdapter?.notifyDataSetChanged()
        }
    }

}