import { Main } from "../Main";
import UserModel from "./UserModel";
import { FYHD_Sdk } from "./FYHD_Sdk";

import { WXSdk } from "./WXSdk";


export class BannerAndVideo {
    private static RewardedVideoAd;
    private static BannerAd;
    private static videoId = "adunit-1cf906a0264f73a6";
    private static bannerId = "adunit-be1cfcdad2aea78b";
    private static VideoIsLoad: boolean = false;
    private static ExitIsShowBanner = false;
    private static blockbannerShow: boolean = false;
    private static ShowBanner(callVack: Function = null, iswait: boolean = false) {
        if (!Main.IsWx) return;
        BannerAndVideo.blockbannerShow = false;
        if (BannerAndVideo.BannerAd) BannerAndVideo.BannerAd.hide();
        let newBanner = Laya.Browser.window.wx.createBannerAd({
            adUnitId: BannerAndVideo.bannerId,
            style: {
                left: 0,
                top: 0,
                width: 460
            }
        });
        newBanner.onError(err => {
            console.log("onError:" + JSON.stringify(err));
            if (callVack != null) {
                callVack(null);
            }
        });
        newBanner.onLoad(() => {
            if (this.BannerAd)
                this.BannerAd.destroy();
            this.BannerAd = newBanner;
            console.log('banner 广告加载成功');
            FYHD_Sdk.ClickAd(FYHD_Sdk.Ad_ClickType.Banner);
            var rh: number;
            if (this.blockbannerShow) {
                this.BannerAd.hide();
            } else {
                rh = this.showBanner();
            }
            if (iswait) {
                this.BannerAd.hide();
                console.log("banner等待时间显示" + UserModel.Instance.CurModel.BannerMoveTime);
                Laya.timer.once(UserModel.Instance.CurModel.BannerMoveTime * 1000, this, () => {
                    this.BannerAd.show();
                    if (callVack != null) {
                        console.log("rh" + rh);
                        callVack(rh);
                    }
                });
            } else {
                if (this.ExitIsShowBanner) {
                    this.BannerAd.show();
                }
            }
        });
    }
    private static ratio;
    private static showBanner() {
        console.log("show banner ad");
        if (BannerAndVideo.BannerAd) {
            let info = Laya.Browser.window.wx.getSystemInfoSync();
            BannerAndVideo.BannerAd.style.width = info.windowWidth;
            BannerAndVideo.ratio = info;
            BannerAndVideo.BannerAd.show().catch(err => console.log("catch:" + err));
            BannerAndVideo.BannerAd.style.top = info.windowHeight - BannerAndVideo.BannerAd.style.realHeight;
            var h = Laya.stage.height / info.windowHeight * BannerAndVideo.BannerAd.style.realHeight;
            console.log("bannerAD的高度:++++++" + h);
            return h;
        }
    }
    public static HideBanner() {
        if (!Main.IsWx) return;
        BannerAndVideo.blockbannerShow = true;
        BannerAndVideo.ExitIsShowBanner = false;
        Laya.timer.clearAll(BannerAndVideo);
        if (BannerAndVideo.BannerAd) BannerAndVideo.BannerAd.hide();
    }

    //预先加载视屏广告
    public static InitVideo() {
        if (!Main.IsWx) return;
        this.RewardedVideoAd = Laya.Browser.window.wx.createRewardedVideoAd({ adUnitId: BannerAndVideo.videoId });
        this.RewardedVideoAd.onLoad(() => {
            console.log('激励视频 广告加载成功');
            this.VideoIsLoad = true;
        });
        this.RewardedVideoAd.onError(err => {
            console.log("激励视屏加载失败:" + err);
            this.VideoIsLoad = false;
            WXSdk.ShowToast("今日看视屏次数已用完!明天再来吧^_^", false);
        });
        this.RewardedVideoAd.onClose(res => {
            console.log("come++++++++++");
            if (this.videoComCallBack != null) {
                WXSdk.HideLoading(false);
                if (res && res.isEnded || res == undefined) {
                    this.videoComCallBack(true);
                    FYHD_Sdk.ClickAd(FYHD_Sdk.Ad_ClickType.Watch);
                }
                else {
                    this.videoComCallBack(false);
                }
            }
            // this.RewardedVideoAd.offClose();
        });
    }


