import Vector3 = Laya.Vector3;
import { EventManager, EventType } from "./EventManager";
import { GameManager, GameState } from "./GameManager";
import { LevelManager } from "./LevelManager";
import { GameUtils } from "../../GameUtils/GameUtils";

export default class CameraManager extends Laya.Script
{
    private static CameraManagerInstance:CameraManager;
    constructor()
    {
        super();
        CameraManager.CameraManagerInstance = this;
    }
    public static get Instance():CameraManager
    {
        return CameraManager.CameraManagerInstance;
    }

    camera:Laya.Camera;
    public follow = false;
    lookat = false;

    curTarget:Laya.Transform3D;
    offest:Vector3 = new Vector3();
    curCameraPos:Vector3 = new Vector3();

    startPos = new Vector3();
    starRot = new Vector3();

    forwardDistance = 2;

    addspeed = 1;

    onAwake()
    {
        this.camera = this.owner as Laya.Camera;
    }

    public SetFollow(_target:Laya.Transform3D,_lookat = false)
    {
        this.curTarget = _target;
        Vector3.subtract(_target.position,this.camera.transform.position,this.offest)
        this.follow = true;
        this.lookat = _lookat;

        this.startPos = this.camera.transform.position.clone();
        this.starRot = this.camera.transform.rotationEuler.clone();

        Laya.timer.clearAll(this);
    }


    public StopFollow()
    {
        this.follow = false;
        this.curTarget = null;

        this.offesty = 0;
        this.offestz = 0;

        this.offest = new Vector3(0,0,0);
        this.curCameraPos = new Vector3(0,0,0);
        this.lerpPos = new Vector3(0,0,0);
    }

    public PauseFollow()
    {
        this.follow = false;
    }

    public ResumeFollow()
    {
        this.follow = true;
    }

    followrate = 0.3;
    lerpPos = new Vector3();
    lerprot = new Laya.Quaternion();
    lookdir = new Vector3();
    offesty = 0;
    offestz = 0;
    onUpdate()
    {
        if(this.follow && this.curTarget)
        {
            Vector3.subtract(this.curTarget.position,this.offest,this.curCameraPos);

            this.offesty = GameUtils.lerp(this.offesty,1,0.02);
            //this.offestz = GameUtils.lerp(this.offestz,0.2,0.01);

            this.curCameraPos = new Vector3(this.curCameraPos.x,this.curCameraPos.y + this.offesty,this.curCameraPos.z + this.offestz);
            
            Vector3.lerp(this.camera.transform.position,this.curCameraPos,this.followrate,this.lerpPos);

            this.camera.transform.position = new Vector3(this.curCameraPos.x,this.lerpPos.y,this.lerpPos.z);

            var quaternion1 = new Laya.Quaternion();
            var lookpos = new Vector3(this.lerpPos.x,this.lerpPos.y - 0.7,this.lerpPos.z);
            Laya.Quaternion.lookAt(lookpos,this.curTarget.position,new Vector3(0,1,0), quaternion1);
            quaternion1.invert(quaternion1);
            Laya.Quaternion.slerp(this.camera.transform.rotation,quaternion1,0.05,quaternion1);
            this.camera.transform.rotation = quaternion1;
        }
    }

    public SetCameraPos()
    {
        Vector3.subtract(this.curTarget.position,this.offest,this.curCameraPos);

        Vector3.lerp(this.camera.transform.position,this.curCameraPos,0.7,this.lerpPos);
        
        this.camera.transform.position = new Vector3(this.lerpPos.x,this.camera.transform.position.y,this.camera.transform.position.z);
    }

    public get GetCurCameraPos():Vector3
    {
        Vector3.subtract(this.curTarget.position,this.offest,this.curCameraPos);
        return this.curCameraPos;
    }

}