
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 { MathTool } from "../Util/MathTool";
import {MainRoleManager} from "../Manager/MainRoleManager";

import MovieClip = Laya.MovieClip;
import Animation = Laya.Animation;
import Stage = Laya.Stage;
import Rectangle = Laya.Rectangle;
import Loader = Laya.Loader;
import Browser = Laya.Browser;
import WebGL = Laya.WebGL;

import EventData = Laya.EventData;
import Skeleton  = Laya.Skeleton;
import Templet   = Laya.Templet;
import Sprite    = Laya.Sprite;
import Handler   = Laya.Handler;
import Stat      = Laya.Stat;
import Tween     = Laya.Tween;


export  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 SWFPath: string = "res/swf/dragon.swf";

    private MCWidth: number = 318;
    private MCHeight: number = 406;

    private createMovieClip(): void {
        var mc: MovieClip = new MovieClip();
        mc.load(this.SWFPath);

        mc.x = (Laya.stage.width - this.MCWidth) / 2;
        mc.y = (Laya.stage.height - this.MCHeight) / 2;

        Laya.stage.addChild(mc);
    }
    /****************************************************** */


    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.SetModelPos(this._mousePos.x);
        MainRoleManager.Instance.CreateShoot();
    }
    public OnClickUp(e?: Event) {
        if (!this._isCanClick || !this._switch) return;
        //  if (e.target instanceof Laya.UIComponent) return;
        if (!this.clickD) return;
        this.MouseU();
    }

    /********************************************************** */
    private createAnimation(): void {
        var ani: Animation = new Animation();
        ani.interval = 30; // 设置播放间隔（单位：毫秒）
        ani.index = 1; // 当前播放索引
        ani.play(); // 播放图集动画
        // 获取动画的边界信息
        var bounds: Rectangle = ani.getGraphicBounds();
        ani.pivot(bounds.width / 2, bounds.height / 2);

        ani.pos(Laya.stage.width / 2, Laya.stage.height / 2);
        Laya.stage.addChild(ani);
    }
    /********************************************************** */

    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 = MathTool.VecZero;
    private tempMouse = MathTool.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.PlayerSlip(-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;
    }

    /********************************************************** */
    private mAniPath:string;
    private mStartX:number = 400;
    private mStartY:number = 500;
    private mFactory:Templet;
    private mActionIndex:number = 0;
    private mCurrIndex:number = 0;
    private mArmature:Skeleton;
    private mCurrSkinIndex:number = 0;
    private mFactory2:Templet;
    private mLabelSprite:Sprite;

    private play():void
		{
			this.mCurrIndex++;
			if (this.mCurrIndex >= this.mArmature.getAnimNum())
			{
				this.mCurrIndex = 0;
			}
			this.mArmature.play(this.mCurrIndex,false);
			
		}
		
		private onEvent(e):void
		{
			var tEventData:EventData = e as EventData;
			Laya.stage.addChild(this.mLabelSprite);
			this.mLabelSprite.x = this.mStartX;
			this.mLabelSprite.y = this.mStartY;
			this.mLabelSprite.graphics.clear();
			this.mLabelSprite.graphics.fillText(tEventData.name, 0, 0, "20px Arial", "#ff0000", "center");
		}
    /********************************************************** */


}
