import Vector2 = Laya.Vector2;
import Vector3 = Laya.Vector3;
import { GameTools } from "../Tools/GameTools";
import { AssetManager } from "../Tools/AssetManager";
import { Runner, CharacterState } from "./Runner";
import { EventManager, StageState } from "../Tools/EventManager";
import InGameView from "../View/InGameView";
import { GameManager } from "./GameManager";
import { AccountManager } from "../Net/AccountManager";
import { StageControl } from "./StageControl";
import { WXSDK } from "../Net/WXSDK";
export class PlayerControl{
    private static ins:PlayerControl;
    public Player:Laya.Sprite3D;
    public Camera:Laya.Camera;
    //向下检测到的物体
    public DownHit:Laya.HitResult = new Laya.HitResult();
     //当前关卡
     private stage:number;
    //玩家速度
    public speed:number = 1;
    //是否为按压状态
    public press:boolean = false;
    //箭的位置
    public arrowpos:Laya.Sprite3D;

    public win:boolean = false;

    _runner:Runner;

    constructor(){
        PlayerControl.ins = this;
        EventManager.StageOn(StageState.Lose,this,this.GameOver);
        EventManager.StageOn(StageState.Win,this,this.GameOver);
    }
    public static get Instance():PlayerControl{
        if(PlayerControl.ins){
            return PlayerControl.ins
        }
        else{
            return new PlayerControl();
        }
    }
    Init(_player:Laya.Sprite3D){
        this.win = false;
        this.Player = _player;
        this.arrowpos = _player.getChildByName("arrowpos") as Laya.Sprite3D;
        let runner = this.Player.getComponent(Runner) as Runner;
        if(runner){
            runner.destroy();
        }
        this._runner = this.Player.addComponent(Runner) as Runner;
        this.Camera = AssetManager.Instance.maincamera;
        this.Camera.transform.position = new Laya.Vector3(0,10,-19);
        this.Camera.transform.rotationEuler = new Laya.Vector3(-35.7,180,0);
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
        this.CameraFollow();
        Laya.timer.frameLoop(1,this,this.CheckWin);
        this.stage =Number(AccountManager.Instance.curplayerData.GainAccount("stage"));

        this.curtouchsmoothness = AccountManager.Instance.curplayerData.CurTouchsmoothness;
        this.curtouchsize = AccountManager.Instance.curplayerData.CurTouchsize;
    }
    mouseDownTime:number;
    
    curMousePos:Vector2;

    offset:Vector3 = new Vector3(0,0,0);
    offestx = 0;

    curtouchsize:number;
    curtouchsmoothness:number;

    CheckWin(){
        if(this.stage == 1){
            if(this.Player.transform.position.z>= StageControl.Instance.curstage.endposition.z*1.65 && !this.win){
                this.win = true;
                EventManager.StageTrigger(StageState.Win);
                Laya.timer.clear(this,this.CheckWin);
            }
        }
        else if(this.stage>2 && StageControl.Instance.curstage.isboss == 0){
            if(this.Player.transform.position.z>= StageControl.Instance.curstage.endposition.z*1.2 && !this.win){
                this.win = true;
                EventManager.StageTrigger(StageState.Win);
                Laya.timer.clear(this,this.CheckWin);
            }
        }
    }
    first = false;
    onMouseDown(e: Laya.Event) {
        if(!this._runner.canmove)return;
        if(this.stage>2){
            this._runner.started = true;
        }
        else if(this.stage == 2 && !this.first){
            this._runner.started = true;
        }
        if(this.stage == 2 && !this.first){
            this.first = true;
            Laya.timer.once(1500,this,()=>{
                this._runner.started = false;
                InGameView.Instance.Guide2.visible = true;
            })
        }
        if(InGameView.Instance.Guide1.visible){
            InGameView.Instance.Guide1.visible = false;
            WXSDK.AldEvent("完成第一阶段引导");
            InGameView.Instance.ani1.stop();
            this._runner.started = true;
            Laya.timer.once(2000,this,()=>{
                this._runner.started = false;
                InGameView.Instance.Guide2.visible = true;
            })
        }
        
        this._runner._press = true;
        this.curMousePos = new Laya.Vector2(Laya.MouseManager.instance.mouseX,Laya.MouseManager.instance.mouseY);

        Laya.timer.frameLoop(1,this,this.onMouseMove);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.onMouseUp);
        // // //移出屏幕视作松开鼠标
        Laya.stage.on(Laya.Event.MOUSE_OUT, this, this.onMouseUp);
        // if(this.stage>1){
            this._runner.ChangeState(CharacterState.Run);
        // }
        
