// 奇遇表
import { parseGoodStr, decodeArrayStr, readFileAndParse, parseNumberList } from '../util'
import { FILENAME } from '../../consts'
import { RewardInter } from '../../pubUtils/interface';

interface SuitLevel {
    readonly min: number;
    readonly max?: number;
}

export interface DicEvent {
    // 事件id
    readonly eventID: number;
    // 事件类型
    readonly eventType: number;
    // 事件品质
    readonly quality: number;
    // 事件名
    readonly name: string;
    // 胜利奖励
    readonly winReward: Array<RewardInter>;
    // 失败奖励
    readonly loseReward: Array<RewardInter>;
    // 适用等级
    readonly suitLevel: SuitLevel;
    // 关联关卡id
    readonly warId: number;
    // 权重
    readonly weight: number;
    // 移动点
    readonly movePointArray: Array<number>;
    // 必须通过的关卡
    readonly previousGk: number;

}

export const dicEvent = new Map<number, DicEvent>();
export const dicEventList = new Array<DicEvent>();

export function loadEvent () {
    dicEvent.clear();
    dicEventList.splice(0, dicEventList.length);
    let arr = readFileAndParse(FILENAME.DIC_EVENT);
    
    arr.forEach(o => {
        o.winReward = parseGoodStr(o.winReward);
        o.loseReward = parseGoodStr(o.loseReward);
        o.suitLevel = parseSuitLevel(o.suitLevel);
        o.movePointArray = parseNumberList(o.movePointArray);
        dicEventList.push(o);
        dicEvent.set(o.eventID, o);
    });
    arr = undefined;
}


function parseSuitLevel(str: string) {
    let decodeArr = decodeArrayStr(str, '&');
    let [min, max] = decodeArr;
    let suitLevel: SuitLevel;
    if(isNaN(parseInt(min)) || isNaN(parseInt(max))) throw new Error('data table format wrong');
    suitLevel = ({min: parseInt(min), max: parseInt(max)});
    return suitLevel;
}