// 礼包奖励表
import { decodeArrayListStr, readFileAndParse } from '../util';
import { FILENAME } from '../../consts';
import { RewardParam } from '../../domain/activityField/rewardField';

export interface DicGiftPackage {
    // 礼包id
    readonly id: number;
    // 礼包类型 GIFT_PACKAGE_TYPE 1.全部；2.选择；3.随机
    readonly type: number;
    // 个数 type=1时，无效
    readonly count: number;
    // 礼包内容
    readonly reward: Array<RewardParam>;
    // 描述
    readonly des: string;

}

export const dicGiftPackage = new Map<number, DicGiftPackage>();
export function loadGiftPackage() {
    dicGiftPackage.clear();
    let arr = readFileAndParse(FILENAME.DIC_GIFT_PACKAGE);

    arr.forEach(o => {
        o.reward = parseAttr(o.reward);
        dicGiftPackage.set(o.id, o);
    });
    arr = undefined;
}

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