import UserModel from "./UserModel";
import { UserInfoManager } from "../Game/UserInfoManager";
import {GameVersion} from "./GameVersion";
import { FYHD_Sdk } from "./FYHD_Sdk";


export  class GuangGao 
{
    private static ins:GuangGao;
    private static fsaasfafssfa;

    private static curbanner;

    private static readonly videlAdUnitId = "adunit-0fb53ab4969d46bb";
    private static bannerAdUnitId = "adunit-de3afc5da6054394";
    private static bannerAdUnitId2 = "adunit-72492b628fc3dee9";
    private static bannerAdUnitId3 = "adunit-a0e0b5dbf2b6c840";


    private static afsfsaafsfsa: boolean = false;
    //视屏广告是否加载成功
    private static asfasfsaffsa: boolean = false;

    constructor(){
        GuangGao.ins = this;
    }
    public  static get Instance():GuangGao{
        if(GuangGao.ins == null){
            return new GuangGao();
        }
        return GuangGao.ins;
    }


    //预先加载视屏广告
    public static InitVideo()
    {
        if (!Laya.Browser.onWeiXin ) return;
        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();
                    });
                });
            }
            else
            {
            }
    }

    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)
        {
            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(err);
            callVack && 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(!UserInfoManager.Cheat)
        {
            console.log("cheat:false-> CurAccountData.cheat != 1");
            return false;
        }
		var checkscence = UserInfoManager.CheckScene;
        if (checkscence == false)
        {
            console.log("cheat:true-> " + GameVersion.wxValue);
            return true;
        }
        if (GameVersion.wxValue == "1037" || GameVersion.wxValue == "1058"|| GameVersion.wxValue == "1067" || GameVersion.wxValue == "1095") 
        {
            console.log("cheat:true-> " + GameVersion.wxValue);
            return true;
        }
        console.log("cheat:false-> " + GameVersion.wxValue);
        return false;
    }


    static bannerhigt = 0;
    
    //按钮误导
    // static CheatBanner(btn:Laya.UIComponent,complete:Function = null,gap = 0)
    // {
    //     if(!FYHD_Sdk.iswhite)
    //     {
    //         console.log("ignorecheat");
    //         GuangGao.OpenBanner(0,(height) =>
    //         {
    //             if(height == null)
    //             {
                    
    //             }
    //         });
    //     }
    //     else
    //     {
    //         // btn.y = Laya.stage.height-btn.height - 100;
    //         GuangGao.OpenBanner(gap,(height) => 
    //         {
    //             if(height == null)
    //             {
                    
    //             }
    //             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(UserInfoManager.CheatTime*1000,this,()=>
    //                 {
    //                     btn.mouseEnabled = true;
    //                     this.showbanner();
    //                     Laya.Tween.to(btn,{y:end},500,null,Laya.Handler.create(this,()=>
    //                     {
    //                         if(complete != null)
    //                         {
    //                             complete();
    //                         }
    //                     }))
    //                 })
    //             }
    //         },true);
    //     }
    // }




    _bannerAd:any;
    _bannerAd2:any;
    _bannerAd3:any;

   public  showBanner(success: Function = null, fail: Function = null,gap:number = 0) {
        if (!Laya.Browser.onWeiXin){
            success && success(null)
            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-10;
            this._bannerAd.show();
            var hight: number = GuangGao.GetRealHight2(this._bannerAd,gap);
            let data = {};
            data["banner"] = this._bannerAd;
            data["gap"] = hight;
            success && success(data);
        });
        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;
        }
    }


    public  showBanner2(success: Function = null, fail: Function = null,gap:number = 0) {
        if (!Laya.Browser.onWeiXin){
            success && success(null)
            return;
        } 
        let ad = GuangGao.bannerAdUnitId2;
        let bannerAd = Laya.Browser.window.wx.createBannerAd({
            adUnitId: ad,
            style: {
                left: 0,
                top: 0,
                width: 460
            }
        });
        bannerAd.onLoad(() => {
            console.log("[LOG] Banner广告加载成功");
            this.hideBanner2();
            this._bannerAd2 = bannerAd;
            let info = Laya.Browser.window.wx.getSystemInfoSync();
            this._bannerAd2.style.width = info.windowWidth;
            this._bannerAd2.style.top = info.windowHeight - this._bannerAd2.style.realHeight-10;
            this._bannerAd2.show();
            var hight: number = GuangGao.GetRealHight2(this._bannerAd2,gap);
            let data = {};
            data["banner"] = this._bannerAd2;
            data["gap"] = hight;
            console.log("data>>>>>",data);
            success && success(data);
        });
        bannerAd.onError(err => {
            console.log("[LOG] Banner广告加载失败");
            console.log(err);
            fail && fail();
        });
    }
    hideBanner2(){
        if(!Laya.Browser.onWeiXin) return
        if(this._bannerAd2){
            this._bannerAd2.hide();
            this._bannerAd2.destroy();
            this._bannerAd2 = null;
        }
    }


    public  showBanner3(success: Function = null, fail: Function = null,gap:number = 0) {
        if (!Laya.Browser.onWeiXin){
            success && success(null)
            return;
        } 
        let ad = GuangGao.bannerAdUnitId3;
        let bannerAd = Laya.Browser.window.wx.createBannerAd({
            adUnitId: ad,
            style: {
                left: 0,
                top: 0,
                width: 460
            }
        });
        bannerAd.onLoad(() => {
            console.log("[LOG] Banner广告加载成功");
            this.hideBanner3();
            this._bannerAd3 = bannerAd;
            let info = Laya.Browser.window.wx.getSystemInfoSync();
            this._bannerAd3.style.width = info.windowWidth;
            this._bannerAd3.style.top = info.windowHeight - this._bannerAd3.style.realHeight-10;
            this._bannerAd3.show();
            var hight: number = GuangGao.GetRealHight2(this._bannerAd3,gap);
            let data = {};
            data["banner"] = this._bannerAd2;
            data["gap"] = hight;
            success && success(data);
        });
        bannerAd.onError(err => {
            console.log("[LOG] Banner广告加载失败");
            console.log(err);
            fail && fail();
        });
    }
    hideBanner3(){
        if(!Laya.Browser.onWeiXin) return
        if(this._bannerAd3){
            this._bannerAd3.hide();
            this._bannerAd3.destroy();
            this._bannerAd3 = null;
        }
    }

    static CheatBanner(btn:Laya.UIComponent,complete:Function = null,gap = 0)
    {
        btn.mouseEnabled = false;
            GuangGao.Instance.showBanner((height) => 
            {
                if(height == null)
                {
                    btn.mouseEnabled = true;
                }
                else
                {
                    let _height = Number(height["gap"]);
                    height["banner"].hide();
                    console.log("cheatstart","bottom:100");
                    btn.mouseEnabled = false;
                    console.log("——height--",_height);
                    var end=Laya.stage.height-_height-btn.height - 50;
                    console.log("y:" + end);
                    Laya.timer.once(UserInfoManager.CheatTime*1000,this,()=>
                    {
                        
                        height["banner"].show();
                        Laya.Tween.to(btn,{y:end},500,null,Laya.Handler.create(this,()=>
                        {
                            btn.mouseEnabled = true;
                            console.log("上移成功cheat1--");
                            if(complete != null)
                            {
                                
                                complete();
                            }
                        }))
                    })
                }
            });
    }
    static CheatBanner2(btn:Laya.UIComponent,complete:Function = null,gap = 0)
    {
        btn.mouseEnabled = false;
            GuangGao.Instance.showBanner2((height) => 
            {
                if(height == null)
                {
                    btn.mouseEnabled = true;
                }
                else
                {
                    let _height = Number(height["gap"]);
                    height["banner"].hide();
                    console.log("cheatstart","bottom:100");
                    // btn.mouseEnabled = false;
                    console.log("——height--",_height);
                    var end=Laya.stage.height-_height-btn.height - 50;
                    console.log("y:" + end);
                    Laya.timer.once(UserInfoManager.CheatTime*1000,this,()=>
                    {
                        
                        height["banner"].show();
                        Laya.Tween.to(btn,{y:end},500,null,Laya.Handler.create(this,()=>
                        {
                            btn.mouseEnabled = true;
                            console.log("上移成功cheat2---");
                            height["banner"].hide();
                            GuangGao.Instance.showBanner3();
                            if(complete != null)
                            {
                                complete();
                            }
                        }))
                    })
                }
            });
    }
    static CheatBanner3(btn:Laya.UIComponent,complete:Function = null,gap = 0)
    {
        btn.mouseEnabled = false;
            GuangGao.Instance.showBanner3((height) => 
            {
                if(height == null)
                {
                    btn.mouseEnabled = true;
                }
                else
                {
                    let _height = Number(height["gap"]);
                    height["banner"].hide();
                    console.log("cheatstart","bottom:100");
                    // btn.mouseEnabled = false;
                    console.log("——height--",_height);
                    var end=Laya.stage.height-_height-btn.height - 50;
                    console.log("y:" + end);
                    Laya.timer.once(UserInfoManager.CheatTime*1000,this,()=>
                    {
                        
                        height["banner"].show();
                        Laya.Tween.to(btn,{ y: end }, 500, null, Laya.Handler.create(this, () =>  {
                            console.log("上移成功cheat3--");
                            btn.mouseEnabled = true;
                            height["banner"].hide();
                            GuangGao.Instance.showBanner();
                            if (complete != null)  {
                                complete();
                            }
                        }))
                    })
                }
            });
    }


    public static GetRealHight2(banner,gap)
    {
        if (banner) 
        {
            let info = Laya.Browser.window.wx.getSystemInfoSync();
            banner.style.width = info.windowWidth;

            banner.style.top = info.windowHeight - banner.style.realHeight - gap;
            console.log("banner真实高度为---",Laya.stage.height / info.windowHeight * banner.style.realHeight);
            return Laya.stage.height / info.windowHeight * banner.style.realHeight;
        }
    }
}