import { Main } from "../Main";
import { WXSDK } from "./WXSDK";
import { AccountManager } from "./AccountManager";

//import { HPApi } from "./HPApi";



export default class GuangGao 
{
    public _bannerAd:any;
    private static ins:GuangGao;
    private static fsaasfafssfa;

    private static asgagassga;

    private static readonly videlAdUnitId = "adunit-40ac03a5538e8de0";
    private static bannerAdUnitId = "adunit-dc3e1cfa3ac69be3";

    private static afsfsaafsfsa: boolean = false;
    //视屏广告是否加载成功
    private static asfasfsaffsa: boolean = false;
    private static afsfasfasasf: number = 0;
   constructor(){
       GuangGao.ins = this;
   }
   public static get Instance():GuangGao{
       if(GuangGao.ins){
           return GuangGao.ins;
       }
       else{
           return new GuangGao();
       }
   }
    static InitId()
    {
       // this.bannerAdUnitId = AccountManager.Instance.curplayerData.uintid;
        //console.log(this.bannerAdUnitId);
    }

    //预先加载视屏广告
    public static JiaZaiShiPin()
    {
        if (this.afsfsaafsfsa) return;
        if (!Laya.Browser.onWeiXin) return;

        //GuangGao.InitId();
        //vedio
        this.fsaasfafssfa = Laya.Browser.window.wx.createRewardedVideoAd({ adUnitId: this.videlAdUnitId });

        this.fsaasfafssfa.onLoad(() => 
        {
            console.log('激励视频 广告加载成功');
            this.asfasfsaffsa = true;
        });

        this.fsaasfafssfa.onError(err => {
            console.log("激励视屏加载失败:" + err);
            this.asfasfsaffsa = false;
        });

        this.afsfasfasasf = 0;
        this.afsfsaafsfsa = true;
    }

    //看视频
    // public static KaiShiPin(callback: Function) 
    // {
    //     if (Laya.Browser.onWeiXin) 
    //     {
    //         console.log("showVideoAd , asfasfsaffsa:" + this.asfasfsaffsa);

    //         if (AccountManager.Instance.curplayerData.videoshare && AccountManager.Instance.curplayerData.fenxiang) 
    //         {
    //            // WXSDK.fengxiangtupian("看视频",null,"看视频");
    //         }

    //         if(AccountManager.Instance.curplayerData.video == false)
    //         {
    //             WXSDK.ShowTxt("暂未开放");
    //             return;
    //         }

    //         //HPApi.statistics1("video");

    //         this.fassfafasafs(callback);
            
    //         this.fsaasfafssfa.show().catch(err => 
    //             {
    //                 this.fsaasfafssfa.load().then(() => 
    //                 {
    //                     this.fassfafasafs(callback);
    //                     this.fsaasfafssfa.show();
    //                 });
    //             });
    //     } 
    //     else
    //     {
    //         callback(false);
    //     }
    // }

    private static fassfafasafs(callback:Function)
    {
        this.fsaasfafssfa.offClose();
        this.fsaasfafssfa.onClose(res => 
        {

            console.log("激励视频关闭:"+res.isEnded);
            if (res && res.isEnded || res === undefined) {callback && callback(true)}
            else { callback && callback(false) }
            this.fsaasfafssfa.offClose();
        });
    }
   
   public  showBanner(success: Function = null, fail: Function = null) {
        if (!Laya.Browser.onWeiXin) return;
        let ad = GuangGao.bannerAdUnitId;
        let bannerAd = Laya.Browser.window.wx.createBannerAd({
            adUnitId: ad,
            style: {
                left: 0,
                top: 0,
                width: 460
            }
        });
        bannerAd.onLoad(() => {
            console.log("[LOG] Banner广告加载成功");
            this.hideBanner();
            this._bannerAd = bannerAd;
            let info = Laya.Browser.window.wx.getSystemInfoSync();
            this._bannerAd.style.width = info.windowWidth;
            this._bannerAd.style.top = info.windowHeight - this._bannerAd.style.realHeight-20;
            this._bannerAd.show();
            success && success(this._bannerAd);
        });
        bannerAd.onError(err => {
            console.log("[LOG] Banner广告加载失败");
            console.log(err);
            fail && fail();
        });
    }
    hideBanner(){
        if(!Laya.Browser.onWeiXin) return
        if(this._bannerAd){
            this._bannerAd.hide();
            this._bannerAd.destroy();
            this._bannerAd = null;
        }
    }

