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


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 killCount = 0;


    public istry = false;;
    public equipId: number = 1;
    public SetTheme() {

    }



    public SetTryOutById(id) {
        // if (id == this.equip_gun) return false;
        // this.istry = true;
        // let ids = CfgManager.Instance.GetThemeGun(id)["gun_id"];
        // this.eneny_gun_id = CfgManager.Instance.GetGun(ids)["id"];
        // return true;
    }


    static tower_reward: number;
    static block_reward: number;
    static enemy_reward: number;
    public static SetGlobe() {
        PlayerManager.tower_reward = Number(CfgManager.Instance.GetGlobe("tower_reward"));
        PlayerManager.block_reward = Number(CfgManager.Instance.GetGlobe("block_reward"));
        PlayerManager.enemy_reward = Number(CfgManager.Instance.GetGlobe("enemy_reward"));
        //PlayerManager.treasure_golds = GameTool.SpliteString(CfgManager.Instance.GetGlobe("treasure_golds"));
        //PlayerManager.instance.equipId = Math.random() > 0.5 ? 1 : 2;
    }

    /**
      * 玩家属性
      */
    static bulletAttack = 1;//攻击力
    static bulletTime = 0.3;//子弹的间隔时间
    powerLevel: number;
    rateLevel: number;
    public SetPlayer() {
        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.SetArr();
    }
    SetArr() {
        let power = UserModel.Instance.CurModel.GetUserAttr(UserAttr.attrs)["power"];
        if (power) this.powerLevel = Number(power);
        else this.powerLevel = 1;

        let al = CfgManager.Instance._atk.length
        let t = this.powerLevel > al ? al : this.powerLevel;
        PlayerManager.bulletAttack = CfgManager.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 = CfgManager.Instance._rate.length
        let t1 = this.rateLevel > al1 ? al1 : this.rateLevel;
        PlayerManager.bulletTime = CfgManager.Instance.GetRate(t1)["rate"];
    }

    public GradePower(level?, chanel?, count?, callback?) {
        // let count = 0;
        // if (chanel == "money") {
        //     let al = CfgManager.Instance._atk.length
        //     let t = level > al ? al : level;
        //     count = CfgManager.Instance.GetAtk(t)["cost"];
        // }
        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();
                // this.SetArr();
                if (callback) callback(true);
            },
            (data) => {
                // if (callback) callback();
                if (callback) callback(false);
            });
    }
    public GradeRate(level?, chanel?, count?, callback?) {
        // let count = 0;
        // if (chanel == "money") {
        //     let al = CfgManager.Instance._rate.length
        //     let t = level > al ? al : level;
        //     count = CfgManager.Instance.GetRate(t)["cost"];
        // }
        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();
                //this.SetArr();
                if (callback) callback(true);
            },
            (data) => {
                // if (callback) callback();
                if (callback) callback(false);
            });
    }

}