    public static get mVideoAdIsload(): boolean {
        if (!Main.IsWx) return true;
        if (!this.VideoIsLoad)
            this.RewardedVideoAd.load();
        return this.VideoIsLoad;
    }
    private static videoComCallBack: Function = null;
    public static showVideoAd(callback: Function) {
        if (!Main.IsWx) return;
        console.log("showVideoAd , videoAdIsload:" + this.VideoIsLoad);
        if (UserModel.Instance.CurModel.IsShare) {
            if (UserModel.Instance.CurModel.ISShareThenVideo) {
                WXSdk.Game_Share();
            }
        }
        FYHD_Sdk.ClickAd(FYHD_Sdk.Ad_ClickType.Video);
        WXSdk.ShowLoading(null, false);
        this.videoComCallBack = callback;
        this.RewardedVideoAd.show().catch(err => {
            this.RewardedVideoAd.load().then(() => this.RewardedVideoAd.show());
        });
    }

    private static isfirst = true;
    public static LoadVideo(ca: Function) {
        if (this.isfirst) {
            if (!Main.IsWx) return true;
            WXSdk.ShowLoading(null, false);
            this.RewardedVideoAd = Laya.Browser.window.wx.createRewardedVideoAd({ adUnitId: BannerAndVideo.videoId });
            this.RewardedVideoAd.onLoad(() => {
                console.log('激励视频 广告加载成功');
                this.VideoIsLoad = true;
                if (this.isfirst) {
                    console.log("这里我只来一次");
                    this.isfirst = false;
                    if (ca) {
                        ca(true);
                    }
                }
            });
            this.RewardedVideoAd.onError(err => {
                if (this.isfirst) {
                    console.log("这里我可不想来");
                    this.isfirst = false;
                    if (ca) {
                        ca(false);
                    }
                }
                console.log("激励视屏加载失败:" + err);
                this.VideoIsLoad = false;
                WXSdk.ShowToast("今日看视屏次数已用完!明天再来吧^_^", false);
            });
            this.RewardedVideoAd.onClose(res => {
                console.log("come++++++++++");
                if (this.videoComCallBack != null) {
                    WXSdk.HideLoading(false);
                    if (res && res.isEnded || res == undefined) {
                        this.videoComCallBack(true);
                        FYHD_Sdk.ClickAd(FYHD_Sdk.Ad_ClickType.Watch);
                    }
                    else {
                        this.videoComCallBack(false);
                    }
                }
                // this.RewardedVideoAd.offClose();
            });
        } else {
            if (!Main.IsWx) {
                if (ca)
                    ca(true);
                return;
            }
            if (!this.VideoIsLoad)
                this.RewardedVideoAd.load();
            if (ca) ca(this.VideoIsLoad);
        }
    }

    // public static MoveBanner(btn: Laya.UIComponent, da__ta?: string) {
    //     if (UserModel.Instance.CurModel.HaveBanner) {
    //         if (GameManager.Instance.WXignore() && btn && UserModel.Instance.CurModel.BtnCanMove) {
    //             console.log("btn原来的高度" + btn.y);
    //             btn.bottom = NaN;
    //             var origHeight = btn.y;
    //             btn.y = Laya.stage.height - 70 - btn.height;
    //             console.log("向下移动" + btn.y);
    //             BannerAndVideo.ShowBanner((height) => {
    //                 if (!btn) {
    //                     BannerAndVideo.HideBanner();
    //                     return;
    //                 }
    //                 if (!height) {
    //                     console.log("banner加载失败;btn原始高度:" + origHeight);
    //                     btn.y = origHeight;
    //                     return;
    //                 }
    //                 console.log("真实的高度" + height);
    //                 var end = Laya.stage.height - height - btn.height;
    //                 console.log(end);
    //                 btn.y = end;
    //                 console.log(btn.y);
    //             }, true);
    //         } else {
    //             BannerAndVideo.ShowBanner();
    //         }
    //     }
    // }
    
    // public static GggMove(btn: Laya.UIComponent, da__ta?: string){
    //     if (!Main.IsWx) return;
    //     if (!this.BannerAd) return;
    //    var top= this.BannerAd.style.top;
    //    this.BannerAd.style.top= top-btn.height;
    //    Laya.timer.once(800,this,()=>{
    //     this.BannerAd.style.top=top;
    //    });
    // }

}

