import GameSetting from "./GameSetting";
import { WXSdk } from "./WXSdk";
import { AppSetting } from "../GameLogic/AssetManager";
import ViewManager from "./ViewManager";


export default class BannerAndVideo1 {

    private static curVideo;

    private static curbanner;

    private static videlAdUnitId = ["adunit-be9371877a88ed51","adunit-3881e83f08f8b7c9"];
    private static bannerAdUnitId = ["adunit-e4f1a4e63109e4ba","adunit-541ca055934fd3f1"];
    private static curBannerId = 0;

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

    private static videoCallback;
    //预先加载视屏广告
    public static InitVideo() {
        if (!GameSetting.isWx) return;
        let index = Math.random() > 0.5 ? 0 : 1;
        BannerAndVideo1.curVideo = Laya.Browser.window.wx.createRewardedVideoAd({ adUnitId: BannerAndVideo1.videlAdUnitId[index] });
        BannerAndVideo1.curVideo.onLoad(() => {
            console.log('激励视频 广告加载成功');
            this.videoLoadCom = true;
        });

        BannerAndVideo1.curVideo.onError(err => {
            console.log("激励视屏加载失败:" + err);
            this.videoLoadCom = false;
        });
        BannerAndVideo1.curVideo.onClose(res => {
            console.log("激励视频关闭:" + res.isEnded);
            if (res && res.isEnded || res === undefined) { this.videoCallback && this.videoCallback(true) }
            else { this.videoCallback && this.videoCallback(false) }
            // this.curVideo.offClose();
        });

    }

    public static ShowVideo(callback: Function) {
        if (GameSetting.isWx) {

            if (BannerAndVideo1.videoLoadCom) {
                console.log("showVideoAd:" + BannerAndVideo1.videoLoadCom);

                BannerAndVideo1.videoCallback = callback;

                BannerAndVideo1.curVideo.show().catch(err => {
                    BannerAndVideo1.curVideo.load().then(() => {
                        // this.SetVideoCallBack(callback);
                        BannerAndVideo1.curVideo.show();
                    });
                });
            }
            else {
                WXSdk.Game_Share(callback);
            }


        }
        else {
            ViewManager.Instance.ShowTips("暂未开放")
            callback(true);
        }
    }
    static canshowbanner = true;
    //打开banner
    public static OpenBanner( callVack: Function = null, iswait: boolean = false) {
        //  if (callVack) callVack(null);
        if (!(GameSetting.isWx)) return;
        BannerAndVideo1.canshowbanner = true;

        var idd;


        this.curBannerId++;

        if (this.curBannerId > 1) this.curBannerId = 0;

        idd = BannerAndVideo1.bannerAdUnitId[this.curBannerId];
        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(() => {
            console.log("新的");
            if (this.curbanner) this.curbanner.destroy();
            this.curbanner = newBanner;
            let info = Laya.Browser.window.wx.getSystemInfoSync();
            this.curbanner.style.width = info.windowWidth;

            this.curbanner.style.top = info.windowHeight - this.curbanner.style.realHeight - 5;
            var hight: number = Laya.stage.height / info.windowHeight * this.curbanner.style.realHeight;
            if (callVack != null) {
                callVack(hight);
            }
            if (this.canshowbanner && !iswait) {
                this.curbanner.show();
            }
            else {
                this.curbanner.hide();
            }
        });
    }


    static Clear() {
        Laya.timer.clearAll(BannerAndVideo1);
        Laya.Tween.clearAll(BannerAndVideo1);
        BannerAndVideo1.canshowbanner = false;
        if (BannerAndVideo1.curbanner) BannerAndVideo1.curbanner.hide();
        //JiuJiuSdk.HideBannerImage();

    }
    public static showbanner() {
        if (!GameSetting.isWx) return;
        if (BannerAndVideo1.curbanner) BannerAndVideo1.curbanner.show();
    }

    //按钮误导
    static CheatBanner(btn: Laya.UIComponent, complete: Function = null) {
        if (!AppSetting.bannerCanMove || !btn||!this.WXignore()) {
            console.log("ignorecheat");
            BannerAndVideo1.OpenBanner();
        }
        else {
            btn.bottom = NaN;
            btn.y = Laya.stage.height - btn.height - 40;
            BannerAndVideo1.OpenBanner( (height) => {
                if (!height) {
                    return;
                } else {
                    //  btn.y = Laya.stage.height - btn.height - 100;
                    console.log("cheatstart", "bottom:100");
                    btn.mouseEnabled = false;
                    var end = Laya.stage.height - height - btn.height - 20;
                    console.log("y:" + end);
                    //  console.log(UseModule.CurrentAccount.CurAccountData.cheattime);
                    Laya.timer.once(AppSetting.moveTime * 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 static WXignore(): boolean {
        if (!AppSetting.OpenSceneValue) {
            return true;
        }
        //1037  1058 1067 1095
        if (GameSetting.wxValue == "1044" || GameSetting.wxValue == "1104" || GameSetting.wxValue == "00000" || GameSetting.wxValue == "1058" || GameSetting.wxValue == "1067" || GameSetting.wxValue == "1095"
            || GameSetting.wxValue == "1089" || GameSetting.wxValue == "1001" || GameSetting.wxValue == "1037" || GameSetting.wxValue == "1007"
        ) {
            return true;
        }
        return false;
    }
}