import { Framework } from "../../framework/Framework";
import { AttrBaseData, AttrConf, AttrEnum, ItemEnum, Role, RoleServer } from "../common/InterfaceAddEnum";
import { LoginMgr } from "../common/LoginManager";
import { FateattridConf } from "../config/FateattridConf";
import { FateConf } from "../config/FateConf";
import { RolegradeConf } from "../config/RolegradeConf";
import { RoleData } from "../data/RoleData";
import { RoleConf } from "../ui/tower/conf/RoleConf";
import { EquipManager } from "./EquipManager";

//角色管理器
export class RoleManager {
    //抽一个英雄
    static getNewRole() {
        LoginMgr.sendPost('tavern', 'get', (data) => {
            console.log(data);
        }, {})
        return false;
    }

    static setData(data: { [id: string]: RoleServer }): void {
        let roles = {}
        let rConf = RoleConf.data;
        if (data) {
            for (const id in data) {
                if (data.hasOwnProperty(id)) {
                    if (rConf[id]) {
                        let role: Role = <Role>data[id];
                        role.conf = rConf[id];
                        role.type = ItemEnum.role;
                        role.attr = this.getRoleAttr(data[id]);
                        roles[data[id].id] = role;
                        // console.log(`ID: ${id}, Name: ${role.name}, Level: ${role.level}`);
                    }
                }
            }
        }
        RoleData.setData(roles);
    }

    static getRoleAttr(role): AttrBaseData {
        let attrs:AttrBaseData = {
            [AttrEnum.attack]: 0,
            [AttrEnum.defence]: 0,
            [AttrEnum.hp]: 0,
            [AttrEnum.speed]: 0,
            [AttrEnum.broken]: 0,
            [AttrEnum.power]: 0,
            [AttrEnum.hp_p]: 0,
            [AttrEnum.attack_p]: 0,
            [AttrEnum.defence_p]: 0,
            [AttrEnum.hit]: 0,
            [AttrEnum.miss]: 0,
            [AttrEnum.crite]: 0,
            [AttrEnum.decrite]: 0,
            [AttrEnum.critedamage]: 0,
            [AttrEnum.adddamage]: 0,
            [AttrEnum.dedamage]: 0,
            [AttrEnum.suck]: 0,
            [AttrEnum.thorns]: 0,
            [AttrEnum.block]: 0
        };
        //星级
		let gradeConf = RolegradeConf.data[String(role['grade'])];
        let roleConf = RoleConf.data[String(role['id'])]
        let atkNum = roleConf['Damage'];
		atkNum = atkNum * gradeConf[`LvAttrMod${roleConf['Quality']}`];
        attrs[AttrEnum.attack] += Number(atkNum);
        //装备
        let equipData = EquipManager.getEquipWearRaceGroup()[roleConf['Race']];
        for (const key in equipData) {
            if (Object.prototype.hasOwnProperty.call(equipData, key)) {
                const element = equipData[key];
                if (element) {
                    for (let index = 1; index <= 2; index++) {
                        let Stat = element.conf['Stat' + index];
                        if (Stat != 0) {
                            let str = Stat.split(':');
                            attrs[str[0]] += Number(str[1]);
                        }
                    }
                }
            }
        }
        //羁绊
        return attrs;
    }

    //英雄升星
    static sendRoleAdvance(args: { hid: number }, callback) {
        LoginMgr.sendPost('role', 'grade_up', (data) => {
            console.log(data);
            let rData = RoleData.getRoleDataByID(args.hid);
            rData.grade = data.grade;
            RoleData.setRoleDataByID(args.hid, rData)
            if (data.awards) {
                Framework.unionManager.parseServerAwards(data.awards);
            }
            callback();
        }, args)
    }

    static sendFateLevelUp(args: { id: number }, callback) {
        LoginMgr.sendPost('role', 'fate_level_up', (data) => {
            console.log(data);
            this.setFateData(data.fate);
            if (data.awards) {
                Framework.unionManager.parseServerAwards(data.awards);
            }
            callback();
        }, args)
    }

    static getRolesRaceGroup() {
        let roles = {};
        let roleConf = RoleConf.data
        for (const rId in roleConf) {
            if (Object.prototype.hasOwnProperty.call(roleConf, rId)) {
                const element = roleConf[rId];
                let rData = RoleData.getRoleDataByID(rId);
                if (roles[element.Race]) {
                    if (rData) {
                        roles[element.Race].push(rData);
                    } else {
                        roles[element.Race].push({ conf: element });
                    }
                } else {
                    roles[element.Race] = [];
                    if (rData) {
                        roles[element.Race].push(rData);
                    } else {
                        roles[element.Race].push({ conf: element });
                    }
                }
            }
        }
        for (const key in roles) {
            if (Object.prototype.hasOwnProperty.call(roles, key)) {
                const element = roles[key];
                element.sort((a, b) => {
                    return Number(a.conf.Id) < Number(b.conf.Id) ? -1 : 1;
                });
            }
        }
        return roles;
    }

    static getRoleById(rId: number) {
        let roleConf = RoleConf.data[rId]
        if (roleConf) {
            let rData = RoleData.getRoleDataByID(rId);
            if (rData) {
                return rData;
            } else {
                return { conf: roleConf, type: ItemEnum.role };
            }
        }
        return null;
    }


    static setFateData(data: { [key: string]: number }) {
        let fateData = {};
        let conf = FateConf.data;
        let atteConf = FateattridConf.data;
        for (const key in conf) {
            if (Object.prototype.hasOwnProperty.call(conf, key)) {
                const element = conf[key];
                fateData[key] = {
                    id: key,
                    conf: element,
                    level: 0
                };
                if (data[key]) {
                    fateData[key].level = data[key];
                }
            }
        }
        RoleData.fate = fateData;
    }
}