import  {Main} from "../Main";
import { WXSDK } from "./WXSDK";
import { AccountManager } from "./AccountManager";
//import { HPApi } from "./HPApi";

export default class GuangGao 
{

    private static fsaasfafssfa;

    private static asgagassga;

    private static readonly videlAdUnitId = "adunit-47ace2391e74f21e";
    private static bannerAdUnitId = "adunit-876b793421608933";

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

    static InitId()
    {
       // this.bannerAdUnitId = AccountManager.Instance.curplayerData.uintid;
        //console.log(this.bannerAdUnitId);
    }

    //预先加载视屏广告
    public static JiaZaiShiPin()
    {
        if (this.afsfsaafsfsa) return;
        if (!Laya.Browser.onWeiXin) return;
        //GuangGao.InitId();
        //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.afsfasfasasf = 0;
        this.afsfsaafsfsa = true;
    }

    //看视频
    public static KaiShiPin(callback: Function) 
    {
        if (Laya.Browser.onWeiXin) 
        {
            console.log("showVideoAd , asfasfsaffsa:" + this.asfasfsaffsa);

            if (AccountManager.Instance.curplayerData.videoshare && AccountManager.Instance.curplayerData.fenxiang) 
            {
               // WXSDK.fengxiangtupian("看视频",null,"看视频");
            }

            if(AccountManager.Instance.curplayerData.video == false)
            {
                WXSDK.ShowTxt("暂未开放");
                return;
            }

            //HPApi.statistics1("video");

            this.fassfafasafs(callback);
            
            this.fsaasfafssfa.show().catch(err => 
                {
                    this.fsaasfafssfa.load().then(() => 
                    {
                        this.fassfafasafs(callback);
                        this.fsaasfafssfa.show();
                    });
                });
        } 
        else
        {
            callback(false);
        }
    }

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

