import { GameTools } from "../GameUtils/GameTools";
import { AccountManager } from "./AccountManager";

export class WxGameItem
{
    appNameHidden:boolean = false;
    color:string = "C93B3BFF";
    size:number = 100;
    borderWidth:number = 50;
    borderColor:string = "A1A0A0FF";
    left:number;
    top:number;
}

export default class GuangGao 
{

    private static ins:GuangGao;
    private static fsaasfafssfa;

    private static curbanner;

    private static readonly videlAdUnitId = "adunit-8feb4e40536601ff";
    private static readonly bannerAdUnitId = "adunit-426c9d6299d18f7e";
    
    private static afsfsaafsfsa: boolean = false;
    //视屏广告是否加载成功
    private static asfasfsaffsa: boolean = false;

    constructor(){
        GuangGao.ins = this;
    }
    public static get Instance():GuangGao{
        if(GuangGao.ins){
            return GuangGao.ins;
        }
        else{
            return new GuangGao();
        }
    }
    //预先加载视屏广告
    public static InitVideo()
    {
        if (!Laya.Browser.onWeiXin) return;

        console.log(this.bannerAdUnitId);
        //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.afsfsaafsfsa = true;
    }

    //看视频
    public static ShowVideo(callback: Function) 
    {
            if(this.asfasfsaffsa)
            {
                console.log("showVideoAd:" + this.asfasfsaffsa);
                this.SetVideoCallBack(callback);
                this.fsaasfafssfa.show().catch(err => 
                {
                    this.fsaasfafssfa.load().then(() => 
                    {
                        this.SetVideoCallBack(callback);
                        this.fsaasfafssfa.show();
                    });
                });
            }
    }

    private static SetVideoCallBack(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();
        });
    }

    static canshowbanner = true;
    //打开banner
    public static OpenBanner(gap: number = 0,callVack: Function = null, iswait: boolean = false) 
    {
        if(!Laya.Browser.onWeiXin)
        {
            if(callVack) {
                callVack(null);
            }
            return;
        }

        this.canshowbanner = true;
        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("广告错误onerror--");
            console.log(err);
            callVack(null);
        });
        newBanner.onLoad(() => 
        {
            if (this.curbanner) this.curbanner.destroy();
            this.curbanner = newBanner;
            var hight: number = this.GetRealHight(gap);

            if (callVack != null) 
            {
                callVack(hight);
            }
            if(this.canshowbanner && !iswait)
            {
                this.curbanner.show();
            }
            else
            {
                this.curbanner.hide();
            }
        });
    }

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

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

    //clear
    static Clear()
    {
        Laya.timer.clearAll(this);
        Laya.Tween.clearAll(this);
        this.canshowbanner = false;
        if (this.curbanner) this.curbanner.hide();
    }

    public static hidebanner() 
    {
        if (!Laya.Browser.onWeiXin) return;
        if (this.curbanner) this.curbanner.hide();
    }
    public static showbanner() 
    {
        if (!Laya.Browser.onWeiXin) return;
        if (this.curbanner) this.curbanner.show();
    }

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

    static bannerhigt = 0;
    public static SetPosBanner(btn:Laya.UIComponent,complete:Function)
    {
        if(Laya.Browser.onPC)
        {
            complete(false);
            return;
        }

        let info = Laya.Browser.window.wx.getSystemInfoSync();
        var reshight = btn.y * info.windowHeight / Laya.stage.height;

        var idd = this.bannerAdUnitId;
        console.log("adUnitId:"+idd);
        if (this.curbanner) this.curbanner.destroy();
        let newBanner = Laya.Browser.window.wx.createBannerAd({
            adUnitId: idd,
            style: 
            {
                left: 0,
                top: 0,
                width: 720
            }
        });
        newBanner.onError(err => {
            console.log(err);
            complete(false);
        });
        
        newBanner.onLoad(() => 
        {
            this.curbanner = newBanner;
            
            this.curbanner.style.top = reshight;
            if(this.curbanner)
            {
                console.log("banner show");
                this.curbanner.show();
                Laya.timer.once(2300,this,()=>
                {
                    this.curbanner.style.top = info.windowHeight - GuangGao.bannerhigt;
                    complete(true);
                })
            }
        });
        newBanner.onResize((res)=>
        {
            GuangGao.bannerhigt = res.height;
            this.curbanner.style.width = info.windowWidth;
            this.curbanner.style.top = reshight;
        });
    }

    //按钮误导
    static CheatBanner(btn:Laya.UIComponent,complete:Function = null,gap = 0)
    {
        if(!AccountManager.Instance.curplayerData.Cheat)
        {
            console.log("ignorecheat");
            GuangGao.OpenBanner(0,(height) =>
            {
                if(height == null)
                {
                    console.log("banner高度为null---");
                    // YQSdk.ShowBannerImag();
                }
            });
        }
        else
        {
            btn.y = Laya.stage.height-btn.height - 100;
            GuangGao.OpenBanner(gap,(height) => 
            {
                if(height == null)
                {
                    // YQSdk.ShowBannerImag();
                }
                else
                {
                    console.log("cheatstart","bottom:100");
                    btn.mouseEnabled = false;
                    var end=Laya.stage.height-height-btn.height - 20;
                    console.log("y:" + end);
                    Laya.timer.once(1000,this,()=>
                    {
                        btn.mouseEnabled = true;
                        this.showbanner();
                        Laya.Tween.to(btn,{y:end},500,null,Laya.Handler.create(this,()=>
                        {
                            if(complete != null)
                            {
                                complete();
                            }
                        }))
                    })
                }
            },true);
        }
    }


    public _bannerAd:any;
    public  showBanner(success: Function = null, fail: Function = null) {
        if (!Laya.Browser.onWeiXin) return;
        var ran = GameTools.RandomANumber(0,3);
        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;
        }
    }
}