package com.gh.gamecenter.entity

import android.os.Parcelable
import android.text.TextUtils
import com.gh.common.util.PlatformUtils
import com.gh.gamecenter.common.entity.CommunityEntity
import com.gh.gamecenter.common.entity.Display
import com.gh.gamecenter.common.entity.LinkEntity
import com.google.gson.annotations.SerializedName
import com.halo.assistant.HaloApp
import kotlinx.parcelize.Parcelize

@Parcelize
data class ApkEntity(
    @SerializedName("package")
    var packageName: String = "",
    @SerializedName("_id")
    var id: String = "",
    var size: String? = null,
    var url: String? = null,
    private var platform: String? = null,
    var version: String? = null,
    @SerializedName("gh_version")
    var ghVersion: String? = null,
    var etag: String? = null,
    var apkCollection: GameCollectionEntity? = null,
    var order: Int = 0,
    @SerializedName("active")
    var isActive: Boolean = true,
    var force: Boolean = false,
    var apkLink: ApkLink? = null,
    var plugin: String? = "",/*控制是否显示插件化 默认：open，取值有：open/only_index/only_game/close*/
    var time: Long? = null,
    var md5: String? = null, // 包体的 MD5 用于内型为畅玩的游戏使用用来更新
    @SerializedName("platform_name")
    private var platformName: String = "",
    val remark: String = "",
    @SerializedName("platform_icon")
    private var platformIcon: String = "",
    @SerializedName("download_instruction")
    val downloadInstruction: String = "",
    var recommend: Recommend? = null,
    @SerializedName("sign")
    var signature: String? = "",
    @SerializedName("version_code")
    var versionCode: Int = 0,

    // 以下是历史版本用的字段，其它地方可能会没有
    @SerializedName("update_time")
    var updateTime: Long = 0,
    @SerializedName("update_des")
    var updateDesc: String = "",
    @SerializedName("download_status")
    var downloadStatus: String = "",
    var format: String = ""
) : Parcelable {
    fun getPlatform(): String? {
        if (TextUtils.isEmpty(platform)) {
            return "官方版"
        }
        return platform
    }

    fun setPlatform(platform: String) {
        this.platform = platform
    }

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

    fun getPlatformName(): String {
        if (platformName.isNotEmpty()) {
            return platformName
        }

        return PlatformUtils
            .getInstance(HaloApp.getInstance().application)
            .getPlatformName(platform) ?: ""
    }

    fun setPlatformName(platformName: String) {
        this.platformName = platformName
    }

    fun getPlatformIcon(): String {
        if (platformIcon.isNotEmpty()) {
            return platformIcon
        }

        return PlatformUtils
            .getInstance(HaloApp.getInstance().application)
            .getPlatformPicUrl(platform) ?: ""
    }

    fun setPlatformIcon(platformIcon: String) {
        this.platformIcon = platformIcon
    }
}

@Parcelize
class ApkLink(
    @SerializedName("_id")
    val id: String = "",
    val name: String = "",
    @SerializedName("new_icon")
    val icon: String = "",
    val type: String = "",
    val link: String = "",
    val text: String = "",
    val sort: Int = 0,
    val collection: String = "",
    val remark: String = "",
    @SerializedName("link_community", alternate = ["community"])
    var community: CommunityEntity? = CommunityEntity(),
    var display: Display? = null
) : Parcelable {

    fun getLinkEntity(): LinkEntity {
        val linkEntity = LinkEntity()
        linkEntity.type = type
        linkEntity.link = link
        linkEntity.text = text
        linkEntity.community = community
        linkEntity.display = display
        return linkEntity
    }
}

@Parcelize
data class Recommend(val subscript: String = "", val description: String = "") : Parcelable