// 地玉石表
import { readFileAndParse, parseGoodStr, decodeArrayListStr } from '../util'
import { FILENAME } from '../../consts';
import { RewardInter } from '../interface';

export interface DicStone {
    // 物品id
    readonly good_id: number;
    // 地玉石名
    readonly name: string;
    // 装备栏id
    readonly eplaceId: number;
    // itid
    readonly itid: number;
    // 地玉石阶
    readonly lv: number;
    // 地玉石品质
    readonly quality: number;
    // 合成消耗
    readonly composeMaterial: RewardInter[];
    // 属性提升
    readonly attribute: {id: number, num: number}[];
}


export const dicStone = new Map<number, DicStone>();
export function loadStone() {
    dicStone.clear();
    
    let arr = readFileAndParse(FILENAME.DIC_STONE);

    arr.forEach(o => {
        o.composeMaterial = parseGoodStr(o.composeMaterial);
        o.attribute = parseAttr(o.attribute);
        dicStone.set(o.good_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
}