package com.adealink.weparty.config

import com.adealink.frame.base.AppBase
import com.adealink.frame.util.ONE_MINUTE
import com.adealink.frame.util.PackageUtil
import com.adealink.weparty.channel.getChannel
import com.adealink.weparty.constant.ProductFlavor
import com.google.gson.annotations.SerializedName

data class GetGlobalConfigReq(
    @SerializedName("configs") val configs: List<Int>,
    @SerializedName("seqid") val seqId: Long = System.currentTimeMillis(),
    @SerializedName("versionCode") val versionCode: Int = PackageUtil.getVersionCode(),
    @SerializedName("platform") val platform: String = AppBase.platform,
    @SerializedName("packageName") val packageName: String = PackageUtil.getPackageName(),
    @SerializedName("channel") val channel: String = getChannel(),
)

data class GetGlobalConfigRes(
    @SerializedName("configs") val configsMap: Map<Int, List<String>>,
    @SerializedName("seqid") val seqId: Long = 0,
)

enum class GlobalConfigType(val value: Int) {
    //GLOBAL_VIDEO_ROOM_PLAYER_LOAD_TYPE(130)//视频房播放器初始化类型
    GLOBAL_APPROVING_VERSION(0), //提审配置
    ;

    companion object {
        fun map(value: Int): GlobalConfigType? {
            return values().firstOrNull { it.value == value }
        }
    }
}

data class ApprovingVersionConfig(
    @SerializedName("android")
    val android: String,
    @SerializedName("android_lite")
    val androidLite: String,
    @SerializedName("android_v2")
    val androidV2: String,
) {
    /**
     * 在 ≤ Yoki-v1.4.2(5) 版本，“审核中”的判断有问题：已过审的App也会出现功能不可见；
     * 新增 android_v2 新字段用于新的判断逻辑；
     * 优先使用 android_v2，再使用 android：是为了过一些版本后可再统一用回 android（舍弃android_v2）；
     */
    private fun getAndroidVersion(): Int {
        if (ProductFlavor.isCurrProductLite()) {
            return androidLite.toIntOrNull() ?: PackageUtil.getVersionCode()
        }

        if (androidV2.isNotEmpty()) {
            return androidV2.toIntOrNull() ?: PackageUtil.getVersionCode()
        }
        return android.toIntOrNull() ?: PackageUtil.getVersionCode()
    }

    fun isApproving(): Boolean {
        return PackageUtil.getVersionCode() > getAndroidVersion()
    }
}

const val TAG_GLOBAL_CONFIG = "tag_global_config"
const val GET_CONFIG_TIME_INTERVAL = 15 * ONE_MINUTE