// 装备强化表
import { readFileAndParse, decodeArrayListStr } from '../util'
import { FILENAME } from '../../consts'

export interface DicEquipStrengthAttr {
    // id
    readonly id: number;
    // 装备id
    readonly equipId: number; 
    // 等级
    readonly lv: number;
    // 属性
    readonly attr: {id: number, num: number}[];
}

export const dicEquipStrengthAttr = new Map<string, DicEquipStrengthAttr>();
export function loadEquipStrengthAttr() {
    dicEquipStrengthAttr.clear();
    let arr = readFileAndParse(FILENAME.DIC_EQUIP_STRENGTH_ATTR);
    
    arr.forEach(o => {
        o.attr = parseAttr(o.attr);
        dicEquipStrengthAttr.set(`${o.equipId}_${o.lv}`, o);
    });
    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
}