import { ui } from "../../ui/layaMaxUI";
import { EventManager } from "../GameLogic/EventManager";
import Transform3D = Laya.Transform3D;
import { PlayerController } from "../GameLogic/PlayerController";
import Vector3 = Laya.Vector3;


export class game_ui extends ui.game_uiUI implements UI_Interface {


    private pTrans: Transform3D;
    constructor() {
        super();
        this.resizeHander();
        EventManager.on(Laya.Event.RESIZE, this, this.resizeHander);
    }
    resizeHander() {
        this.height = Laya.stage.height;

    }
    OnShow(data?: any) {
        this.initEvent();
        this.initData();
    }

    initEvent() {
        this.on(Laya.Event.MOUSE_DOWN, this, this.OnClickDown);
        this.on(Laya.Event.MOUSE_UP, this, this.OnClickUp);
        this.on(Laya.Event.MOUSE_OUT, this, this.OnClickUp);
    }
    offEvent() {
        EventManager.off(Laya.Event.RESIZE, this, this.resizeHander);
        this.off(Laya.Event.MOUSE_DOWN, this, this.OnClickDown);
        this.off(Laya.Event.MOUSE_UP, this, this.OnClickUp);
        this.off(Laya.Event.MOUSE_OUT, this, this.OnClickUp);
    }

    initData() {
        var p = PlayerController.Instance.player;
        if (p) this.pTrans = p.transform;
    }


    private _mousePos: Vector3=Vector3._ZERO.clone();
    private tempMouse = Vector3._ZERO.clone();
    private tar = Vector3._ZERO.clone();
    private lerpPos = Vector3._ZERO.clone();
    OnClickDown(e: Laya.Event) {
        this.on(Laya.Event.MOUSE_MOVE, this, this.OnSlider);
        this._mousePos.x = Laya.MouseManager.instance.mouseX;
        this._mousePos.z = Laya.MouseManager.instance.mouseY;
    }
    OnClickUp(e: Laya.Event) {
        this.off(Laya.Event.MOUSE_MOVE, this, this.OnSlider);
    }

    OnSlider(e: Laya.Event) {
        this.tempMouse.x = Laya.MouseManager.instance.mouseX;
        this.tempMouse.z = Laya.MouseManager.instance.mouseY;
        var offestX = this.tempMouse.x - this._mousePos.x;
        var offestY = this.tempMouse.z - this._mousePos.z;
        if (offestX > 0) offestX = offestX < 0.2 ? offestX : 0.2;
        if (offestX < 0) offestX = offestX > -0.2 ? offestX : -0.2;
        if (offestY > 0) offestY = offestY < 0.2 ? offestY : 0.2;
        if (offestY < 0) offestY = offestY > -0.2 ? offestY : -0.2;

        if (this.pTrans) {
            this.tar.x = this.pTrans.localPositionX -= offestX;
            this.tar.y = this.pTrans.localPositionY;
            this.tar.z = this.pTrans.localPositionZ -= offestY;
        }
        Vector3.lerp(this.pTrans.localPosition, this.tar, 0.1, this.lerpPos);
        this.pTrans.localPositionX = this.lerpPos.x;
        this.pTrans.localPositionZ = this.lerpPos.z;
        this._mousePos.x = Laya.MouseManager.instance.mouseX;
        this._mousePos.z = Laya.MouseManager.instance.mouseY;
    }

    OnClose() {
        this.offEvent();
    }
}