package com.gh.gamecenter.gamedetail.detail.viewholder

import android.content.Context
import android.view.View
import androidx.annotation.CallSuper
import androidx.recyclerview.widget.RecyclerView
import com.gh.download.DownloadManager
import com.gh.gamecenter.feature.view.DownloadButton
import com.gh.gamecenter.gamedetail.GameDetailViewModel
import com.gh.gamecenter.gamedetail.entity.GameDetailData
import com.lightgame.download.DownloadStatus

abstract class BaseGameDetailItemViewHolder(
    val itemView: View,
    val downloadBtn: DownloadButton?,
    val viewModel: GameDetailViewModel
) : RecyclerView.ViewHolder(itemView) {
    protected val context: Context = itemView.context
    protected var itemData: GameDetailData? = null
    protected val gameId
        get() = viewModel.game?.id ?: ""
    protected val gameName
        get() = viewModel.game?.name ?: ""
    protected val gameType
        get() = viewModel.game?.categoryChinese ?: ""
    protected val moduleType
        get() = itemData?.typeChinese ?: ""
    protected val sequence
        get() = itemData?.position?.plus(1) ?: 0
    protected val gameStatus
        get() = getGameStatus(downloadBtn?.buttonStyle, viewModel)

    protected val baseTrackData
        get() = GameDetailModuleTrackData(
            gameId = gameId,
            gameName = gameName,
            gameType = gameId,
            moduleType = moduleType,
            sequence = sequence,
        )

    @CallSuper
    open fun bindView(data: GameDetailData) {
        itemData = data
    }

    data class GameDetailModuleTrackData(
        var gameId: String = "",
        var gameName: String = "",
        var gameType: String = "",
        var gameStatus: String = "",
        var moduleType: String = "",
        var moduleName: String = "",
        var sequence: Int = 0,
        var subModuleName: String = "",
        var supSequence: Int = 0
    )

    companion object {
        fun getGameStatus(buttonStyle: DownloadButton.ButtonStyle?, viewModel: GameDetailViewModel) = when (buttonStyle) {
            DownloadButton.ButtonStyle.WAITING,
            DownloadButton.ButtonStyle.UPDATING,
            DownloadButton.ButtonStyle.PAUSE,
            DownloadButton.ButtonStyle.FAILURE,
            DownloadButton.ButtonStyle.DOWNLOADING_NORMAL,
            DownloadButton.ButtonStyle.DOWNLOADING_PLUGIN -> "下载中"

            DownloadButton.ButtonStyle.LAUNCH_OR_OPEN -> "已安装"
            DownloadButton.ButtonStyle.RESERVABLE -> "预约"
            DownloadButton.ButtonStyle.RESERVED -> "已预约"
            else -> determineStatus(viewModel)
        }

        private fun determineStatus(viewModel: GameDetailViewModel): String {
            viewModel.updateGameStatus()
            if (viewModel.isGameInstalled) {
                return "已安装"
            }

            val downloadEntity = DownloadManager.getInstance().getDownloadEntitySnapshot("", viewModel.gameId)
            return when (downloadEntity?.status) {
                DownloadStatus.done -> "待安装"
                null,
                DownloadStatus.cancel,
                DownloadStatus.hijack,
                DownloadStatus.notfound,
                DownloadStatus.uncertificated,
                DownloadStatus.unqualified,
                DownloadStatus.unavailable,
                DownloadStatus.banned -> "未下载"
                else -> "下载中"
            }
        }
    }
}