
import WxApi from "./wxsdk";
import { Main } from "../Main";
import { PlayerAccount } from "../Scripts/Network/PlayerAccount";



export default class ADPlayer 
{
    private static rewardedVideoAd;

    private static bannerAd;

    private static videlAdUnitId = "adunit-99203ac49ea44bd0";
    private static bannerAdUnitId = "adunit-398817d584c5941d";

    private static isInit: boolean = false;
    //视屏广告是否加载成功
    private static videoAdIsload: boolean = false;
    private static bannerCreateTimes: number = 0;

    //预先加载视屏广告
    public static initAd()
    {
        if (this.isInit) return;
        if (!Main.OnWx) return;
        //vedio
        this.rewardedVideoAd = Laya.Browser.window.wx.createRewardedVideoAd({ adUnitId: this.videlAdUnitId });

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

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

        this.bannerCreateTimes = 0;
        this.isInit = true;
    }

    public static showVideoAd(callback: Function) 
    {
        if (Main.OnWx) 
        {
            console.log("showVideoAd , videoAdIsload:" + this.videoAdIsload);

            if (PlayerAccount.CurrentAccount.CurAccountData.share) 
            {
                WxApi.shareImage("看视频",null,"看视频");
            }

            if(PlayerAccount.CurrentAccount.CurAccountData.video == false)
            {
                return;
            }

            //HPApi.statistics("video");

            this.RegVideo(callback);
            
            this.rewardedVideoAd.show().catch(err => 
                {
                    this.rewardedVideoAd.load().then(() => 
                    {
                        this.RegVideo(callback);
                        this.rewardedVideoAd.show();
                    });
                });
        } 
        else
        {
            callback(false);
        }
    }

    private static RegVideo(callback:Function)
    {
        this.rewardedVideoAd.offClose();
        this.rewardedVideoAd.onClose(res => 
        {
            console.log("激励视频关闭:"+res.isEnded);
            console.log(callback);
            if (res && res.isEnded || res === undefined) {callback && callback(true)}
            else { callback && callback(false) }
            this.rewardedVideoAd.offClose();
        });
    }

    public static showBannerAd(gap: number = 0,callVack: Function = null, iswait: boolean = false) 
    {
        if (!Main.OnWx) return;
        if (this.bannerCreateTimes >= 10000) 
        {
            var hh = this.ResetBannerHight(gap);

            if (iswait) 
            {
                this.bannerAd.hide();
                Laya.timer.once(PlayerAccount.CurrentAccount.CurAccountData.cheattime * 1000, this, () => 
                {
                    if (callVack != null) 
                    {
                        callVack(hh);
                    }
                    this.bannerAd.show();
                })
            }
            else
            {
                if (callVack != null) 
                {
                    callVack(hh);
                }
                this.bannerAd.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(err);
                if (callVack != null) 
                {
                    callVack(null);
                }
                this.ResetBannerHight(gap);
            });
            newBanner.onLoad(() => 
            {
                
                this.bannerCreateTimes++;
                if (this.bannerAd) this.bannerAd.destroy();
                this.bannerAd = newBanner;
                var hight: number = this.ResetBannerHight(gap);

                if(iswait)
                {
                    this.bannerAd.hide();
                    Laya.timer.once(PlayerAccount.CurrentAccount.CurAccountData.cheattime * 1000, this, () => 
                    {
                        if(callVack != null)
                        {
                            callVack(hight);
                        }
                        this.bannerAd.show();
                    })
                } 
                else 
                {
                    if (callVack != null) 
                    {
                        callVack(hight);
                    }
                    this.bannerAd.show();
                }
            });
        }
    }

    public static ResetBannerHight(gap) 
    {
        if (this.bannerAd) 
        {
            let info = Laya.Browser.window.wx.getSystemInfoSync();
            this.bannerAd.style.width = info.windowWidth;

            this.bannerAd.style.top = info.windowHeight - this.bannerAd.style.realHeight - gap;
            return Laya.stage.height / info.windowHeight * this.bannerAd.style.realHeight;
        }
    }

    static ClearBanner()
    {
        Laya.timer.clearAll(this);
        if (!Main.OnWx) return;
        if (this.bannerAd) this.bannerAd.hide();
        //if(HPApi.bannerImag && HPApi.bannerImag.visible)HPApi.bannerImag.visible = false;
    }

    public static hiddenBannerAd() 
    {
        if (!Main.OnWx) return;
        if (this.bannerAd) this.bannerAd.hide();
    }
    public static BannerAdEnable() 
    {
        if (!Main.OnWx) return;
        if (this.bannerAd) this.bannerAd.show();
    }

    public static get CanCheat():boolean
	{
		var cheat = PlayerAccount.CurrentAccount.CurAccountData.cheat;
		if(cheat == false)
		{
			console.log("cheat:"+cheat);
			return false;
		}
		if(Main.sceneValue == "1037" || Main.sceneValue == "1058" 
        || Main.sceneValue == "1067" || Main.sceneValue == "1095"  
		)
		{
			console.log("cheat:true-> "+Main.sceneValue);
			return true;
		}
		else
		{
			var checkscence = PlayerAccount.CurrentAccount.CurAccountData.checkscence;
			if(checkscence == false)
			{
				console.log("cheat:true->checkscence: "+checkscence);
				return true;
			}
		}
		console.log("cheat:false-> "+Main.sceneValue);
		return false;
	}

    static MoveButton(btn:Laya.UIComponent,ShowBanner = true,complete:Function = null,gap = 0)
    {
        if(PlayerAccount.CurrentAccount.CurAccountData.banner)
        {
            ADPlayer.hiddenBannerAd();

            if(ADPlayer.CanCheat == false)
            {
                btn.bottom=350;
                ADPlayer.showBannerAd();
            }
            else
            {
                if(ShowBanner)
                {
                    btn.bottom=100;

                    btn.mouseEnabled = false;

                    ADPlayer.showBannerAd(gap,(height) => 
                    {
                        var end = Laya.stage.height-height-btn.height;
                        btn.y = end - 40;
                    },true);

                    Laya.timer.once(PlayerAccount.CurrentAccount.CurAccountData.cheattime*1000,this,()=>
                    {
                        btn.mouseEnabled = true;
                        
                        if(complete != null)
                        {
                            complete();
                        }
                    })
                }
                else
                {
                    if(complete != null)
                    {
                        complete();
                    }
                }
            }

            
        }
    }
}