import { LevelManager } from "./LevelManager";
import { AssetManager } from "../Resources/AssetManager";
import { MateManager } from "./MateManager";
import { MateMove } from "../Game/MateMove";
import { AccountManager } from "../NetWork/AccountManager";
import { DispatcherManager, StageState } from "../GameUtils/EventManager";

export class PlayerManager{
    private static ins:PlayerManager;
    constructor(){
        PlayerManager.ins = this;
    }
    public static get Instance():PlayerManager{
        if(PlayerManager.ins == null){
            return new PlayerManager();
        }
        return PlayerManager.ins;
    }
    // public PlayerNode:Laya.Sprite3D;
    public player:Laya.Sprite3D;

    //是否能移动
    public canmove:boolean = false;
    private Direction:number;

    Init(){
        this.Direction = AccountManager.Instance.curplayerData.LevelDirection;

        let playerpref = AssetManager.Instance.Character.get(1) as Laya.Sprite3D;
        this.player = Laya.Sprite3D.instantiate(playerpref);
        LevelManager.Instance.spritepool.addChild(this.player);
        this.player.transform.position = AssetManager.Instance.playerpos.transform.position.clone();
        this.player.transform.rotation = AssetManager.Instance.playerpos.transform.rotation.clone();
        this.trun = true;

        DispatcherManager.StageOn(StageState.ShowGuide1,this,this.Guide1);
        DispatcherManager.StageOn(StageState.ShowGuide2,this,this.Guide2);
    }
    RefreshPro():number{
        let startpoint = AssetManager.Instance.playerpos.transform.position.clone();
        let Z = startpoint.z;
        let finishpoint = AssetManager.Instance.finishpoint.z;
        let distance = finishpoint - Z;
        let pro = this.player.transform.position.z/distance;
        return pro;
    }
    EnablePlayer(){
        let _move =  this.player.addComponent(MateMove) as MateMove;
        _move.Tag_Head = true;
        this.ListenOn();
    }
    ListenOn(){
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.OnMouseDown);
    }
    ListenOff(){
        Laya.stage.off(Laya.Event.MOUSE_DOWN,this,this.OnMouseDown);
    }
    DisablePlayer(){
        let _move = this.player.getComponent(MateMove) as MateMove;
        if(_move){
            _move.destroy();
        }
    }
    Guide1(){
        this.ListenOff();
        let _matemove = this.player.getComponent(MateMove) as MateMove;
        if(_matemove){
            _matemove.Pause();
        }
    }
    Guide2(){
        let _matemove = this.player.getComponent(MateMove) as MateMove;
        if(_matemove){
            _matemove.Pause();
        }
        this.guide2stop = true;
    }
    Resume(){
        let _matemove = this.player.getComponent(MateMove) as MateMove;
        if(_matemove){
            _matemove.Resume();
        }
    }
    guide2stop = false;
    trun = true;
    // first = true;
    OnMouseDown(){
        if(this.Direction == 1){
            if(this.trun){
                this.player.transform.rotationEuler = new Laya.Vector3(0,-90,0);
                this.trun = !this.trun;
            }
            else{
                this.player.transform.rotationEuler = new Laya.Vector3(0,0,0);
                this.trun = !this.trun;
            }
        }
        else if(this.Direction == 2){
            if(this.trun){
                this.player.transform.rotationEuler = new Laya.Vector3(0,90,0);
                this.trun = !this.trun;
            }
            else{
                this.player.transform.rotationEuler = new Laya.Vector3(0,0,0);
                this.trun = !this.trun;
            }
        }
        if(this.guide2stop){
            this.guide2stop = false;
            this.Resume();
            DispatcherManager.StageTrigger(StageState.HideGuide2);
        }
    }
}