import Config from "../Config";
import { gameMethod } from "../common/gameMethod";
import HttpRequest from "../network/HttpRequest";
import { FormulaCom } from "./Formula";
import { ZeroGlobal } from "./ZeroGlobal";

interface RemoteConifgParam {
    preVersion: string //微信-忆游十三道
    policy?: string, // 隐私协议是否默认勾选 没有配置或者配置1默认勾选，配置0默认不勾选
    showTips?: string //是否显示启动页0.1折的提示
    showTipType?: string; // 显示启动页0.1折提示图片
    shShowTips?: string //审核服是否显示启动页0.1折的提示
    shShowTipType?: string; // 审核服显示启动页0.1折提示图片
    showAgeTip?: string; // 显示启动页适龄图片
    showBgType?: string; // 显示启动页背景图片
    adVideo?: string; // 是否有广告 0关，1开
    openTD?: number; // 数数数据上报开关 0关，1开
    kefu?: string; // 1开启客服按钮，0或者空关闭客服按钮
    beianUrl?: string; // 备案网址
    // preVersionPay: number // 审核服是否打开支付开关 0-关闭 1-开启 
}

const pidSwitch = {//正式服pid:审核服pid
    "1220":"1200",
    "1221":"1201",
    "1222":"1202",
    "1223":"1203",
    "1224":"1204",
    "1225":"1205",
}
export default class RemoteConfig {
    static init(cb: Function) {
        console.log("paltform=>", cc.sys.platform)
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER || cc.sys.platform == cc.sys.MOBILE_BROWSER || cc.sys.platform == cc.sys.WECHAT_GAME || cc.sys.platform == cc.sys.BYTEDANCE_GAME
            || cc.sys.platform == cc.sys.ANDROID || cc.sys.platform == cc.sys.IPHONE || cc.sys.platform == cc.sys.IPAD
            || cc.sys.platform == cc.sys.TAOBAO_MINIGAME || cc.sys.platform == cc.sys.VIVO_GAME || cc.sys.platform == cc.sys.XIAOMI_GAME || cc.sys.platform == cc.sys.OPPO_GAME) {
            // 只在微信小游戏上使用此入口文件,用来设定pid值
            //电魂sdk漏斗打点
            if (window['dhSDK']) {
                // 事件ID
                let eventId = "100101::GAME_CREATE_VERSION"; // 表示SDK客户端上报的启动事件
                // 事件参数
                let eventParams = {};
                // 必填参数
                eventParams["logLevel"] = "d";
                eventParams["logContent"] = "检查版本开始";
                // 执行上报
                window['dhSDK'].trackEvent(eventId, eventParams);
            }
            HttpRequest.GET(Config.remoteVersionUrl, (data: RemoteConifgParam) => {
                if (data == null) {
                    Config.pid = ZeroGlobal.getKey("packageID") || '202'
                    cb()
                } else {
                    let _pid = ZeroGlobal.getKey("packageID") || '202'
                    console.log("==原始PID==", _pid)
                    let preVersion = data["preVersion_" + _pid] || "10.0.0"
                    let adVideo = data["adVideo" + _pid] || "1"
                    let openTD = data["openTD" + _pid] || "0"
                    if (gameMethod.isEmpty(preVersion)) {
                        Config.pid = ZeroGlobal.getKey("packageID") || '202'
                        Config.policy = data.policy
                        Config.kefu = data.kefu
                        Config.showTips = data.showTips
                        Config.showTipType = data.showTipType
                        Config.showAgeTip = data.showAgeTip
                        Config.showBgType = data.showBgType
                        Config.adVideo = adVideo
                        Config.openTD = openTD
                        Config.beianUrl = data.beianUrl
                        console.log("获取远程版本号失败，设置pid==", Config.pid)
                        cb()
                        return
                    }
                    console.log("==远程版本号==", preVersion)
                    console.log("==本地版本号==", Config.appVersion)
                    let compare = FormulaCom.compareVersion(Config.appVersion, preVersion)
                    console.log("==版本对比==", compare)

                    Config.showTips = data.showTips
                    Config.showTipType = data.showTipType
                    Config.showAgeTip = data.showAgeTip
                    Config.showBgType = data.showBgType
                    Config.openTD = openTD

                    if (compare >= 0) {
                        // 是审核版本,进入审核服
                        if (_pid == "102") {
                            Config.pid = "103"
                            Config.showBgType = "0"
                        } else if (_pid == "202") {
                            Config.pid = "201"
                            Config.showBgType = "0"
                        } else if (_pid == "902") {
                            Config.pid = "901"
                        } else if (pidSwitch[_pid]) {
                            Config.pid = "" + pidSwitch[_pid]
                            Config.showTips = data.shShowTips
                            Config.showTipType = data.shShowTipType
                            if (data["adVideo" + Config.pid]) {
                                Config.adVideo = data["adVideo" + Config.pid];//用切服后的pid取广告配置
                            }

                        }
                    } else {
                        Config.pid = ZeroGlobal.getKey("packageID") || '202'
                    }
                    Config.policy = data.policy
                    Config.kefu = data.kefu
                    Config.adVideo = adVideo
                    console.log("设置PID:", Config.pid)
                    console.log("policy:", Config.policy)
                    console.log("kefu:", Config.kefu)
                    console.log("showTips:", Config.showTips)
                    console.log("showTipType:", Config.showTipType)
                    console.log("showAgeTip:", Config.showAgeTip)
                    console.log("showBgType:", Config.showBgType)
                    console.log("adVideo:", Config.adVideo)
                    console.log("openTD:", Config.openTD)
                    cb()
                }
            })
        } else {
            Config.pid = ZeroGlobal.getKey("packageID") || '1'
            cb()
        }
    }
}