
/**
 * 全局变量 sdk
 */


let sdk = new class {

    readonly default: string = "csj"
    readonly tt: string = "tt"
    readonly wx: string = "wx"
    readonly android: string = "android"

    private deviceId: string
    //安卓广告渠道
    public ANDROID_AD_CHANNEL = {
        CSJ: "csj",
        YLH: "ylh",
        KS: "ks",
    }
    channel: string
    currSDK: any
    // 视频回调
    private videoSucceed: Function
    private videoFail: Function
    //每天广告总数限制
    public readonly VIDEO_LIMIT: number = 10000

    constructor() {
        setTimeout(() => {
            this.init()
        }, 100)
    }

    init() {
        this.channel = this.default
        this.currSDK = TestDefault
        if (cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID) {
            this.channel = this.android
            this.currSDK = android
        }

        if (this.currSDK && this.currSDK.init) {
            this.currSDK.init()
        }
        console.log("sdk.channel = " + this.channel)
    }



    public getDeviceId() {
        if (this.deviceId) {
            return this.deviceId
        }
        this.deviceId = cc.sys.localStorage.getItem("zsdk_deviceid")
        if (this.deviceId) {
            if (CC_JSB && cc.sys.os == cc.sys.OS_ANDROID && jsb.reflection) {
                jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "setDeviceId", "(Ljava/lang/String;)V", this.deviceId)
            }
            return this.deviceId
        }
        this.deviceId = new Date().getTime() + "-" +parseInt( Math.random() * 10000+"")
        cc.sys.localStorage.setItem('zsdk_deviceid', this.deviceId)
        if (CC_JSB && cc.sys.os == cc.sys.OS_ANDROID && jsb.reflection) {
            jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "setDeviceId", "(Ljava/lang/String;)V", this.deviceId)
        }
        return this.deviceId
    }


    // ======================= video =======================
    // ======================= video =======================
    // ======================= video =======================
    isHaveVideo() {
        if (this.currSDK && this.currSDK.isHaveVideo) {
            return this.currSDK.isHaveVideo()
        }
        return false
    }
    //激励视频
    showVideoAd(succeed: Function, fail: Function) {
        console.log("showVideoAd>>")
        this.videoSucceed = succeed
        this.videoFail = fail
        if (window["TTSliderBarAward"] == 1) {
            window["TTSliderBarAward"] = 0
            sdk.onVideoSucceed()
            return
        }
        if (this.currSDK && this.currSDK.showVideoAd) {
            this.currSDK.showVideoAd()
            return
        }
        sdk.onVideoFail()
    }
    /**
     * 视频成功回调
     */
    onVideoSucceed() {
        this.videoSucceed && this.videoSucceed()
        this.videoSucceed = null
        this.videoFail = null
        ADmultiTool.saveEcpm(sdk.ANDROID_AD_CHANNEL.CSJ)
    }

    //激励视频,安卓多渠道
    showVideoAdMulti(channel: string, succeed: Function, fail: Function) {
        console.log("showVideoAdMulti")
        if (CC_JSB && cc.sys.os == cc.sys.OS_ANDROID && jsb.reflection) {
            switch (channel) {
                case sdk.ANDROID_AD_CHANNEL.CSJ:
                    android && android.showVideoAdCSJ()
                    break;
                case sdk.ANDROID_AD_CHANNEL.YLH:
                    android && android.showVideoAdYLH()
                    break;
                case sdk.ANDROID_AD_CHANNEL.KS:
                    android && android.showVideoAdKS()
                    break;
                default:
                    console.log("channel erro:" + channel)
                    return;
            }
            this.videoSucceed = succeed
            this.videoFail = fail
        }
    }
    /**
     * 安卓渠道单独的回调
     * @param channel 
     */
    onVideoSucceedMulti(channel: string) {
        this.videoSucceed && this.videoSucceed(channel)
        this.videoSucceed = null
        this.videoFail = null
    }

    /**
     * 视频失败回调
     */
    onVideoFail() {
        this.videoFail && this.videoFail()
        this.videoSucceed = null
        this.videoFail = null
    }

    showInterstitial() {
        if (this.currSDK && this.currSDK.showInterstitial) {
            this.currSDK.showInterstitial()
        }
    }
    share(parm?) {
        if (this.currSDK && this.currSDK.shareAppMessage) {
            this.currSDK.shareAppMessage(parm)
        }
    }

}

/**
 * 广告单价数据
 */
class AdData {
    channel: string = ""
    price: number = 0 //单价
    private today_earning: { [key: number]: number } = {} //今日收入
    private count_today: { [key: number]: number } = {} //今天点击
    private count_month: { [key: number]: number } = {} //月点击
    constructor(data: AdData) {
        if (data) {
            this.channel = data.channel
            this.price = data.price
            this.today_earning = data.today_earning
            this.count_today = data.count_today
            this.count_month = data.count_month
        }
    }
    //添加数据
    addData(price: number) {
        let day = ZTime.getTimeYMD()
        let mon = ZTime.getTimeYM()
        this.price = price
        this.today_earning[day] = (this.today_earning[day] || 0) + price
        this.count_today[day] = (this.count_today[day] || 0) + 1
        this.count_month[mon] = (this.count_month[mon] || 0) + 1
    }

    getTodayEarning(): number {
        let day = ZTime.getTimeYMD()
        return this.today_earning[day] || 0
    }

    getCountToday(): number {
        let day = ZTime.getTimeYMD()
        return this.count_today[day] || 0
    }

    getCountMonth(): number {
        let mon = ZTime.getTimeYM()
        return this.count_month[mon] || 0
    }

}

window["sdk"] = sdk
window["AdData"] = AdData
