import { DispatcherManager, EventType } from "./DispatcherManager";
import { NetInfoManager } from "./NetInfoManager";
import UserModel, { UserAttr } from "../Net/UserModel";
import {GameTool} from "../Util/GameTool";
import NetManager, { PROTOCOLS } from "../Net/NetManager";

import Stage = Laya.Stage;
import TiledMap = Laya.TiledMap;
import Rectangle = Laya.Rectangle;
import Browser = Laya.Browser;
import Handler = Laya.Handler;
import Stat = Laya.Stat;
import WebGL = Laya.WebGL;

export class PlayerManager {
    private static instance: PlayerManager;
    public static get Instance(): PlayerManager {
        return PlayerManager.instance || (PlayerManager.instance = new PlayerManager());
    }
    public Play_Id;
    public stage: number = 1;
    public equipId: number = 1;

    public static GlobalMessage() {
        PlayerManager.tower_reward = Number(NetInfoManager.Instance.GetGlobal("tower_reward"));
        PlayerManager.block_reward = Number(NetInfoManager.Instance.GetGlobal("block_reward"));
        PlayerManager.enemy_reward = Number(NetInfoManager.Instance.GetGlobal("enemy_reward"));
    }

    /**
      * 玩家属性
      */
    static bulletAttack = 1;//攻击力
    static bulletTime = 0.3;//子弹的间隔时间
    powerLevel: number;
    rateLevel: number;
    public InitPlayer() {
        var usermodel = UserModel.Instance.CurModel;
        PlayerManager.instance.stage = (Number(usermodel.GetUserAttr(UserAttr.stage)) + 1);
        PlayerManager.instance.equipId = Number(usermodel.GetUserAttr(UserAttr.equip_themes)["1"]);
        this.SetInfo();
    }

    public SpeedUp(level?, chanel?, count?, callback?) {
        let param = [{ "t": UserModel.Instance.CurModel.ServerTime }, { "attr": "rate" }, { "level": level }, { "channel": chanel }, { "amount": count }];
        NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.UpgradeAttr, param,
            (data: any) => {
                if (callback) callback(true);
            },
            (data) => {
                if (callback) callback(false);
            });
    }

    static tower_reward: number;
    static block_reward: number;
    static enemy_reward: number;
    SetInfo() {
        let power = UserModel.Instance.CurModel.GetUserAttr(UserAttr.attrs)["power"];
        if (power) this.powerLevel = Number(power);
        else this.powerLevel = 1;

        let al = NetInfoManager.Instance._atk.length
        let t = this.powerLevel > al ? al : this.powerLevel;
        PlayerManager.bulletAttack = NetInfoManager.Instance.GetAtk(t)["atk"];//TODO

        let rate = UserModel.Instance.CurModel.GetUserAttr(UserAttr.attrs)["rate"];
        if (rate) this.rateLevel = Number(rate);
        else this.rateLevel = 1;

        let al1 = NetInfoManager.Instance._rate.length
        let t1 = this.rateLevel > al1 ? al1 : this.rateLevel;
        PlayerManager.bulletTime = NetInfoManager.Instance.GetRate(t1)["rate"];
    }

    /************************************************************************ */
    private tiledMap: TiledMap;
    private mLastMouseX: number = 0;
    private mLastMouseY: number = 0;
    private mX: number = 0;
    private mY: number = 0;
    //创建地图
		private createMap() {
			//创建地图对象
			this.tiledMap = new TiledMap();

			this.mX = this.mY = 0;
			//创建地图，适当的时候调用destory销毁地图
			this.tiledMap.createMap("res/tiledMap/desert.json", new Rectangle(0, 0, Browser.width, Browser.height), new Handler(this, this.completeHandler));
		}
		/**
		 * 地图加载完成的回调
		 */
		private completeHandler(): void {
			console.log("地图创建完成");
			console.log("ClientW:" + Browser.clientWidth + " ClientH:" + Browser.clientHeight);
			Laya.stage.on(Laya.Event.RESIZE, this, this.resize);
			this.resize();
		}

		//鼠标按下拖动地图
		private mouseDown(): void {
			this.mLastMouseX = Laya.stage.mouseX;
			this.mLastMouseY = Laya.stage.mouseY;
			Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
		}

		private mouseMove(): void {
			//移动地图视口
			this.tiledMap.moveViewPort(this.mX - (Laya.stage.mouseX - this.mLastMouseX), this.mY - (Laya.stage.mouseY - this.mLastMouseY));
		}

		private mouseUp(): void {
			this.mX = this.mX - (Laya.stage.mouseX - this.mLastMouseX);
			this.mY = this.mY - (Laya.stage.mouseY - this.mLastMouseY);
			Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
		}

		// 窗口大小改变，把地图的视口区域重设下
		private resize(): void {
			//改变地图视口大小
			this.tiledMap.changeViewPort(this.mX, this.mY, Browser.width, Browser.height);
        }
    /************************************************************************ */       
    
    

    public PowerUp(level?, chanel?, count?, callback?) {
        let param = [{ "t": UserModel.Instance.CurModel.ServerTime }, { "attr": "power" }, { "level": level }, { "channel": chanel }, { "amount": count }];
        NetManager.Instance.HttpPostRequest(PROTOCOLS.protocols.UpgradeAttr, param,
            (data: any) => {
                if (callback) callback(true);
            },
            (data) => {
                if (callback) callback(false);
            });
    }
    
}



