// 时装表
import { decodeArrayListStr, readFileAndParse } from '../util';
import { FILENAME } from '../../consts';

export interface DicFashions {
    // 时装id
    readonly id: number;
    // 皮肤装名
    readonly name: string;
    // 指向heroSkill表
    readonly skillId: number;
    // 全局加成
    readonly globalAttr: Array<{id: number, number: number}>;
    // 单体加成
    readonly actorAttr: Array<{id: number, number: number}>;
    // 角色id
    readonly actorId: number;
}

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

    arr.forEach(o => {
        o.globalAttr = parseAttr(o.globalAttr);
        o.actorAttr = parseAttr(o.actorAttr);
        dicFashions.set(o.id, o);
    });
    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(parseInt(number))) {
            throw new Error('data table format wrong');
        }
        result.push({id: parseInt(id), number: parseInt(number)});
    } 
    return result
}