require("./AnyThinkAds/ATInterstitialJSSDK");
require("./AnyThinkAds/ATJSSDK");

var ATInterstitialJS = cc.Class({

    name: "ATInterstitialJS",

    /** 代理脚本(TS) */
    proxy: null,

    /** 插屏类型 */
    interType: 0,

    ctor: function () {
        // console.log("ATNativeJS created! ");
    },

    init: function () {
        ATBannerJSSDK.setAdListener(this);
    },

    /** 广告位id */
    placementID: function () {
        return this.proxy.placementID(this.interType);
    },

    /**
     * 插屏广告加载
     * @param {*} type 0 插屏 1 全屏
     */
    loadInter: function (type) {
        //v5.6.8新增，只针对Sigmob平台，Sigmob的激励视频广告源当做插屏使用
        var setting = {};
        setting[ATInterstitialJSSDK.UseRewardedVideoAsInterstitial] = false;
        this.interType = type
        ATInterstitialJSSDK.setAdListener(this);
        //setting[ATInterstitialJSSDK.UseRewardedVideoAsInterstitial] = true;
        if (this.interType == 0) {
            ATInterstitialJSSDK.loadInterstitial(this.placementID(), setting);
            // ATInterstitialJSSDK.showAd(this.interPlacementId);
        }
        else if (this.interType == 1) {
            ATInterstitialJSSDK.loadInterstitial(this.placementID(), setting);
            // ATInterstitialJSSDK.showAd(this.fullInterPlacementId);
        }
    },

    /**
     * 检查插屏是否加载成功
     * @param {*} type 0 插屏 1 全屏
     * @returns 
     */
    checkInterReady: function (type) {
        if (this.proxy == null) return;
        if (type == 0) {
            return ATInterstitialJSSDK.hasAdReady(this.placementID()) ? true : false;
        }
        else if (type == 1) {
            return ATInterstitialJSSDK.hasAdReady(this.placementID()) ? true : false;
        }
    },

    /**
     * 显示插屏
     * @param {*} type 0 插屏 1 全屏
     */
    showInter: function (type) {
        if (type == 0) {
            ATInterstitialJSSDK.showAd(this.placementID());
        }
        else if (type == 1) {
            ATInterstitialJSSDK.showAd(this.placementID());
        }
    },

    /**
     * 插屏广告加载成功
     * @param {*} placementId 广告位Id
     * @returns 
     */
    onInterstitialAdLoaded: function (placementId) {
        //ATJSSDK.printLog("AnyThinkInterstitialDemo::onInterstitialAdLoaded(" + placementId + ")");
        if (this.proxy == null) return;
        this.proxy.onInterstitialAdLoaded(placementId);
    },

    /**
     * 插屏广告加载失败
     * @param {*} placementId 广告位Id
     * @param {*} errorInfo   错误信息
     * @returns 
     */
    onInterstitialAdLoadFail: function (placementId, errorInfo) {
        //ATJSSDK.printLog("AnyThinkInterstitialDemo::onInterstitialAdLoadFail(" + placementId + ", " + errorInfo + ")");   
        if (this.proxy == null) return;
        this.proxy.onInterstitialAdLoadFail(placementId, errorInfo);
    },

    /**
     * 插屏广告显示成功
     * @param {*} placementId  广告位id
     * @param {*} callbackInfo 回调信息
     * @returns 
     */
    onInterstitialAdShow: function (placementId, callbackInfo) {
        //ATJSSDK.printLog("AnyThinkInterstitialDemo::onInterstitialAdShow("  + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onInterstitialAdShow(placementId, callbackInfo);
    },
    //广告视频开始播放，部分平台有此回调

    /**
     * 插屏广告开始播放视屏，部分广告平台有此回调
     * @param {*} placementId  广告位id 
     * @param {*} callbackInfo 回调信息
     */
    onInterstitialAdStartPlayingVideo: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkInterstitialDemo::onInterstitialAdStartPlayingVideo("  + placementId + ", " + callbackInfo + ")");
    },

    /**
     * 插屏广告视频播放结束,部分广告平台有此回调
     * @param {*} placementId  广告位id
     * @param {*} callbackInfo 回调信息
     */
    onInterstitialAdEndPlayingVideo: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkInterstitialDemo::onInterstitialAdEndPlayingVideo("  + placementId + ", " + callbackInfo + ")");
    },

    /**
     * 插屏广告视频播放失败，部分广告平台有此回调
     * @param {*} placementId 广告位id
     * @param {*} errorInfo   错误回调
     */
    onInterstitialAdFailedToPlayVideo: function (placementId, errorInfo) {
        // ATJSSDK.printLog("AnyThinkInterstitialDemo::onInterstitialAdFailedToPlayVideo(" + placementId + ", " + errorInfo + ")");
    },

    /**
     * 插屏广告显示失败
     * @param {*} placementId 广告位id
     */
    onInterstitialAdFailedToShow: function (placementId) {
        // ATJSSDK.printLog("AnyThinkInterstitialDemo::onInterstitialAdFailedToShow(" + placementId + ")");
    },

    /**
     * 广告被关闭
     * @param {*} placementId  广告位id
     * @param {*} callbackInfo 回调信息
     * @returns 
     */
    onInterstitialAdClose: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkInterstitialDemo::onInterstitialAdClose("  + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onInterstitialAdClose(placementId, callbackInfo);
    },

    /**
     * 广告被点击
     * @param {*} placementId  广告位id
     * @param {*} callbackInfo 回调信息
     * @returns 
     */
    onInterstitialAdClick: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkInterstitialDemo::onInterstitialAdClick("  + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onInterstitialAdClick(placementId, callbackInfo);
    }
});

module.exports = ATInterstitialJS;
if (!cc["ATInterstitialJS"]) {
    cc["ATInterstitialJS"] = new ATInterstitialJS();
}
