import JsbSystem from "./JsbSystem";

/**
 * 广告管理类脚本
 * @description 显示/播放广告
 * @author 冯聪
 */
export default class AdSystem {
    
    /** 构造函数
     * @param adSdk 传入当前使用广告sdk
     */
    constructor(adSdk: any) {
        this.adSdk = adSdk;
        this.init();
    }

    /** 广告sdk */
    public adSdk: any = null;

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

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

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

    /**
     * 观看广告
     * @param callBack 广告相关的回调
     */
    public watchAd(callBack: Function) {

        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() {
        this.videoAdcallBack(false);
    }

    /** 观看广告取消 */
    public watchAdCancel() {
        this.videoAdcallBack(false);
    }

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

    /** 显示原生广告 */
    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)
        }
    }
}
