// 物品表
import { readFileAndParse, parseGoodStr, decodeArrayListStr, } from '../util'
import { FILENAME, } from '../../consts'
import { RewardInter } from '../interface';
const _ = require('lodash');

export interface DicArtifactLvPlan { 
    // 方案id
    planId: number;
    // 等级
    lv: number;
    // 升到这级需要消耗什么，id&count
    consumes: RewardInter[];
    // 这级的属性
    attr: {id: number, attr: number}[];
}

type KeysEnum<T> = { [P in keyof Required<T>]: true };
const DicArtifactLvPlanKeys: KeysEnum<DicArtifactLvPlan> = {
    planId: true,
    lv: true,
    consumes: true,
    attr: true,
}
export const dicArtifactLvPlan = new Map<number, Map<number, DicArtifactLvPlan>>(); // planId => lv => dic

export function loadArtifactLvPlan() {
    dicArtifactLvPlan.clear();
    
    let arr = readFileAndParse(FILENAME.DIC_ARTIFACT_LV_PLAN);
    arr.forEach(o => {
        o.consumes = parseGoodStr(o.consumes);
        o.attr = parseAttr(o.attr);
        if(!dicArtifactLvPlan.has(o.planId)) {
            dicArtifactLvPlan.set(o.planId, new Map());
        }
        dicArtifactLvPlan.get(o.planId)?.set(o.lv, _.pick(o, Object.keys(DicArtifactLvPlanKeys)));
    });

    arr = undefined;
}

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