// 关卡表
import {decodeArrayListStr, parseNumberList, readWarJsonFileAndParse} from '../util'

export interface DicWarJson {

    // 关卡id
    readonly warId: number;
    // 武将编号
    readonly actorId: number;
    // 武将名字
    readonly actorName: string;
    // 战场中指向该角色的唯一代码
    readonly dataId: number;
    // 角色属于我方/敌方/友军
    readonly relation: number;
    // 上阵时的方向
    readonly dirction: number;
    // 客户端存入数组的顺序
    readonly outIndex: number;
    // x坐标
    readonly x: number;
    // y坐标
    readonly y: number;
    // 剧本中调用角色时的变量
    readonly var: number;
    // 角色等级
    readonly lv: number;
    // 是否隐藏
    readonly hide: number;
    // 角色使用的AI类型
    readonly initial_ai: number;
    // 属性
    readonly attribute: {id: number, val: number}[];
    // 武将技能
    readonly skill: string;
    // 武将被动技能
    readonly seid: string;
    // 角色星级
    readonly star: number;
    // s动画
    readonly spine: string;
    // boss阶段
    readonly bossStage: number;
    // 敌人是小boss还是大boss，和gateActivityPoint的id相对应
    readonly enemyType: number;
    // 召唤技能的召唤物
    readonly callSkillData: string;
    // 敌军数量
    readonly enemyCount: number;
    // pvp中随机敌军
    readonly randomEnemy: number[];
}

export const dicWarJson = new Map<number, Array<DicWarJson>>();
export function loadWarJson() {
    dicWarJson.clear();
    readWarJsonFileAndParse().forEach(arr => {
    
        let warjson = new Array<DicWarJson>();
        let warid = 0;
        let enemyCount = 0;
        arr.forEach(o => {
            o.attribute = parseAttribute(o.attribute);
            if(o.warId) warid = o.warId;
            if(o.relation == 2) enemyCount++;
        });
        arr.forEach(o => {
            o.randomEnemy = parseNumberList(o.randomEnemy);
            o.enemyCount = enemyCount;
            warjson.push(o);
        });
        dicWarJson.set(warid, warjson);
    })
}

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