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

var ATRewardVideoJS = cc.Class({

    name: "ATRewardVideoJS",

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

    ctor: function () {
        console.log("ATRewardVideoJS.js created! ")
    },

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

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

    /** 加载广告 */
    loadAd: function (userIdKey, userDataKey) {
        var setting = {};
        setting[ATRewardedVideoJSSDK.userIdKey] = userIdKey;
        setting[ATRewardedVideoJSSDK.userDataKey] = userDataKey;
        ATRewardedVideoJSSDK.loadRewardedVideo(this.placementID(), setting);
    },

    /** 显示广告 */
    showAd: function () {
        ATRewardedVideoJSSDK.showAdInScenario(this.placementID(), "");
    },

    //广告回调部分------------------------------------------------------------------------
    /** 检测广告是否加载好 */
    checkReady: function () {
        ATJSSDK.printLog("AnyThinkRewardedVideo::checkReady()   " + (ATRewardedVideoJSSDK.hasAdReady(this.placementID()) ? "Ready" : "No"));
        if (this.proxy == null) return;
        return ATRewardedVideoJSSDK.hasAdReady(this.placementID()) ? true : false;
    },

    /** 
     * 激励视频加载完毕
     * @param {*} placementId 广告位id
     */
    onRewardedVideoAdLoaded: function (placementId) {
        ATJSSDK.printLog("AnyThinkRewardedVideo::onRewardedVideoAdLoaded(" + placementId + ")");
        if (this.proxy == null) return;
        this.proxy.onRewardedVideoAdLoaded(placementId);
    },

    /**
     * 激励视频加载失败
     * @param {*} placementId 广告位Id
     * @param {*} errorInfo   错误信息
     * @returns 
     */
    onRewardedVideoAdFailed: function (placementId, errorInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideo::onRewardedVideoAdFailed(" + placementId + ", " + errorInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onRewardedVideoAdFailed(placementId, errorInfo);
    },

    /**
     * 激励视频开始播放
     * @param {*} placementId 
     * @param {*} callbackInfo 
     * @returns 
     */
    onRewardedVideoAdPlayStart: function (placementId, callbackInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideo::onRewardedVideoAdPlayStart(" + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onRewardedVideoAdPlayStart(placementId, callbackInfo);
    },

    /**
     * 激励视频播放结束
     * @param {*} placementId   广告位Id
     * @param {*} callbackInfo  回调返回信息
     * @returns 
     */
    onRewardedVideoAdPlayEnd: function (placementId, callbackInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideo::onRewardedVideoAdPlayEnd(" + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onRewardedVideoAdPlayEnd(placementId, callbackInfo);
    },

    /**
     * 激励视频播放失败
     * @param {*} placementId  广告位ID 
     * @param {*} errorInfo    错误信息
     * @param {*} callbackInfo 回调返回信息
     * @returns 
     */
    onRewardedVideoAdPlayFailed: function (placementId, errorInfo, callbackInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideo::onRewardedVideoAdPlayFailed(" + placementId + ", " + errorInfo + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onRewardedVideoAdPlayFailed(placementId, errorInfo, callbackInfo);
    },

    /**
     * 激励视频被关闭
     * @param {*} placementId  广告位ID 
     * @param {*} callbackInfo 回调返回信息
     * @returns 
     */
    onRewardedVideoAdClosed: function (placementId, callbackInfo) {
        ATJSSDK.printLog("AnyThinkRewardedVideo::onRewardedVideoAdClosed(" + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onRewardedVideoAdClosed(placementId, callbackInfo);
    },

    /**
     * 激励视频被点击
     * @param {*} placementId  广告位ID
     * @param {*} callbackInfo 回调返回信息
     * @returns 
     */
    onRewardedVideoAdPlayClicked: function (placementId, callbackInfo) {
        //ATJSSDK.printLog("AnyThinkRewardedVideo::onRewardedVideoAdPlayClicked(" + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onRewardedVideoAdPlayClicked(placementId, callbackInfo);
    },

    /**
     * 激励视频获得奖励
     * @param {*} placementId  广告位ID
     * @param {*} callbackInfo 回调返回信息
     * @returns 
     */
    onReward: function (placementId, callbackInfo) {
        //ATJSSDK.printLog("AnyThinkRewardedVideo::onReward(" + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onReward(placementId, callbackInfo);
    }
});

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