import { readFileAndParse, parseGoodStr, parseNumberList } from '../util'
import { FILENAME } from '../../consts'
type KeysEnum<T> = { [P in keyof Required<T>]: true };
import { RewardInter } from '../interface';
const _ = require('lodash');


export interface DicArmyTrainJuDian {

    readonly id: number;
    readonly trainLv: number;
    // 训练场多个难度组成的gk，每个难度一个warId，而这个gkId指trainGk_info这张表的id
    readonly trainInstances: Array<{hid: number, gkId: number, progress: number}>;
    // 进阶奖励
    readonly jinjieReward: RewardInter[];
    readonly soloRewardRatio: number;
}

const DicArmyTrainJuDianKeys: KeysEnum<DicArmyTrainJuDian> = {
    id: true,
    trainLv: true,
    trainInstances: true,
    jinjieReward: true,
    soloRewardRatio: true
};

export const dicArmyTrainJuDian = new Map<number, DicArmyTrainJuDian>();
export const dicLastGuildTrainIdOfLv = new Map<number, number>(); // 每级试炼场等级的最后据点id
export const dicFirstGuildTrainIdOfLv = new Map<number, number>(); // 每级试炼场等级的最先据点id
export function loadArmyTrainJuDian() {
    dicArmyTrainJuDian.clear();
    dicLastGuildTrainIdOfLv.clear();
    dicFirstGuildTrainIdOfLv.clear();

    let arr = readFileAndParse(FILENAME.DIC_ARMY_TRAIN_JU_DIAN);
    arr.forEach(o => {
        o.trainInstances = o.gkid.split('|').map(element => {
            let arr = element.split('&');
            if(isNaN(parseInt(arr[0])) || isNaN(parseInt(arr[1])) || isNaN(parseInt(arr[2]))) {
                throw new Error(`${o.id} ${element}`)
            }
            return {hid: parseInt(arr[0]), gkId: parseInt(arr[1]), progress: parseInt(arr[2])};
        });

        let heroid = parseNumberList(o.heroid);
        o.trainInstances.forEach(instance => {
            if(heroid.indexOf(instance.hid) == -1) throw new Error(`${o.id} ${instance.hid}`)
        })
        o.jinjieReward = parseGoodStr(o.jinjieReward);
        dicArmyTrainJuDian.set(o.id,_.pick(o, Object.keys(DicArmyTrainJuDianKeys)));

        if(!dicLastGuildTrainIdOfLv.has(o.trainLv)) {
            dicLastGuildTrainIdOfLv.set(o.trainLv, o.id);
        } else {
            if(dicLastGuildTrainIdOfLv.get(o.trainLv) < o.id) {
                dicLastGuildTrainIdOfLv.set(o.trainLv, o.id);
            }
        }

        if(!dicFirstGuildTrainIdOfLv.has(o.trainLv)) {
            dicFirstGuildTrainIdOfLv.set(o.trainLv, o.id);
        } else {
            if(dicFirstGuildTrainIdOfLv.get(o.trainLv) > o.id) {
                dicFirstGuildTrainIdOfLv.set(o.trainLv, o.id);
            }
        }
    });
    arr = undefined;
}