package com.gh.vspace

import com.gh.gamecenter.manager.PackagesManager
import com.lightgame.download.DownloadEntity
import com.lightgame.download.DownloadStatus

data class VGameItemData(
    var downloadEntity: DownloadEntity,

    var shouldShowMask: Boolean = false,
    var shouldShowProgressBar: Boolean = false,
    var shouldShowUpdate: Boolean = false,
    var shouldShowControlTv: Boolean = false,
    var shouldShowDot: Boolean = false,
    var controlText: String = "继续"
) {
    fun refresh() {
        if (downloadEntity.status == DownloadStatus.done
            && VHelper.getLastPlayedTime(downloadEntity) == 0L
        ) {
            shouldShowDot = true
        }

        if (PackagesManager.isCanUpdate(
                downloadEntity.gameId,
                downloadEntity.packageName
            )
        ) {
            shouldShowUpdate = true
        }

        when (downloadEntity.status) {
            DownloadStatus.done -> {
                controlText = ""
            }
            DownloadStatus.pause,
            DownloadStatus.subscribe,
            DownloadStatus.waiting -> {
                shouldShowMask = true
                shouldShowProgressBar = true
                shouldShowControlTv = true

                controlText = if (downloadEntity.status == DownloadStatus.waiting) {
                    "等待中"
                } else {
                    "继续"
                }
            }
            DownloadStatus.downloading -> {
                shouldShowMask = true
                shouldShowProgressBar = true
                controlText = ""
            }

            DownloadStatus.cancel,
            DownloadStatus.timeout,
            DownloadStatus.neterror,
            DownloadStatus.hijack,
            DownloadStatus.uncertificated,
            DownloadStatus.unqualified,
            DownloadStatus.notfound,
            DownloadStatus.unavailable,
            DownloadStatus.overflow -> {
                controlText = "重试"
            }
            else -> {
                // do nothing
            }
        }
    }

    companion object {
        fun from(downloadEntity: DownloadEntity): VGameItemData {
            return VGameItemData(downloadEntity).also { it.refresh() }
        }
    }
}