package com.gh.gamecenter.game

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import com.gh.download.DownloadManager
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.gamecenter.core.iinterface.IOffsetable
import com.gh.gamecenter.entity.SubjectEntity
import com.gh.gamecenter.entity.SubjectRecommendEntity
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.game.data.GameItemData
import com.gh.gamecenter.home.BlankDividerViewHolder
import com.gh.gamecenter.home.test_v2.HomeGameTestV2DownloadStateUpdateHelper

abstract class BaseGameViewModel(application: Application, open var blockData: SubjectRecommendEntity?) : AndroidViewModel(application), IOffsetable {

    private var mOffsetMap: HashMap<Int, Int> = hashMapOf()

    protected val mItemDataListCache: MutableList<GameItemData> = ArrayList()

    val loadStatus = MutableLiveData<LoadStatus>()
    val commandScrollTop = MutableLiveData<Any>()

    var itemDataList: MediatorLiveData<List<GameItemData>> = MediatorLiveData()

    var entrance: String = ""

    var positionAndPackageMap = HashMap<String, Int>() // key: packageName + position, value: position

    private var mHomeGameTestV2DownloadStateUpdateHelper: HomeGameTestV2DownloadStateUpdateHelper? = null

    override fun getOffset(position: Int) = mOffsetMap[position] ?: 0

    override fun updateOffset(position: Int, offset: Int) {
        mOffsetMap[position] = offset
    }

    override fun resetOffset() {
        mOffsetMap.clear()
    }

    fun initData() {
        onInitData()
    }

    fun isGameRepo(): Boolean {
        return blockData?.name?.contains("游戏库") == true
    }

    fun setHomeGameTestV2DownloadStateUpdateHelper(helper: HomeGameTestV2DownloadStateUpdateHelper) {
        mHomeGameTestV2DownloadStateUpdateHelper = helper
        mHomeGameTestV2DownloadStateUpdateHelper?.setOnGameListAddCallback { position, gameEntities ->
            gameEntities.forEach {
                addGamePositionAndPackage(it, position)
            }
        }
    }

    protected fun addGamePositionAndPackage(game: GameEntity, position: Int) {
        var packages = ""
        for (apkEntity in game.getApk()) {
            packages += apkEntity.packageName
        }
        positionAndPackageMap[packages + position] = position
        game.gameLocation = GameEntity.GameLocation.INDEX
        game.setEntryMap(DownloadManager.getInstance().getEntryMap(game.name))
    }

    protected fun addGamePositionAndPackage(game: GameEntity) {
        var packages = ""
        for (apkEntity in game.getApk()) {
            packages += apkEntity.packageName
        }
        positionAndPackageMap[packages + (mItemDataListCache.size - 1)] = mItemDataListCache.size - 1
        game.gameLocation = GameEntity.GameLocation.INDEX
        game.setEntryMap(DownloadManager.getInstance().getEntryMap(game.name))
    }

    protected fun appendAdditionalInfoToSubjectGame(subject: SubjectEntity, outerPosition: Int) {
        subject.outerSequence = outerPosition
        subject.data?.let {
            for ((index, game) in it.withIndex()) {
                // 开测表不是专题不需要补充专题 id
                if (subject.tag != "test") {
                    game.subjectId = subject.id
                }
                game.sequence = index
                game.containerId = blockData?.link
                game.containerType = ExposureEntity.BLOCK_ID
                game.outerSequence = outerPosition
            }
        }
    }

    /**
     * 获取填充间距的空白 item
     */
    protected fun getBlankSpacingItem() =
        GameItemData().apply { blankDivider = BlankDividerViewHolder.DEFAULT_BLANK_HEIGHT * 2 }

    protected abstract fun onInitData()

    abstract fun getSubjectList()

    abstract fun replaceRefreshData(itemData: GameItemData): Boolean

    abstract fun changeSubjectGame(subjectId: String)

    abstract fun refreshDiscoverCardData()

    abstract fun changeGameCollectionRefresh(collectionId: String, isRefreshClick: Boolean): Boolean

}