    //打开banner
    public static OpenBanner(gap: number = 0,callVack: Function = null, iswait: boolean = false) 
    {
        if (!Laya.Browser.onWeiXin) return;
        if (this.afsfasfasasf >= 10000) 
        {
            var hh = this.gasagsgasgsa(gap);

            if (iswait) 
            {
                this.asgagassga.hide();
                Laya.timer.once(AccountManager.Instance.curplayerData.wudaoshijian * 1000, this, () => 
                {
                    if (callVack != null) 
                    {
                        callVack(hh);
                    }
                    this.asgagassga.show();
                })
            }
            else
            {
                if (callVack != null) 
                {
                    callVack(hh);
                }
                this.asgagassga.show();
            }
        } 
        else 
        {
            //var idd = this.bannerAdUnitId[GameUtils.getRandomInt(0,this.bannerAdUnitId.length - 1)];
            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);
                this.gasagsgasgsa(gap);
            });
            newBanner.onLoad(() => 
            {
                this.afsfasfasasf++;
                if (this.asgagassga) this.asgagassga.destroy();
                this.asgagassga = newBanner;
                var hight: number = this.gasagsgasgsa(gap);
                if(iswait)
                {
                    this.asgagassga.hide();
                    Laya.timer.once(AccountManager.Instance.curplayerData.wudaoshijian * 1000, this, () => 
                    {
                        if(callVack != null)
                        {
                            callVack(hight);
                        }
                        this.asgagassga.show();
                    })
                } 
                else 
                {
                    if (callVack != null) 
                    {
                        callVack(hight);
                    }
                    this.asgagassga.show();
                }
            });
        }
    }
    public static gasagsgasgsa(gap) 
    {
        if (this.asgagassga) 
        {
            let info = Laya.Browser.window.wx.getSystemInfoSync();
            this.asgagassga.style.width = info.windowWidth;
            this.asgagassga.style.top = info.windowHeight - this.asgagassga.style.realHeight - gap-30;
            return Laya.stage.height / info.windowHeight * this.asgagassga.style.realHeight;
        }
    }

    //clear
    static Clear()
    {
        Laya.timer.clearAll(this);
        if (!Laya.Browser.onWeiXin) return;
        if (this.asgagassga) this.asgagassga.hide();
    }

    public static agagsagags() 
    {
        if (!Laya.Browser.onWeiXin) return;
        if (this.asgagassga) this.asgagassga.hide();
    }
    public static gasagsasg() 
    {
        if (!Laya.Browser.onWeiXin) return;
        if (this.asgagassga) this.asgagassga.show();
    }

    public static get keyiwudao():boolean
	{
		var wudao = AccountManager.Instance.curplayerData.wudao;
		if(wudao == false)
		{
			console.log("cheat:"+wudao);
			return false;
		}
		if(Main.SceneNum == "1037" || Main.SceneNum == "1007" 
        || Main.SceneNum == "1044" || Main.SceneNum == "1104" 
		|| Main.SceneNum == "1089" || Main.SceneNum == "1001"
		|| Main.SceneNum == "00000")
		{
			console.log("cheat:true-> "+Main.SceneNum);
			return true;
		}
		else
		{
			var checkscence = AccountManager.Instance.curplayerData.jiancechangjin;
			if(checkscence == false)
			{
				console.log("cheat:true->checkscence: "+checkscence);
				return true;
			}
		}
		console.log("cheat:false-> "+Main.SceneNum);
		return false;
	}

    //按钮误导
    static WudaoAnNiu(btn:Laya.UIComponent,ShowBanner = true,complete:Function = null,gap = 0)
    {
        if(AccountManager.Instance.curplayerData.banner)
        {
            GuangGao.agagsagags();

            if(GuangGao.keyiwudao == false)
            {
                btn.bottom=300;
                GuangGao.OpenBanner();
            }
            else
            {
                if(ShowBanner)
                {
                    btn.bottom=100;

                    btn.mouseEnabled = false;
                    console.log("bottom:100");
                    GuangGao.OpenBanner(gap,(height) => 
                    {
                        if(height == null)
                        {
                            console.log("展示互推位");
                            // HPApi.ShowBannerImag((hh)=>
                            // {
                            //     var end=Laya.stage.height-height-btn.height;
                            //     btn.y = end;
                            //     btn.bottom = NaN;
                            // });
                        }
                        else
                        {
                            var end=Laya.stage.height-height-btn.height;
                            btn.y = end;
                            btn.bottom = NaN;
                            console.log("y:" + end);
                        }
                    },true);

                    Laya.timer.once(AccountManager.Instance.curplayerData.wudaoshijian*1000,this,()=>
                    {
                        btn.mouseEnabled = true;
                        
                        if(complete != null)
                        {
                            complete();
                        }
                    })
                }
                else
                {
                    if(complete != null)
                    {
                        complete();
                    }
                }
            }
        }
    }

    private on3DComplete(): void {
        //创建场景
        var scene;
        Laya.stage.addChild(scene);
        //创建摄像机1添加到场景
        var camera1: Laya.Camera = new Laya.Camera();
        scene.addChild(camera1);
        //摄像机1添加控制脚本
        //修改摄像机1位置及角度
        camera1.transform.translate(new Laya.Vector3(0, 2, 8), true);
        camera1.transform.rotate(new Laya.Vector3(-23, 0, 0), true, false);
        //设置视口为左半屏
        camera1.viewport = new Laya.Viewport(0, 0, 640, 720);
        //创建摄像机2添加到场景
        var camera2: Laya.Camera = new Laya.Camera();
        scene.addChild(camera2);
        //修改摄像机2位置及角度
        camera2.transform.rotate(new Laya.Vector3(-45, 0, 0), false, false);
        camera2.transform.translate(new Laya.Vector3(0, 0, 25), true);
        //设置视口为右半屏
        camera2.viewport = new Laya.Viewport(640, 0, 640, 720);
    }
private onModelOK():void{
        //添加3D场景
        var scene:Laya.Scene = new Laya.Scene();
        Laya.stage.addChild(scene);
        //创建摄像机（纵横比，近距裁剪，远距裁剪）
        var camera:Laya.Camera = new Laya.Camera(0,0.1,1000);
        //加载到场景
        scene.addChild(camera);
        //旋转摄像机角度
        camera.transform.rotate(new Laya.Vector3(-25,0,0),false,false);
        //移动摄像机位置
        camera.transform.position = new Laya.Vector3(0,5,10);
        //加入摄像机移动控制脚本
        //添加蒙皮动画角色模型
        var role3D:Laya.Sprite3D = Laya.loader.getRes("monkey/monkey.lh");
        //加载到场景
        scene.addChild(role3D);
    }
}