// 装备表
import { readFileAndParse, decodeArrayListStr, parseGoodStr } from '../util'
import { FILENAME } from '../../consts'
import { RewardInter } from '../interface';

export interface DicEquip {
    // (key) 装备的id
    readonly id: number;   
    // 装备的位置（武器、衣甲、帽冠、行具）
    readonly eplaceId: number; 
    // 装备名
    readonly name: string; 
    // 匹配的武将的职业的大类
    readonly jobClass: number; 
    // 套装id
    readonly suitId: number; 
    // 属性提升
    readonly attribute: {id: number, num: number}[];
    // 属性成长加成提升
    readonly attributeUp: {id: number, num: number}[];
    // 合成消耗
    readonly composeMaterial: RewardInter[];

}

export const dicEquipById = new Map<number, DicEquip>();
export const dicEquipIdByJobClassAndEplace = new Map<string, number>();
export function loadEquip() {
    dicEquipById.clear();
    dicEquipIdByJobClassAndEplace.clear();
    let arr = readFileAndParse(FILENAME.DIC_EQUIP);
    
    arr.forEach(o => {
        o.attribute = parseAttr(o.attribute);
        o.attributeUp = parseAttr(o.attributeUp);
        o.composeMaterial = parseGoodStr(o.composeMaterial);
        dicEquipById.set(o.id, o);
        dicEquipIdByJobClassAndEplace.set(`${o.jobClass}_${o.eplaceId}`, o.id);
    });
    arr = undefined;
}

function parseAttr(str: string) {
    let result = new Array<{id: number, num: number}>();
    if(!str) return result;
    let decodeArr = decodeArrayListStr(str);
    for(let [id, num] of decodeArr) {
        if(isNaN(parseInt(id)) || isNaN(parseInt(num))) {
            throw new Error('data table format wrong');
        }
        result.push({id: parseInt(id), num: parseInt(num)});
    }
    return result
}