
import Event = Laya.Event;
import Vector3 = Laya.Vector3;
import MouseManager = Laya.MouseManager;
import { Scene3DManager } from "./Scene3DManager";
import Sprite3D = Laya.Sprite3D;
import { DispatcherManager, EventType } from "./DispatcherManager";
import { Mathf } from "../Util/Mathf";
import MainRoleManager from "../Manager/MainRoleManager";
export default class MouseEvent {
    private static instance: MouseEvent;
    public static get Instance(): MouseEvent {
        return this.instance || (this.instance = new MouseEvent());
    }
    public InitMouseEvent() {
        Laya.stage.on(Event.MOUSE_DOWN, this, this.OnClickDown);

        Laya.stage.on(Event.MOUSE_OUT, this, this.OnClickUp);
    }

    private _switch = false;

    private _isCanClick = true;

    private clickD = false;
    private OnClickDown(e: Event) {
        if (!this._isCanClick || !this._switch) return;
        if (e.target instanceof Laya.UIComponent) return;
        this.clickD = true;
        Laya.stage.on(Event.MOUSE_UP, this, this.OnClickUp);
        Laya.stage.on(Event.MOUSE_MOVE, this, this.OnSlider);
        this._mousePos.x = MouseManager.instance.mouseX;
        this._mousePos.y = MouseManager.instance.mouseY;
        this.MouseD();
    }
    public StartGameClick() {
        this._switch = true;
        if (!this._isCanClick) return;
        this.clickD = true;
        Laya.stage.on(Event.MOUSE_UP, this, this.OnClickUp);
        Laya.stage.on(Event.MOUSE_MOVE, this, this.OnSlider);
        this._mousePos.x = MouseManager.instance.mouseX;
        // this._mousePos.y = MouseManager.instance.mouseY;
        this.MouseD();
    }

    private MouseD() {
        MainRoleManager.Instance.SetPlayerPos(this._mousePos.x);
        MainRoleManager.Instance.CreateBullet();
    }
    public OnClickUp(e?: Event) {
        if (!this._isCanClick || !this._switch) return;
        //  if (e.target instanceof Laya.UIComponent) return;
        if (!this.clickD) return;
        this.MouseU();
    }

    public OffMouseEvent() {
        Laya.stage.off(Event.MOUSE_DOWN, this, this.OnClickDown);
        Laya.stage.off(Event.MOUSE_UP, this, this.OnClickUp);
        Laya.stage.off(Event.MOUSE_OUT, this, this.OnClickUp);
    }

    private MouseU() {
        this.clickD = false;
        Laya.stage.off(Event.MOUSE_MOVE, this, this.OnSlider);
        Laya.stage.off(Event.MOUSE_UP, this, this.OnClickUp);
     //   MainRoleManager.Instance.StopCreate();

    }
    private _mousePos: Vector3 = Mathf.VecZero;
    private tempMouse = Mathf.VecZero;
    OnSlider(e: Event) {
        this.tempMouse.x = MouseManager.instance.mouseX;
        // this.tempMouse.y = MouseManager.instance.mouseY
        let offestX = this.tempMouse.x - this._mousePos.x;

        //     let offestZ = this.tempMouse.y - this._mousePos.y;
        if (offestX >= 0) offestX = offestX < 20 ? offestX : 20;
        if (offestX < 0) offestX = offestX > -20 ? offestX : -20;

        MainRoleManager.Instance.SliderPlayer(-offestX);
        this._mousePos.x = this.tempMouse.x;
        this._mousePos.y = this.tempMouse.y;
    }
    //舞台是否接受鼠标事件
    private clickCount = 0;
    public OpenInput() {
        this.clickCount++;
        if (this.clickCount > 1) return;
        this._isCanClick = true;
    }
    public CloseInput() {
        this.clickCount--;
        if (this.clickCount > 0) return;
        this._isCanClick = false;

        this.clickD = false;
        // this.MouseU();
    }

    //输入总开关
    public set _Switch(value) {
        this._switch = value;
    }

}
