// 邮件内容
import {decodeArrayListStr, parseGoodStr, readFileAndParse} from '../util'
import { FILENAME } from '../../consts'
import { RewardInter } from '../interface';

export interface DicLadderDifficultRatio {
    // id
    readonly id: number;
    // 排名
    readonly rank: number;
    // 等级
    readonly level: number;
    // 关卡id
    readonly gkId: number;
    // 头像
    readonly head: number;
    // 形象
    readonly spine: number;
    // 胜利奖励
    readonly randomWinReward: { id: number, count: number, weight: number }[];
    // 失败奖励
    readonly randomFailReward: { id: number, count: number, weight: number }[];
    // 突破奖励
    readonly onceReward: RewardInter[];
    // 战力
    readonly ce: number;
}

export const dicLadderDifficultRatio = new Map<number, DicLadderDifficultRatio>();
export function loadLadderDifficultRatio() {
    dicLadderDifficultRatio.clear();
    
    let arr = readFileAndParse(FILENAME.DIC_LADDER_DIFFICULTRATIO);
    arr.forEach(o => {
        o.gkId = parseInt(o.gkId);
        o.randomWinReward = parseRewardWithWeight(o.randomWinReward);
        o.randomFailReward = parseRewardWithWeight(o.randomFailReward);
        o.onceReward = parseGoodStr(o.onceReward)
        dicLadderDifficultRatio.set(o.id, o);
    });
    arr = undefined;
}

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