    //打开banner
    public static OpenBanner(gap: number = 0,callVack: Function = null, iswait: boolean = false) 
    {
        if (!Laya.Browser.onWeiXin) return;
        if (this.afsfasfasasf >= 10000) 
        {
            var hh = this.gasagsgasgsa(gap);

            if (iswait) 
            {
                this.asgagassga.hide();
                Laya.timer.once(AccountManager.Instance.curplayerData.wudaoshijian * 1000, this, () => 
                {
                    if (callVack != null) 
                    {
                        callVack(hh);
                    }
                    this.asgagassga.show();
                })
            }
            else
            {
                if (callVack != null) 
                {
                    callVack(hh);
                }
                this.asgagassga.show();
            }
        } 
        else 
        {
            //var idd = this.bannerAdUnitId[GameUtils.getRandomInt(0,this.bannerAdUnitId.length - 1)];
            var idd = this.bannerAdUnitId;
            console.log("adUnitId:"+idd);
            let newBanner = Laya.Browser.window.wx.createBannerAd({
                adUnitId: idd,
                style: 
                {
                    left: 0,
                    top: 0,
                    width: 460
                }
            });
            newBanner.onError(err => {
                console.log("没有拉到banner--");
                console.log(err);
                    newBanner = Laya.Browser.window.wx.createBannerAd({
                    adUnitId: idd,
                    style: 
                    {
                        left: 0,
                        top: 0,
                        width: 460
                    }
                });

                this.gasagsgasgsa(gap);
            });
            newBanner.onLoad(() => 
            {

                this.afsfasfasasf++;
                if (this.asgagassga) this.asgagassga.destroy();
                this.asgagassga = newBanner;
                var hight: number = this.gasagsgasgsa(gap);
                if(iswait)
                {
                    this.asgagassga.hide();
                    Laya.timer.once(AccountManager.Instance.curplayerData.wudaoshijian * 1000, this, () => 
                    {
                        if(callVack != null)
                        {
                            callVack(hight);
                        }
                        this.asgagassga.show();
                    })
                } 
                else 
                {
                    if (callVack != null) 
                    {
                        callVack(hight);
                    }
                    this.asgagassga.show();
                }
            });
        }
    }
    public static gasagsgasgsa(gap) 
    {
        if (this.asgagassga) 
        {
            let info = Laya.Browser.window.wx.getSystemInfoSync();
            this.asgagassga.style.width = info.windowWidth;
            this.asgagassga.style.top = info.windowHeight - this.asgagassga.style.realHeight - gap-20;
            return Laya.stage.height / info.windowHeight * this.asgagassga.style.realHeight;
        }
    }

    //clear
    static Clear()
    {
        Laya.timer.clearAll(this);
        if (!Laya.Browser.onWeiXin) return;
        if (this.asgagassga) this.asgagassga.hide();
    }

    public static agagsagags() 
    {
        if (!Laya.Browser.onWeiXin) return;
        if (this.asgagassga) this.asgagassga.hide();
    }
    public static gasagsasg() 
    {
        if (!Laya.Browser.onWeiXin) return;
        if (this.asgagassga) this.asgagassga.show();
    }

