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

var ATBannerJS = cc.Class({

    name: "ATBannerJS",

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

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

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

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

    /**
     * 加载banner广告
     * @returns 
     */
    loadBanner: function () {
        let winHeight = cc.view.getFrameSize().height
        if (winHeight < 1400) {
            return
        }
        var setting = {}
        var bannerH = winHeight / 1334 * 115

        setting[ATBannerJSSDK.kATBannerAdLoadingExtraBannerAdSizeStruct] = ATBannerJSSDK.createLoadAdSize(cc.view.getFrameSize().width, bannerH);
        //只针对Admob的自适应banner
        setting[ATBannerJSSDK.kATBannerAdInlineAdaptiveWidth] = cc.view.getFrameSize().width;
        setting[ATBannerJSSDK.kATBannerAdInlineAdaptiveOrientation] = ATBannerJSSDK.kATBannerAdInlineAdaptiveOrientationCurrent;
        // setting[ATBannerJSSDK.kATBannerAdInlineAdaptiveOrientation] =      ATBannerJSSDK.kATBannerAdInlineAdaptiveOrientationPortrait;
        // setting[ATBannerJSSDK.kATBannerAdInlineAdaptiveOrientation] = ATBannerJSSDK.kATBannerAdInlineAdaptiveOrientationLandscape;
        ATBannerJSSDK.loadBanner(this.placementID(), setting);
    },

    /**
     * 检测banner是否准备就绪
     * @returns 
     */
    checkBannerReady: function () {
        return ATBannerJSSDK.hasAdReady(this.placementID()) ? true : false
    },

    /**
     * 显示banner
     * @returns 
     */
    showBanner: function () {
        if (this.bannerIsShow) {
            return
        }
        ATBannerJSSDK.showAdInPosition(this.placementID(), ATBannerJSSDK.kATBannerAdShowingPositionBottom);
    },

    /**
     * 销毁banner
     */
    destroyBanner: function () {
        if (this.bannerIsShow) {
            ATBannerJSSDK.removeAd(this.placementID());
            this.bannerIsShow = false
            this.loadBanner()
        }
    },

    /**
     * banner加载成功
     * @param {*} placementId 广告位id
     * @returns 
     */
    onBannerAdLoaded: function (placementId) {
        // ATJSSDK.printLog("AnyThinkBannerDemo::onBannerAdLoaded(" + placementId + ")");
        if (this.proxy == null) return;
        this.proxy.onBannerAdLoaded(placementId);
    },

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

    /**
     * banner展示成功
     * @param {*} placementId  广告位id
     * @param {*} callbackInfo 回调信息
     * @returns 
     */
    onBannerAdShow: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkBannerDemo::onBannerAdShow(" + placementId + ", " + callbackInfo + ")");
        this.bannerIsShow = true
        if (this.proxy == null) return;
        this.proxy.onBannerAdShow(placementId, callbackInfo);
    },

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

    /** banner自动刷新成功 */
    onBannerAdAutoRefresh: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkBannerDemo::onBannerAdAutoRefresh(" + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
    },

    /**
     * banner自动刷新失败
     * @param {*} placementId 广告位id
     * @param {*} errorInfo   错误信息
     * @returns 
     */
    onBannerAdAutoRefreshFail: function (placementId, errorInfo) {
        // ATJSSDK.printLog("AnyThinkBannerDemo::onBannerAdAutoRefreshFail(" + placementId + ", " + errorInfo + ")");
        if (this.proxy == null) return;
    },

    /**
     * banner关闭按钮被点击
     * @param {*} placementId  广告位id
     * @param {*} callbackInfo 错误信息
     * @returns 
     */
    onBannerAdCloseButtonTapped: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkBannerDemo::onBannerAdCloseButtonTapped(" + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
    }
});

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

