
import { WxApi } from "./Sdk";
import { ShareUtil } from "./ShareUtil";
import { PlayerAccount } from "./PlayerAccount";
import { FYHD_Sdk } from "./FYHD_Sdk";
import { ttad } from "./ttad";

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

export class GuangGao 
{

    private static fsaasfafssfa;

    private static curbanner;

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

    static get hasbanner():boolean
    {
        if(PlayerAccount.CurrentAccount.CurAccountData.banner)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    static get hasvideo():boolean
    {
        if(Laya.Browser.onWeiXin && PlayerAccount.CurrentAccount.CurAccountData.video)
        {
            var videonum = PlayerAccount.CurrentAccount.CurAccountData.GetGlobe
            return true;
        }
        else
        {
            return false;
        }
    }

    //预先加载视屏广告
    public static InitVideo()
    {
        if (!Laya.Browser.onWeiXin || !PlayerAccount.CurrentAccount.CurAccountData.video) 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.hasvideo) 
        {
            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
            {

                // ShareUtil.shareImage("视频没有分享", callback
                //     , "1");

            }

            
        } 
        else
        {
            callback(false);
            // ShareUtil.shareImage("视频没有分享", callback
            //         , "1");
        }
    }

    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 (!this.hasbanner)
        // {
        //     callVack(null);
        //     return;
        // } 

        if(!Laya.Browser.onWeiXin)
        {
            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(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()
    {
        GuangGao.clearTween();
        Laya.timer.clearAll(this);
        Laya.Tween.clearAll(this);
        this.canshowbanner = false;
        if (this.curbanner) this.curbanner.hide();

        // if (!Laya.Browser.onWeiXin) return;
        // 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){
            return true;
        }
        else{
            return false;
        }
        // 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();
        //console.log(info);
        //console.log(btn);
        //console.log(btn.y,Laya.stage.height,info.windowHeight);
        var reshight = btn.y * info.windowHeight / Laya.stage.height;

        //console.log("reshight",reshight);

        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;

            //console.log("top",this.asgagassga.style.top);
            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 tween:Laya.Tween;
    //按钮误导
    static CheatBanner(btn:fairygui.GObject,complete:Function = null,gap = 0)
    {
        ttad.Showbanner();
        // if(!GuangGao.cancheat)
        // {
        //     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 - 80;
        //             console.log("y:" + end);
        //             Laya.timer.once(PlayerAccount.CurrentAccount.CurAccountData.cheattime*1000,this,()=>
        //             {
        //                 // btn.mouseEnabled = true;
        //                 this.showbanner();
        //                 GuangGao.clearTween();
        //                  GuangGao.tween = Laya.Tween.to(btn,{y:end},200,null,Laya.Handler.create(this,()=>
        //                 {
        //                     if(complete != null)
        //                     {
        //                         complete();
        //                     }
        //                 }))
        //             })
        //         }
        //     },true);
        // }
    }

    public static clearTween():void
    {
        if( GuangGao.tween )
        {
            GuangGao.tween.clear();
            GuangGao.tween = null;
        }
    }

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