import ATSDKMgr from "../sdk/ad/toppon/AnyThinkAdsMgr/ATSDKMgr";
import { zgSingleton } from "../sdk/ad/toppon/zgSingleton";
import JsbSystem from "./JsbSystem";

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

    constructor() {
        super();
        this.init();
    }

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

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

    /** 初始化广告 */
    public init() {
        //获取广告渠道id
        this.getAdvertiseChannel();
        //初始化Topon广告SDK
        ATSDKMgr.getInstance().initATSDK();
    }

    /** 获取投放渠道 和 广告位ID信息 */
    public getAdvertiseChannel() {
        let str = JsbSystem.getAdvertisingChannel();
        let arr = str.split(',')
        let channel = arr[0]
        ATSDKMgr.topon_android_appId = arr[1]
        ATSDKMgr.topon_android_appSecret = arr[2]
        ATSDKMgr.topon_rewardVideo_placementId = arr[3]
        ATSDKMgr.topon_inter_placementId = arr[4]
        ATSDKMgr.topon_native_placementId = arr[5]
        if (arr.length >= 7 && arr[6]) {
            ATSDKMgr.topon_fullinter_placementId = arr[6]
        }
    }

    /**
     * 观看广告
     * @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);
                    ATSDKMgr.getInstance().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);
                ATSDKMgr.getInstance().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);
                ATSDKMgr.getInstance().destroyNativeAd();
            }
            else if (cc.sys.os == cc.sys.OS_IOS) {

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