// 关卡表
import {decodeArrayListStr, decodeArrayStr, readFileAndParse} from '../util'
import { WAR_RELATE_TABLES, WAR_TYPE } from '../../consts';

export interface DicWar {

    // 关卡id
    readonly war_id: number;
    // 关卡固定奖励
    readonly fixReward: Array<{id: number, count: number}>;
    // 关卡条件奖励
    readonly conditionReward: Array<{id: number, count: number, condition: number}>;
    // 关卡随机奖励
    readonly randomReward: Array<{id: number, count: number, frequency: number}>;
    // 关卡类型
    readonly warType: number;
    // 关卡名
    readonly gk_name: string;
    // 胜利后获得的君主经验
    readonly kingExp: number;
    // 进入战场所需的最低等级
    readonly lvLimited: number;
    // 消耗的体力
    readonly cost: number;
    // 前置关卡
    readonly previousGk: number;
    // 每日任务下的小类型
    readonly dailyType: number;
    // 显示章节名
    readonly detailUIBg: string[];
    // 寻宝匹配json
    readonly dispatchJsonId: number;
    // 寻宝奖励
    readonly jackpotReward: Array<{id: number, weight: number}>;
}

export const dicWar = new Map<number, DicWar>();
export const dicWarPvp = new Array<DicWar>();
export const dicDailyWarByType = new Map<number, DicWar[]>();
export function loadWar() {
    dicWarPvp.splice(0, dicWarPvp.length);
    for(let filename of WAR_RELATE_TABLES) {
        let arr = readFileAndParse(filename);
    
        arr.forEach(o => {
            o.fixReward = parseFixReward(o.fixReward);
            o.conditionReward = parseConditionReward(o.conditionReward);
            o.parseRandomReward = parseRandomReward(o.parseRandomReward);
            o.detailUIBg = parseDetailUIBg(o.detailUIBg);
            o.jackpotReward = parseJackpotReward(o.jackpotReward);
    
            dicWar.set(o.war_id, o);
            if(o.warType == WAR_TYPE.PVP) {
                dicWarPvp.push(o);
            } else if (o.warType == WAR_TYPE.DAILY) {
                if(!dicDailyWarByType.has(o.dailyType)) {
                    dicDailyWarByType.set(o.dailyType, []);
                }
                dicDailyWarByType.get(o.dailyType).push(o);
            }
        });
    }
}

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

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

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

function parseDetailUIBg(str: string = '') {
    let result = new Array<string>();
    if(!str) return result;
    let decodeArr = decodeArrayStr(str, '_');
    return decodeArr;
}

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