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

export interface DicEquipQuality {
    // id
    readonly id: number;
    // 装备id
    readonly equipId: number;
    // 装备品质
    readonly quality: number;
    // 装备的武将职业大类
    readonly jobClass: number;
    // 装备栏id
    readonly eplaceId: number;
    // 升星的点
    readonly point: number;
    // 一共有多少点
    readonly count: number;
    // 消耗
    readonly consume: RewardInter[];
    // 属性提升
    readonly attribute: {id: number, num: number}[];

}

export const dicEquipQuality = new Map<number, DicEquipQuality>();
export const dicEquipQualityIdByEquipIdAndPoint = new Map<string, number>(); // equipId&point => id

export function loadEquipQuality() {
    dicEquipQuality.clear();
    dicEquipQualityIdByEquipIdAndPoint.clear();
    let arr = readFileAndParse(FILENAME.DIC_EQUIP_QUALITY);
    
    arr.forEach(o => {
        o.consume = parseGoodStr(o.consume);
        o.attribute = parseAttr(o.attribute);
        dicEquipQualityIdByEquipIdAndPoint.set(`${o.equipId}_${o.quality}_${o.point}`, o.id);
        dicEquipQuality.set(o.id, 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
}