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

var ATNativeJS = cc.Class({

    name: "ATNativeJS",

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

    frameWidth: 0,
    frameHeight: 0,

    nativeIsShow: false,
    nativeType: 0,
    nativeShow: false,
    nativeIsLoad: false,
    nativeIsLoading: false,
    // timeID: 0,

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

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

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

    /**
     * 加载原生广告
     * @param {*} type 类型 
     * @param {*} show 是否显示
     */
    loadNative: function (type = 0, show = false) {

        this.nativeType = type;
        this.nativeShow = show;
        this.nativeIsLoading = true;

        //加载原生广告时需要传入广告展示的宽高
        var frameSize = cc.view.getFrameSize();
        this.frameWidth = frameSize.width;
        this.frameHeight = frameSize.height;
        var per = this.frameWidth / 750
        var sw = per * 656
        var sh = per * 440

        if (this.nativeType == 2) {
            this.destroyNative(false)
            sw *= 0.9
            sh *= 0.8
        }
        else if (this.nativeType == 3) {
            this.destroyNative(false)
            sw *= 0.9
            sh *= 0.75
        }
        else {
            sw *= 0.9
            sh *= 0.75
        }

        ATNativeJSSDK.loadNative(this.placementID(), ATNativeJSSDK.createLoadAdSize(sw, sh));
    },

    /**
     * 销毁原生广告
     * @param {*} ifPreloadNext 是否预加载下一个 
     */
    destroyNative: function (ifPreloadNext = true) {
        ATNativeJSSDK.removeAd(this.placementID());
        this.nativeIsLoad = false
        this.nativeIsShow = false
        if (ifPreloadNext) {
            this.loadNative()
        }
    },

    /** 信息流位置 
     *  @param {*} type 0 弹窗里  1 最底部 2 新车奖励 3 底部小窗口
     */
    showNative: function (type) {

        if (this.nativeIsLoading) {
            this.nativeType = type
            this.nativeShow = true
            return
        }

        if (!this.nativeIsLoad) {
            this.loadNative(type, true)
            return
        }

        if (this.nativeIsShow) {
            return
        }

        this.nativeIsShow = true
        var per = this.frameWidth / 750
        var sw = per * 656
        var sh = per * 440
        var fontSize = 12

        var nativeAdViewProperty = new ATNativeJSSDK.AdViewProperty();
        var posy = 0
        if (type == 0) {
            // posy = this.frameHeight * 0.5 - 100
            posy = this.frameHeight * 0.45
        }
        else if (type == 1) {
            // posy = this.frameHeight * 0.5 + 280
            posy = this.frameHeight * 0.625
        }
        else if (type == 2) {
            // posy = this.frameHeight * 0.5 + 210
            posy = this.frameHeight * 0.595
            sw *= 0.9
            sh *= 0.8
            fontSize = 8
        }
        else if (type == 3) {
            // posy = this.frameHeight * 0.5 + 420
            posy = this.frameHeight * 0.69
            sw *= 0.9
            sh *= 0.75
            fontSize = 8
        }
        else if (type == 4) {
            posy = this.frameHeight * 0.75;
            sw *= 1.1;
        }

        var parx = (this.frameWidth - sw) * 0.5

        nativeAdViewProperty.parent = nativeAdViewProperty.createItemViewProperty(parx, posy, sw, sh, "#ffffff", "", 0);
        nativeAdViewProperty.mainImage = nativeAdViewProperty.createItemViewProperty(0, 0, sw, sh, "#ffffff", "#ffffff", fontSize);
        ATNativeJSSDK.showAd(this.placementID(), nativeAdViewProperty);
    },

    //广告回调部分------------------------------------------------------------------------
    /**
     * 原生广告加载成功
     * @param {*} placementId 
     */
    onNativeAdLoaded: function (placementId) {
        this.nativeIsLoad = true
        this.nativeIsLoading = false
        if (this.nativeShow) {
            this.nativeIsShow = false
            this.showNative(this.nativeType)
        }
    },

    /**
     * 原生广告加载失败
     * @param {*} placementId 广告位id
     * @param {*} errorInfo   错误信息
     */
    onNativeAdLoadFail: function (placementId, errorInfo) {
        // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdLoadFail(" + placementId + ", " + errorInfo + ")");
        this.nativeIsLoading = false
        this.nativeIsLoad = false
    },

    /**
     * 原生广告显示成功
     * @param {*} placementId  广告位id
     * @param {*} callbackInfo 回调信息
     * @returns 
     */
    onNativeAdShow: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdShow(" + placementId + ", " + callbackInfo + ")");
        if (this.proxy == null) return;
        this.proxy.onNativeAdShow(placementId, callbackInfo);
    },

    /**
     * 原生广告被点击
     * @param {*} placementId  广告位Id
     * @param {*} callbackInfo 回调信息
     */
    onNativeAdClick: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdClick(" + placementId + ", " + callbackInfo + ")");
    },

    /**
     * 原生广告视频开始播放，部分广告平台有此回调
     * @param {*} placementId 广告位id
     */
    onNativeAdVideoStart: function (placementId) {
        // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdVideoStart(" + placementId + ")");
    },

    /**
     * 原生广告视频结束播放，部分广告平台有此回调
     * @param {*} placementId 
     */
    onNativeAdVideoEnd: function (placementId) {
        // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdVideoEnd(" + placementId + ")");
    },

    /**
     * 广告关闭按钮被点击，部分广告平台有此回调
     * @param {*} placementId 
     * @param {*} callbackInfo 
     */
    onNativeAdCloseButtonTapped: function (placementId, callbackInfo) {
        // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdCloseButtonTapped(" + placementId + ", " + callbackInfo + ")");
    }
});

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