package com.gh.gamecenter.entity

import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.entity.IconFloat
import com.gh.gamecenter.feature.entity.*
import com.gh.gamecenter.feature.exposure.ExposureEvent
import com.google.gson.annotations.SerializedName
import java.util.*

data class GameUpdateEntity(
    @SerializedName("game_id")
    var id: String = "",
    var name: String? = null,
    var icon: String? = null,
    @SerializedName("ori_icon")
    var rawIcon: String? = null,
    @SerializedName("icon_subscript")
    var iconSubscript: String? = "",
    @SerializedName("icon_float")
    var iconFloat: IconFloat? = null,
    @SerializedName("package")
    var packageName: String = "",
    var size: String? = null,
    var version: String? = null,
    var currentVersion: String? = null,
    @SerializedName("gh_version")
    var ghVersion: String? = null,
    var url: String? = null,
    var platform: String? = null,
    var readablePlatform: String? = "",
    var isPluggable: Boolean = false,
    var isPluggableDownloaded: Boolean = false, // 插件化是否被下载完成
    var etag: String? = null,
    var brief: String? = null,
    var tag: ArrayList<String>? = null,
    var tagStyle: ArrayList<TagStyleEntity> = ArrayList(),
    // 控制是否显示插件化 默认：open，取值有：open/only_index/only_game/close
    var plugin: String? = "",
    // 只控制首页是否显示插件化 open/close(此开关优先级最高)
    @SerializedName("index_plugin")
    var indexPlugin: String = "open",
    var exposureEvent: ExposureEvent? = null,
    var download: Int = 0/*7天下载量*/,
    @SerializedName("p_button_add_word")
    var pluginDesc: String = "", //插件功能描述
    var pluggableCollection: GameCollectionEntity? = null, // 插件化包所在的合集
    var format: String = "",
    var signature: String? = "",
    var category: String? = "",
    var md5: String? = "",
    var downloadStatus: String? = "",
    var landPageAddressDialog: GameEntity.AddressDialog? = null, // 第三方跳转落地页跳转链接

    var isVGameUpdate: Boolean = false
) {
    val categoryChinese: String
        get() = when (category) {
            "online" -> "网络"
            "local" -> "单机"
            "welfare" -> "福利"
            "simulator" -> "模拟器"
            else -> category ?: ""
        }

    val downloadStatusChinese: String
        get() = when (downloadStatus) {
            "on" -> "开启"
            Constants.V_GAME, Constants.V_GAME_32 -> "畅玩"
            "appointment" -> "预约"
            "demo" -> "试玩"
            else -> ""
        }

    /**
     * 是否为 mod 游戏，根据是否有 mod 标签来判断，不受镜像、游戏屏蔽影响
     */
    val isModdedGame: Boolean
        get() = tagStyle.any { it.name.lowercase(Locale.getDefault()) == "mod" }


    fun isShowPlugin(location: PluginLocation): Boolean {
        if (plugin.isNullOrEmpty() || "open" == plugin || plugin == location.name) {
            return true
        }
        return false
    }

    /**
     * 是否为第三方落地页跳转游戏
     */
    fun isLandPageAddressDialog(): Boolean {
        return landPageAddressDialog?.let { it.status == "on" } ?: false
    }

    /**
     * 第三方落地页的状态是否为“仅显示弹窗”
     */
    fun isLandPageAddressDialogShowOnly(): Boolean = landPageAddressDialog?.showOnly == true

    fun transformGameEntity(): GameEntity {
        val gameEntity = GameEntity()
        gameEntity.id = id
        gameEntity.name = name
        gameEntity.icon = icon
        gameEntity.rawIcon = rawIcon
        gameEntity.iconSubscript = iconSubscript
        gameEntity.iconFloat = iconFloat
        gameEntity.tagStyle = tagStyle
        gameEntity.brief = brief
        gameEntity.download = download
        gameEntity.isPluggable = true
        gameEntity.indexPlugin = indexPlugin
        gameEntity.pluginDesc = pluginDesc
        gameEntity.pluggableCollection = pluggableCollection
        gameEntity.category = category
        gameEntity.landPageAddressDialog = landPageAddressDialog

        val apkEntity = ApkEntity()
        apkEntity.url = url
        apkEntity.packageName = packageName
        apkEntity.size = size
        apkEntity.version = version
        apkEntity.ghVersion = ghVersion
        apkEntity.setPlatform(platform!!)
        apkEntity.etag = etag
        apkEntity.plugin = plugin
        apkEntity.format = format
        apkEntity.signature = signature

        val apk = ArrayList<ApkEntity>()
        apk.add(apkEntity)
        gameEntity.setApk(apk)
        return gameEntity
    }
}



