// 时装表
import { decodeArrayListStr, readFileAndParse } from '../util';
import { FILENAME } from '../../consts';
const _ = require('lodash');

export interface DicFashions {
    // 时装id
    readonly id: number;
    // 皮肤装名
    readonly name: string;
    // 全局加成
    readonly globalAttr: Array<{id: number, number: number}>;
    // 即武将表里的heroId
    readonly heroId: number;
    // 原武将id，300以内的武将id
    readonly actorId: number;
}

type KeysEnum<T> = { [P in keyof Required<T>]: true };
const DicFashionsKeys: KeysEnum<DicFashions> = { id: true, name: true, globalAttr: true, heroId: true, actorId: true};

export const dicFashions = new Map<number, DicFashions>();
export const dicFashionsByHeroId = new Map<number, DicFashions>();
export function loadFashions() {
    dicFashions.clear();
    dicFashionsByHeroId.clear();
    let arr = readFileAndParse(FILENAME.DIC_FASHIONS);

    arr.forEach(o => {
        o.id = o.goodId;
        o.globalAttr = parseAttr(o.globalAttr);
        o.actorAttr = parseAttr(o.actorAttr);
        dicFashions.set(o.id, _.pick(o, Object.keys(DicFashionsKeys)));
        dicFashionsByHeroId.set(o.heroId, _.pick(o, Object.keys(DicFashionsKeys)));
    });
    arr = undefined;
}

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