    public static get keyiwudao():boolean
	{
		var wudao = AccountManager.Instance.curplayerData.Cheat;
		if(wudao == false)
		{
			console.log("cheat:"+wudao);
			return false;
		}
		if(Main.SceneNum == "1037" || Main.SceneNum == "1058" 
        || Main.SceneNum == "1067" || Main.SceneNum == "1095" 
		)
		{
			console.log("cheat:true-> "+Main.SceneNum);
			return true;
		}
		else
		{
			var checkscence = AccountManager.Instance.curplayerData.CheckScene;
			if(checkscence == false)
			{
				console.log("cheat:true->checkscence: "+checkscence);
				return true;
			}
		}
		console.log("cheat:false-> "+Main.SceneNum);
		return false;
	}

    //按钮误导
    static WudaoAnNiu(btn:Laya.UIComponent,ShowBanner = true,complete:Function = null,gap = 0)
    {
        if(AccountManager.Instance.curplayerData.Banner)
        {
            GuangGao.agagsagags();

            if(GuangGao.keyiwudao == false)
            {
                btn.bottom=350;
                GuangGao.OpenBanner();
            }
            else
            {
                if(ShowBanner)
                {
                    btn.bottom=100;
                    btn.mouseEnabled = false;
                    console.log("bottom:100");

                    GuangGao.OpenBanner(gap,(height) => 
                    {
                        if(height == null)
                        {
                            console.log("展示互推位");
                            // HPApi.ShowBannerImag((hh)=>
                            // {
                                var end=Laya.stage.height-height-btn.height;
                                btn.y = end;
                                btn.bottom = NaN;
                            // });
                        }
                        else
                        {
                            var end=Laya.stage.height-height-btn.height;
                            btn.y = end-100;
                            btn.bottom = NaN;
                            console.log("y:" + end);
                        }
                    },true);

                    Laya.timer.once(AccountManager.Instance.curplayerData.wudaoshijian*1000,this,()=>
                    {
                        btn.mouseEnabled = true;
                        
                        if(complete != null)
                        {
                            complete();
                        }
                    })
                }
                else
                {
                    if(complete != null)
                    {
                        complete();
                    }
                }
            }

            
        }
    }
    private _rewardedVideoAd;
    private _gameBanner;
    private _onVideoClose: Function;
    private _onLoadVideo: Function;

    private isVideoLoadCom = false;
   public  loadVideo(cb?: Function) {
        if (!Laya.Browser.onWeiXin) { if (cb) cb(true); return };

        this._onLoadVideo = cb;
        if (!this._rewardedVideoAd) {
            let index = GuangGao.videlAdUnitId;
            this._rewardedVideoAd = Laya.Browser.window.wx.createRewardedVideoAd({ adUnitId: index });

            var isc = false;
            this._rewardedVideoAd.onLoad(() => {
                console.log("[LOG] 激励视频广告加载成功");
                this.isVideoLoadCom = true;
                if (isc) return;
                isc = true;
                this._onLoadVideo && this._onLoadVideo(true);
                this._onLoadVideo = null;
            });
            this._rewardedVideoAd.onError(err => {
                console.log("[LOG] 激励视频广告加载失败");
                console.log(err);
                this.isVideoLoadCom = false;
                this._onLoadVideo && this._onLoadVideo(false);
                this._onLoadVideo = null;
            });
            this._rewardedVideoAd.onClose(res => {
                if (!res || res.isEnded) {
                    this._onVideoClose && this._onVideoClose(true);
                    this._onVideoClose = null;
                }
                else {
                    this._onVideoClose && this._onVideoClose(false);
                    this._onVideoClose = null;
                }
            });
        }
        else {
            if (this.isVideoLoadCom) {
                this._onLoadVideo && this._onLoadVideo(true);
                this._onLoadVideo = null;
            }else{
                this._onLoadVideo && this._onLoadVideo(false);
                this._onLoadVideo = null;
            }
        }
    }

    showVideo(cb?: Function) {
        if (!Laya.Browser.onWeiXin) { if (cb) cb(true); return };
        this._onVideoClose = cb;
        this._rewardedVideoAd.show().catch(err => {
            this._rewardedVideoAd.load().then(() => {
                this._rewardedVideoAd.show();
            });
        });
    }
}