import { Framework } from "../../framework/Framework";
import { ItemEnum } from "../common/InterfaceAddEnum";
import { LoginMgr } from "../common/LoginManager";
import { EquipConf } from "../config/EquipConf";
import { EquipstrengthenConf } from "../config/EquipstrengthenConf";
import { Equip, EquipData } from "../data/EquipData";
import { GameEvent } from "../data/GameEvent";

export class EquipManager {
    /**
     * 根据参数获取装备（或奖励装备）
     * @param id 装备id_经验exp_星级star
     * @param count 数量
     * @param isWear 是否穿戴
     */
    static getEquipById(id: string, count: number, isWear: boolean) {
        let conf = EquipConf.data;
        let arr = id.split('_');
        let eConf = conf[arr[0]];
        if (eConf) {
            let lvAdd = this.getEquipLevelByExp(Number(arr[1]));
            let attrAdd = eConf.Stat1.split(':')
            let equipData: Equip = {
                id: id,
                count: count,
                exp: Number(arr[1]),
                star: Number(arr[2]),
                wear: isWear,
                type: ItemEnum.equip,
                fightForce: (1 + lvAdd.atkMod) * attrAdd[1],
                conf: eConf
            }
            return equipData;
        }
        return null;
    }

    /**
     * 根据参数获取装备（或奖励装备）
     * @param id 装备id
     * @param exp 经验
     */
    static getEquipLevelByExp(exp: number) {
        let strengConf = EquipstrengthenConf.data;
        let data = [];
        for (const key in strengConf) {
            if (Object.prototype.hasOwnProperty.call(strengConf, key)) {
                const element = strengConf[key];
                data.push(element);
            }
        }
        data.sort((a, b) => {
            return Number(a.id) < Number(b.id) ? -1 : 1;
        });
        let nowConf = null
        for (let index = 0; index < data.length; index++) {
            const element = data[index];
            if (exp < element.NeedExp) {
                nowConf = element;
                break;
            }
        }
        return nowConf;
    }

    static getEquipNextLevel(exp: number) {
        let strengConf = EquipstrengthenConf.data;
        let data = [];
        for (const key in strengConf) {
            if (Object.prototype.hasOwnProperty.call(strengConf, key)) {
                const element = strengConf[key];
                data.push(element);
            }
        }
        data.sort((a, b) => {
            return Number(a.id) < Number(b.id) ? -1 : 1;
        });
        let nowConf = null
        for (let index = 0; index < data.length; index++) {
            const element = data[index];
            if (exp < element.NeedExp) {
                nowConf = element;
                break;
            }
        }
        return strengConf[String(nowConf.Id + 1)];
    }

    //获取阵营穿戴装备
    static getEquipWearRaceGroup() {
        let equips = {};
        for (let x = 1; x <= 4; x++) {
            for (let y = 1; y <= 4; y++) {
                equips[x] = {};
                equips[x][y] = 0;
            }
        }
        let data = EquipData.bagData;
        for (const key in data) {
            if (Object.prototype.hasOwnProperty.call(data, key)) {
                const element = data[key];
                if (element.wear) {
                    equips[element.conf['Race']][element.conf['Slot']] = element;
                }
            }
        }
        return equips;
    }

    //获取阵营装备(分类)
    static getEquipRaceSlotGroup(race) {
        let equips = { 1: [], 2: [], 3: [], 4: [] };
        let data = EquipData.bagData;
        for (const key in data) {
            if (Object.prototype.hasOwnProperty.call(data, key)) {
                const element = data[key];
                if (!element.wear && element.conf['Race'] == race) {
                    equips[element.conf['Slot']].push(element);
                }
            }
        }
        return equips;
    }

    //获取阵营装备（全）
    static getEquipRaceSlotAllGroup(race) {
        let equips = []
        let data = EquipData.bagData;
        for (const key in data) {
            if (Object.prototype.hasOwnProperty.call(data, key)) {
                const element = data[key];
                if (!element.wear && element.conf['Race'] == race) {
                    equips.push(element);
                }
            }
        }
        return equips;
    }

