// 武将表

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 initialStar: number;
    // 初始星级
    readonly initialColorStar: 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;
    // 天赋树id
    readonly talentId: number;
    // 是否是ur武将
    readonly urType: number;
    // 武将id
    readonly actorId: number;
}

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, initialStar: true, initialColorStar: true, pieceCount: true, baseAbilityArr: true, baseAbilityUpArr: true, initialSkin: true, recruit: true, face_id: true, talentId: true, urType: true, actorId: true };
export const dicHero = new Map<number, DicHero>();
export function loadHero() {
    dicHero.clear();
    
    let arr = readFileAndParse(FILENAME.DIC_HERO);
    arr.forEach(o => {
        o.baseAbilityArr = parseBaseAbilityArr(o);
        o.baseAbilityUpArr = parseBaseAbilityUpArr(o);
        o.recruit = parseInt(o.recruit) == 1;
        if(o.quality == 4) {
            o.initialStar = 6;
            o.initialColorStar = o.initialStars - 6;
        } else {
            o.initialStar = o.initialStars;
            o.initialColorStar = 0;
        }
        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
}