
var md5 = require("md5");
var mta = require("mta");
var sdk_conf = require("sdk_conf");
var sdk = require("sdk");

var adSdk = {


    bannerAd: null,
    bannerAd1: null,
    bannerAd2: null,
    bannerAd3: null,

    isOnce:true,
    videoAd: null,
    videoAd1: null,
    videoAd2: null,
    videoAd3: null,

    //视频看完回调 
    videoSuccess: null,
    //视频未看完回调 
    videoFail: null,
    //视频调起失败回调
    videoError: null,
    
     /**
     * @param {int} index 视频id的数字编号 1 2 3 
     * @param {string} videoAdUnitId
     * 
     *adSdk.videoSuccess = function(){
     *      //视频看完处理逻辑
     *      this.successFunction();
     * };
     * adSdk.videoFail = function(){
     *      //视频未看完处理逻辑
     *      this.failFunction();`
     * };
     * adSdk.videoError = function(){
     *      //视频调起失败处理逻辑
     *      this.errorFunction();`
     * };
     * var videoAd = adSdk.createVideoAd(1,videoAdUnitId1);
     * videoAd.load().then(() => videoAd.show());
     * //如果多个视频广告id（videoAdUnitId）的情况，通过后台技术配置Config2获取对应的视频广告id，比如：sdk.getConfig2().videoAdUnitId1 或者sdk.getConfig2().videoAdUnitId2
     */
    createVideoAd(index, videoAdUnitId) {
        if (index == 1) {
            if (this.videoAd1) {
                return this.videoAd1;
            }
        } else if (index == 2) {
            if (this.videoAd2) {
                return this.videoAd2;
            }

        } else if (index == 3) {
            if (this.videoAd3) {
                return this.videoAd3;
            }
        }
        else {
            if (this.videoAd) {
                return this.videoAd;
            }
        }
        return adSdk.createRewardedVideoAd(index, videoAdUnitId);
    },



     /**
     * 
     * @param {json} obj 
     * @param {string} bannerAdUnitId 
     * 多个banner广告id调用如下：（注意第一次调起不需要去做销毁destory）
     * 
     *       adSdk.createBannerAdByAdId({},bannerAdUnitId1).destroy();
     *       adSdk.bannerAd =null;
     *       var bannerAd =adSdk.createBannerAdByAdId({},bannerAdUnitId2);
     *       bannerAd.show(); 
     * //如果多个banner广告id（bannerAdUnitId）的情况，通过后台技术配置Config2获取对应的banner广告id，比如：sdk.getConfig2().bannerAdUnitId1 或者sdk.getConfig2().bannerAdUnitId2
     */

    createBannerAdByAdId(obj,bannerAdUnitId){
        var self = this;
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            if(this.bannerAd){
                return this.bannerAd;
            }else{
                if(!obj.style){
                    obj.style = {};
                    var phone = wx.getSystemInfoSync();
                    this.w = phone.screenWidth / 2;
                    this.h = phone.screenHeight;
                    obj.style.left = 0;
                    obj.style.top = 0;
                    obj.style.width = 300;
                }
                this.bannerAd = wx.createBannerAd({
                    adUnitId: bannerAdUnitId,
                    style: obj.style,
                })
                this.bannerAd.onResize(function(res){
                    console.log("sdk BannerAd广告缩放事件：", res)
                    self.bannerAd.style.left = self.w - self.bannerAd.style.realWidth/2+0.1;
                    self.bannerAd.style.top = self.h - self.bannerAd.style.realHeight+0.1;
                });
                this.bannerAd.onLoad(function(res){
                    console.log("sdk BannerAd广告加载事件：", res)
                });
                this.bannerAd.onError(function(res){
                    console.log("sdk BannerAd广告错误事件：", res)
                });

                return this.bannerAd;
            }
        }
    },

    createBannerAdByAdId1(obj,bannerAdUnitId){
        var self = this;
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            if(this.bannerAd1){
                return this.bannerAd1;
            }else{
                if(!obj.style){
                    obj.style = {};
                    var phone = wx.getSystemInfoSync();
                    this.w = phone.screenWidth / 2;
                    this.h = phone.screenHeight;
                    obj.style.left = 0;
                    obj.style.top = 0;
                    obj.style.width = 300;
                }
                this.bannerAd1 = wx.createBannerAd({
                    adUnitId: bannerAdUnitId,
                    style: obj.style,
                })
                this.bannerAd1.onResize(function(res){
                    console.log("sdk BannerAd广告缩放事件：", res)
                    self.bannerAd1.style.left = self.w - self.bannerAd1.style.realWidth/2+0.1;
                    self.bannerAd1.style.top = self.h - self.bannerAd1.style.realHeight+0.1;
                });
                this.bannerAd1.onLoad(function(res){
                    console.log("sdk BannerAd广告加载事件：", res)
                });
                this.bannerAd1.onError(function(res){
                    console.log("sdk BannerAd广告错误事件：", res)
                });

                return this.bannerAd1;
            }
        }
    },
    createBannerAdByAdId2(obj,bannerAdUnitId){
        var self = this;
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            if(this.bannerAd2){
                return this.bannerAd2;
            }else{
                if(!obj.style){
                    obj.style = {};
                    var phone = wx.getSystemInfoSync();
                    this.w = phone.screenWidth / 2;
                    this.h = phone.screenHeight;
                    obj.style.left = 0;
                    obj.style.top = 0;
                    obj.style.width = 300;
                }
                this.bannerAd2 = wx.createBannerAd({
                    adUnitId: bannerAdUnitId,
                    style: obj.style,
                })
                this.bannerAd2.onResize(function(res){
                    console.log("sdk BannerAd广告缩放事件：", res)
                    self.bannerAd2.style.left = self.w - self.bannerAd2.style.realWidth/2+0.1;
                    self.bannerAd2.style.top = self.h - self.bannerAd2.style.realHeight+0.1;
                });
                this.bannerAd2.onLoad(function(res){
                    console.log("sdk BannerAd广告加载事件：", res)
                });
                this.bannerAd2.onError(function(res){
                    console.log("sdk BannerAd广告错误事件：", res)
                });

                return this.bannerAd2;
            }
        }
    },
    createBannerAdByAdId3(obj,bannerAdUnitId){
        var self = this;
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
            if(this.bannerAd3){
                return this.bannerAd3;
            }else{
                if(!obj.style){
                    obj.style = {};
                    var phone = wx.getSystemInfoSync();
                    this.w = phone.screenWidth / 2;
                    this.h = phone.screenHeight;
                    obj.style.left = 0;
                    obj.style.top = 0;
                    obj.style.width = 300;
                }
                this.bannerAd3 = wx.createBannerAd({
                    adUnitId: bannerAdUnitId,
                    style: obj.style,
                })
                this.bannerAd3.onResize(function(res){
                    console.log("sdk BannerAd广告缩放事件：", res)
                    self.bannerAd3.style.left = self.w - self.bannerAd3.style.realWidth/2+0.1;
                    self.bannerAd3.style.top = self.h - self.bannerAd3.style.realHeight+0.1;
                });
                this.bannerAd3.onLoad(function(res){
                    console.log("sdk BannerAd广告加载事件：", res)
                });
                this.bannerAd3.onError(function(res){
                    console.log("sdk BannerAd广告错误事件：", res)
                });

                return this.bannerAd3;
            }
        }
    },


    /**
     * 后期会慢慢弃用，因为该方法只适合一个banner广告的情况
     * @param {json} obj 
     * var bannerAd =adSdk.createBannerAd({},bannerAdUnitId);
     * bannerAd.show(); 
     * 
     */
    createBannerAd(obj){
       
        return sdk.createBannerAd(obj);
    },


    /**
     * 该方法不直接对外提供调用
     * @param {int} index 
     * @param {string} videoAdUnitId1 
     * 
     */
    createRewardedVideoAd(index, videoAdUnitId1) {
        let self = this;
        if (cc.sys.platform === cc.sys.WECHAT_GAME) {
           
            let videoAd = wx.createRewardedVideoAd({ adUnitId: videoAdUnitId1 })

            if(self.isOnce)
            {
                self.isOnce=false;
                videoAd.onLoad(function (res) {
                    console.log("sdk VideoAd广告加载事件：", res)
                });
    
                var closeFun1 = function (res) {
                    // 用户点击了【关闭广告】按钮
                    // 小于 2.1.0 的基础库版本，res 是一个 undefined
                    if (res && res.isEnded || res === undefined) {
                        console.log("sdk 看视频成功");
                        self.videoSuccess();
                    } else {
                        console.error("sdk 看视频失败");
                        self.videoFail();
                    }
                };
    
    
                videoAd.onClose(closeFun1);
    
                videoAd.onError(function (res) {
                    wx.showToast({
                        title: '今日视频已上限，明日再来！',
                        icon: 'none'
                    });
                    if (self.videoError) {
                        self.videoError();
                    }
                });
            }
            

            if (index == 1) {
                this.videoAd1 = videoAd;
            }
            else if (index == 2) {
                this.videoAd2 = videoAd;
            }
            else if (index == 3) {
                this.videoAd3 = videoAd;
            }
            else {
                this.videoAd = videoAd;
            }
            return videoAd;
        }
    }
	
};

window.adSdk = adSdk;