        EventManager.StageTrigger(StageState.Press);
    }

    onMouseMove() {
        if(this.stage!=1){
            if(!this._runner.canmove){
                Laya.timer.clear(this,this.onMouseMove);
                return;
            }
        }
        var MousePos = new Laya.Vector2(Laya.MouseManager.instance.mouseX,Laya.MouseManager.instance.mouseY);
        var offestX = MousePos.x - this.curMousePos.x;

        offestX = offestX > 50?50:offestX;
        offestX = offestX < -50?-50:offestX;

        this.offestx = GameTools.Instance.lerp(this.offestx,offestX,this.curtouchsmoothness);//0.005
        // this._runner.MoveX(-this.offestx);
        var dir = new Vector3(-offestX*this.curtouchsize,0,0);
        this._runner.TurnDir(dir);
        this.curMousePos = MousePos;
    }

    onMouseUp() {
        if(!this._runner.canmove)return;
        this.offestx = 0;
        Laya.timer.clear(this,this.onMouseMove);
        this._runner._press = false;
        
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.onMouseMove);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.onMouseUp);
        Laya.stage.off(Laya.Event.MOUSE_OUT, this, this.onMouseUp);

        if ( !this._runner.shoot) {
            //射箭逻辑
            this._runner.shoot = true;
            this._runner.Archery();
        }
        this._runner.ChangeState(CharacterState.Walk);
        EventManager.StageTrigger(StageState.UnPress);
    }

    GameOver(){
        this._runner.canmove = false;
        this._runner._press = false;
        this.CameraCancelFollow();
        Laya.timer.clear(this,this.CheckWin);
        Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.onMouseMove);
        Laya.stage.off(Laya.Event.MOUSE_UP, this, this.onMouseUp);
        Laya.stage.off(Laya.Event.MOUSE_OUT, this, this.onMouseUp);
        Laya.stage.off(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
    }
    Revive(){
        this.Camera.transform.position = new Vector3(this.Camera.transform.position.x,this.Camera.transform.position.y,this.Camera.transform.position.z-15);
        this._runner.Revive();
        this.CameraFollow();
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.onMouseDown);
    }

     /**相机跟随*/
     CameraFollow(){
         this.hights = 0;
         this.wights = 0;
         Vector3.subtract(this.Player.transform.position,this.Camera.transform.position,this.poschazhi);
         Laya.timer.frameLoop(1,this,this.GenSui);
     }
     poschazhi = new Vector3();
     gengsuipos = new Vector3();
     lllpos = new Vector3();
     hights = 0;
     wights = 0;
    
     /**相机跟随方法 */
     GenSui(){
         Vector3.subtract(this.Player.transform.position,this.poschazhi,this.gengsuipos);
         this.hights = GameTools.Instance.lerp(this.hights,1,0.03);
         this.wights = GameTools.Instance.lerp(this.wights,0.8,0.02);
         this.gengsuipos = new Vector3(this.gengsuipos.x,this.gengsuipos.y,this.gengsuipos.z - this.wights);
         Vector3.lerp(this.Camera.transform.position,this.gengsuipos,0.25,this.lllpos);
        //  this.lllpos = new Vector3(this.gengsuipos.x,this.gengsuipos.y,this.lllpos.z);
         this.lllpos = new Vector3(this.gengsuipos.x,this.lllpos.y,this.lllpos.z);
         this.Camera.transform.position = this.lllpos;
         var qqqwe = new Laya.Quaternion();
         var eyePos = new Vector3(this.gengsuipos.x,this.gengsuipos.y - 0.8,this.gengsuipos.z);
         Laya.Quaternion.lookAt(eyePos,this.Player.transform.position, new Vector3(0,1,0), qqqwe);
         qqqwe.invert(qqqwe);
         Laya.Quaternion.slerp(this.Camera.transform.rotation,qqqwe,0.1,qqqwe);
         this.Camera.transform.rotation = qqqwe;
     }

     CameraCancelFollow(){
        Laya.timer.clear(this,this.GenSui);
     }

}