
import { IWindow } from "./Component/IWindow";
import { ui } from "../ui/layaMaxUI";
import { GameManager, GameState } from "../Scripts/Game/GameManager";
import { ResourcesManager } from "../Scripts/Game/ResourcesManager";
import PoolManager from "../Scripts/Game/PoolManager";
import WxApi from "../WXSDK/wxsdk";
import { Player } from "../Scripts/Game/Player";
import { LevelManager } from "../Scripts/Game/LevelManager";
import ADPlayer from "../WXSDK/ADPlayer";
import { Main } from "../Main";
import { WindowManager, WindowType } from "./WindowManager";
import { RecommendWindow } from "./RecommendWindow";
import { FriendPlyaingWindow } from "./FriendPlayingWindow";


export class ReviveWindow extends ui.Window.ReviveWindowUI implements IWindow
{
    private static instance:ReviveWindow;
    constructor()
    {
        super();
        ReviveWindow.instance = this;
    }

    public static get Instance()
    {
        return ReviveWindow.instance;
    }

    curstage:Laya.Sprite3D;
    showScence:Laya.Scene3D;
    showcarCamera:Laya.Camera;
    showlight:Laya.DirectionLight;
    cursprite:Laya.Sprite3D;
    curanimator:Laya.Animator;

    OnOpen(_data = null)
    {
        WxApi.Instance.recordAldEvent("进入复活界面","1");

        this.passBtn.clickHandler = Laya.Handler.create(this,this.OnPassRevive);
        this.reviveBtn.clickHandler = Laya.Handler.create(this,this.OnRevive,null,false);
        this.stagepre.value = Player.Instance.CurLevelPrecent;
        this.stagetex.value = LevelManager.Instance.curLevel.levelid.toString();
        this.stageClip.value = Math.floor(Player.Instance.CurLevelPrecent * 100).toString();
        this.getmoney.value = GameManager.Instance.gamegold.toString();

        WindowManager.Instance.OpenPopView(WindowType.RecommendWindow);
        RecommendWindow.Instance.ShowRecommendWindow(false,false,false,true,false);
        // RecommendWindow.Instance.cainixihuan.bottom = 480;

        // this.showScence = new Laya.Scene3D();
        // this.addChild(this.showScence);
        // this.ReadyShow();

        // ADPlayer.showBannerAd();
        ADPlayer.MoveButton(this.BtnPan);
        this.height = Laya.stage.height;
    }

    OnPassRevive()
    {
        WxApi.Instance.recordAldEvent("点击跳过复活","1");
        GameManager.Instance.ChangeState(GameState.End,false);
        
        //WindowManager.Instance.ShowWindow(WindowType.FriendPlayingWindow);
        
       
       
    }

    ReadyShow()
    {
        this.showcarCamera = new Laya.Camera();
        this.showScence.addChild(this.showcarCamera);

        this.showlight = new Laya.DirectionLight();
        this.showScence.addChild(this.showlight);

        this.curstage = new Laya.Sprite3D();
        this.showScence.addChild(this.curstage);

        this.showcarCamera.viewport = new Laya.Viewport(this.showpan.x,this.showpan.y,this.showpan.width,this.showpan.height);
        this.showcarCamera.clearFlag = 2;
        this.showcarCamera.fieldOfView = 30;
        this.showcarCamera.transform.position = new Laya.Vector3(0,0.5,1.1);

        ResourcesManager.Instance.LoadPlayer(1,Laya.Handler.create(this,this.OnPlayerLoad,[1]));
    }

    OnPlayerLoad(_id:number,_model:Laya.Sprite3D)
    {
        var cursprite = _model.getChildAt(0) as Laya.Sprite3D;
        this.cursprite = PoolManager.Instance.CreatMeshSprite3D(cursprite,"");
        this.curanimator = this.cursprite.getChildAt(0).getComponent(Laya.Animator) as Laya.Animator;
        this.curstage.addChild(this.cursprite);

        this.cursprite.transform.localPosition = new Laya.Vector3(0,0,0);
        this.cursprite.transform.rotationEuler = new Laya.Vector3(-10,0,0);
        var lookpos = new Laya.Vector3(this.cursprite.transform.position.x,this.cursprite.transform.position.y + 0.3,this.cursprite.transform.position.z + 0.01);
        this.showcarCamera.transform.lookAt(lookpos,new Laya.Vector3(0,1,0));
        
        Laya.timer.once(500,this,()=>
        {
            this.curanimator.play("lose");
        })
    }

    OnRevive()
    {
        if(!Main.OnWx)
        {
            GameManager.Instance.ReviveGame();
            return;
        }

        WxApi.Instance.recordAldEvent("点击复活","1");
        ADPlayer.showVideoAd((flag)=>
        {
            if(flag)
            {
                WxApi.Instance.recordAldEvent("复活成功","1");
                GameManager.Instance.ReviveGame();
            }
            else
            {
                WxApi.Instance.recordAldEvent("复活失败","1");
            }
        })
    }

    OnHide()
    {
        // PoolManager.Instance.RecoverMeshSprite3D(this.cursprite);
        // this.showcarCamera.destroy();
        // this.showScence.destroy();
    }
}