import { IADSDKMgr } from "../sdk/ad/IADSDKMgr";
import JsbSystem from "./JsbSystem";

/**
 * 广告管理类脚本
 * @description 显示/播放广告
 * @author 冯聪
 */
export default class AdSystem {

    /** 构造函数
     * @param adSdk 传入当前使用广告sdk
     */
    constructor(adSdk: IADSDKMgr) {
        this.adSdk = adSdk;
        // this.init();
    }

    /** 广告sdk */
    private adSdk: IADSDKMgr = null;

    /** 是否显示广告 */
    public ifShowAd: boolean = true;

    /** 视频广告回调 */
    public videoAdcallBack: Function = null;

    /** 初始化广告 */
    public init() {

        //判定下平台
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            this.ifShowAd = true;
        }
        else {
            this.ifShowAd = false;
        }

        //初始化Topon广告SDK
        this.adSdk.initSDK();
    }

    /**
     * 观看广告
     * @param callBack 广告相关的回调
     */
    public watchAd(callBack: Function) {
        if (this.isAdCooling()) return;

        this.videoAdcallBack = callBack;

        if (!this.ifShowAd) {
            this.wacthAdSuccess()
        }
        else {
            if (cc.sys.platform != cc.sys.WECHAT_GAME) {
                if (cc.sys.os == cc.sys.OS_ANDROID) {
                    // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
                    this.adSdk.showVideoAd();
                }
                else if (cc.sys.os == cc.sys.OS_IOS) {
                    jsb.reflection.callStaticMethod("AppController", "showVideo", "");
                }
            }
            else {
                // WxMgr.getInstance().showRewardAd(pop)
            }
        }
    }

    /** 观看广告失败 */
    public watchAdFail() {
        mk.console.log("=== watchAdFail");
        this.videoAdcallBack(false);
    }

    /** 观看广告取消 */
    public watchAdCancel() {
        mk.console.log("=== watchAdCancel");
        this.videoAdcallBack(false);
    }

    /**
     * 观看广告成功
     * @param placementId 广告位id
     */
    public wacthAdSuccess(placementId = '') {
        mk.console.log("=== wacthAdSuccess");
        this.videoAdcallBack(true);
        this.initLastTimeStamp();
    }

    /** 显示原生广告 */
    public showNativeAd(type = 0, show = true) {
        if (cc.sys.platform != cc.sys.WECHAT_GAME) {
            if (cc.sys.os == cc.sys.OS_ANDROID) {
                // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
                this.adSdk.loadNativeAd(type, show);
            }
            else if (cc.sys.os == cc.sys.OS_IOS) {

            }
        }
        else {
            // WxMgr.getInstance().showRewardAd(pop)
        }
    }

    /** 销毁原生广告 */
    public destroyNativeAd() {
        if (cc.sys.platform != cc.sys.WECHAT_GAME) {
            if (cc.sys.os == cc.sys.OS_ANDROID) {
                // jsb.reflection.callStaticMethod("org/cocos2dx/javascript/config/TTAdManagerHolder", "showVideo", "(I)V", pop);
                this.adSdk.destroyNativeAd();
            }
            else if (cc.sys.os == cc.sys.OS_IOS) {

            }
        }
        else {
            // WxMgr.getInstance().showRewardAd(pop)
        }
    }

    /** 上次播放视频时间戳,单位：毫秒 */
    private last_time_stamp: number = 0;
    private initLastTimeStamp() {
        let new_date = new Date();
        this.last_time_stamp = new_date.getTime();
    }

    /**
     * 视频是否冷却
     * @returns 视频冷却中
     */
    private isAdCooling(): boolean {
        let new_date = new Date();
        let new_time_stamp = new_date.getTime();
        let gap_time_stamp = new_time_stamp - this.last_time_stamp;
        if (gap_time_stamp <= 5000) {
            const time = 6 - Math.ceil(gap_time_stamp / 1000);
            mk.tip.pop('当前看视频太频繁了，请' + time + '秒后再试');
            return true;
        }
        return false;
    }

    public showBannerAd() {
        if (!this.ifShowAd) {
            return;
        }
        this.adSdk.showBannerAd();
    }

    /**
     * showInterAd
     * @param type
     */
    public showInterAd(type: number) {
        if (!this.ifShowAd) {
            return;
        }
        this.adSdk.showInter();
    }

    /**
     * 根据几率检测弹出插屏
     */
    public checkShowInterByChance() {
        let random = Math.random();
        if (random < 0.5) {
            this.showInterAd(0);
        }
    }
}