    static updateEquips(race, slot) {

    }

    //消息-穿戴装备
    static sendWearMsg(args: { eid: string, slot: number, race: number }, callback) {
        LoginMgr.sendPost('equip', 'wear', (data) => {
            console.log(data);
            let wearData = this.getEquipWearRaceGroup()[args.race][args.slot];
            if (wearData && wearData.id) {
                EquipData.removeDataByKey(wearData.id + '_' + args.slot);
            }
            if (data.race_equip) {
                for (const slot in data.race_equip) {
                    if (Object.prototype.hasOwnProperty.call(data.race_equip, slot)) {
                        if (Number(slot) == args.slot) {
                            const eid = data.race_equip[slot];
                            if (eid != 0) {
                                let equip = this.getEquipById(eid, 1, true);
                                if (equip) {
                                    EquipData.addDataByKey(eid + '_' + args.race, equip);
                                }
                            }
                        }
                    }
                }
            }
            if (data.equip) {
                this.parseDataByServer(data.equip);
            }
            Framework.event.fireEvent(GameEvent.EquipWearChange, args.race);
            callback();
        }, args)
    }

    //消息-升级装备
    static sendUpdateMsg(args: { eat_eids: {}, slot: number, race: number }, callback) {
        LoginMgr.sendPost('equip', 'strengthen', (data) => {
            console.log(data);
            let wearData = this.getEquipWearRaceGroup()[args.race][args.slot];
            if (wearData && wearData.id) {
                EquipData.removeDataByKey(wearData.id + '_' + args.slot);
            }
            if (data.race_equip) {
                for (const slot in data.race_equip) {
                    if (Object.prototype.hasOwnProperty.call(data.race_equip, slot)) {
                        if (Number(slot) == args.slot) {
                            const eid = data.race_equip[slot];
                            if (eid != 0) {
                                let equip = this.getEquipById(eid, 1, true);
                                if (equip) {
                                    EquipData.addDataByKey(eid + '_' + args.race, equip);
                                }
                            }
                        }
                    }
                }
            }
            if (data.equip) {
                this.parseDataByServer(data.equip);
            }
            Framework.event.fireEvent(GameEvent.EquipWearChange, args.race);
            callback();
        }, args)
    }


    static setData(bagData, equipData) {
        EquipData.bagData = {};
        for (const key in bagData) {
            if (Object.prototype.hasOwnProperty.call(bagData, key)) {
                const element = bagData[key];
                let equip = EquipManager.getEquipById(key, element, false);
                if (equip) {
                    EquipData.bagData[key] = equip;
                }
            }
        }

        let conf = EquipConf.data;
        for (const race in equipData) {
            if (Object.prototype.hasOwnProperty.call(equipData, race)) {
                const element = equipData[race];
                for (const key in element) {
                    if (Object.prototype.hasOwnProperty.call(element, key)) {
                        const eid = element[key];
                        if (eid != 0) {
                            let equip = EquipManager.getEquipById(eid, 1, true);
                            if (equip) {
                                EquipData.bagData[key + '_' + race] = equip;
                            }
                        }
                    }
                }
            }
        }
    }

    static parseDataByServer(serverData) {
        let data = EquipData.bagData;
        for (const key in serverData) {
            if (Object.prototype.hasOwnProperty.call(serverData, key)) {
                const element = serverData[key];
                if(data[key]){
                    if(element > 0){
                        let equip = EquipManager.getEquipById(key, element, false);
                        if (equip) {
                            data[key] = equip;
                        }
                    }else{
                        delete data[key];
                    }
                }else{
                    let equip = EquipManager.getEquipById(key, element, false);
                    if (equip) {
                        data[key] = equip;
                    }
                }
            }
        }
        EquipData.bagData = data;
    }

}
