// 武将表

import { ABI_TYPE, FILENAME } from '../../consts'
import { readFileAndParse } from '../util'
const _ = require('lodash');

export interface DicHero {
    // 武将id
    readonly heroId: number;
    // 武将名  
    readonly name: string;
    // 初始品质
    readonly quality: number;
    // 阵营
    readonly camp: number;
    // 兵种大类
    readonly jobClass: number;
    // 兵种初始阶层
    readonly jobid: number;
    // 技能
    readonly skill: number;
    // 武将碎片
    readonly pieceId: number;
    // 初始星级
    readonly initialStars: number;
    // 合成碎片数量
    readonly pieceCount: number;
    // 主属性
    readonly baseAbilityArr:Map<number, number>;
    readonly baseAbilityUpArr:Map<number, number>;
    readonly initialSkin: number;
    // 是否可招募
    readonly recruit: boolean;
    // 武将图标id
    readonly face_id: string;
}

type KeysEnum<T> = { [P in keyof Required<T>]: true };
const DicHeroKeys: KeysEnum<DicHero> = {heroId: true, name: true, quality: true, camp: true, jobClass: true, jobid: true, skill: true, pieceId: true, initialStars: true, pieceCount: true, baseAbilityArr: true, baseAbilityUpArr: true, initialSkin: true, recruit: true, face_id: true};
export const dicMyHeroes = new Array<number>();
export const dicHero = new Map<number, DicHero>();
export function loadHero() {
    dicMyHeroes.splice(0, dicMyHeroes.length);
    dicHero.clear();
    
    let arr = readFileAndParse(FILENAME.DIC_HERO);
    arr.forEach(o => {
        if(o.heroId > 0 && o.heroId <= 300) {
            dicMyHeroes.push(o.heroId);
        }
        o.baseAbilityArr = parseBaseAbilityArr(o);
        o.baseAbilityUpArr = parseBaseAbilityUpArr(o);
        o.recruit = parseInt(o.recruit) == 1;
        dicHero.set(o.heroId, _.pick(o, Object.keys(DicHeroKeys)));
    });
    arr = undefined;
}

function parseBaseAbilityArr(json) {
    let map = new Map<number, number>();
    map.set(ABI_TYPE.ABI_HP, json.hp||0);
    map.set(ABI_TYPE.ABI_ATK, json.atk||0);
    map.set(ABI_TYPE.ABI_DEF, json.def||0);
    map.set(ABI_TYPE.ABI_MDEF, json.mdef||0);
    return map
}

function parseBaseAbilityUpArr(json) {
    let map = new Map<number, number>();
    map.set(ABI_TYPE.ABI_HP, json.hp_up||0);
    map.set(ABI_TYPE.ABI_ATK, json.atk_up||0);
    map.set(ABI_TYPE.ABI_DEF, json.def_up||0);
    map.set(ABI_TYPE.ABI_MDEF, json.mdef_up||0);
    return map
}