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

export interface DicEquipStar {
    // id
    readonly id: number;
    // 装备id
    readonly equipId: number;
    // 装备星级
    readonly star: number;
    // 装备的武将职业大类
    readonly jobClass: number;
    // 装备栏id
    readonly eplaceId: number;
    // 一共有多少点
    readonly count: number;
    // 主属性
    readonly mainAttr: Map<number, {id: number, num: number}[]>;
    // 次级属性
    readonly subAttr: {id: number, num: number}[];
    // 一次升点消耗
    readonly mainConsume: RewardInter[];
    // 升星消耗
    readonly subConsume: RewardInter[];

}

export const dicEquipStar = new Map<number, DicEquipStar>();
export const dicEquipStarIdByEquipId = new Map<string, number>(); // equipId&point => id

export function loadEquipStar() {
    dicEquipStar.clear();
    dicEquipStarIdByEquipId.clear();
    let arr = readFileAndParse(FILENAME.DIC_EQUIP_STAR);
    
    arr.forEach(o => {
        o.mainConsume = parseGoodStr(o.mainConsume);
        o.subConsume = parseGoodStr(o.subConsume);
        o.mainAttr = parseAttrMap(o.mainAttr);
        o.subAttr = parseAttr(o.subAttr);
        dicEquipStarIdByEquipId.set(`${o.equipId}_${o.star}`, o.id);
        dicEquipStar.set(o.id, o);
    });
    arr = undefined;
}

function parseAttrMap(str: string) {
    let result = new Map<number, {id: number, num: number}[]>();
    if(!str) return result;
    let decodeArr = decodeArrayListStr(str);
    for(let i = 0; i < decodeArr.length; i++) {
        let [id, num] = decodeArr[i];
        if(isNaN(parseInt(id)) || isNaN(parseFloat(num))) {
            throw new Error('data table format wrong');
        }
        result.set(i + 1, [{id: parseInt(id), num: parseFloat(num)}]);
    }
    return result
}

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(parseFloat(num))) {
            throw new Error('data table format wrong');
        }
        result.push({id: parseInt(id), num: parseFloat(num)});
    }
    return result
}