// 镇念塔表
import {parseGoodStr, decodeArrayListStr, readFileAndParse, parseNumberList} from '../util'
import { FILENAME } from '../../consts';
import { RewardInter } from '../../pubUtils/interface';

export interface DicTower {
    // 镇念塔层数
    readonly towerFloor: number;
    // 包含关卡
    readonly warArray: Array<number>;
    // 层数奖励
    readonly reward: Array<RewardInter>;
    // 收集奖励
    readonly rewardOfcollect: Array<RewardInter>;
    // 是否可以一键跳过
    readonly canSkip: boolean;
}


export const dicTower = new Map<number, DicTower>();
export function loadTower() {
    dicTower.clear();
    
    let arr = readFileAndParse(FILENAME.DIC_TOWER);

    arr.forEach(o => {
        o.warArray = parseNumberList(o.warArray);
        o.reward = parseGoodStr(o.reward);
        o.rewardOfcollect = parseGoodStrOfCollect(o.rewardOfcollect);
        o.canSkip = o.canSkip == 1;
        dicTower.set(o.towerFloor, o);
    });
    arr = undefined;
}

function parseGoodStrOfCollect(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(parseFloat(count))) {
            throw new Error('data table format wrong');
        }
        result.push({id: parseInt(id), count: parseFloat(count)});
    } 
    return result
}