// 武将羁绊表
import { decodeArrayListStr, readFileAndParse, parseNumberList } from '../util';
import { FILENAME } from '../../consts';

export interface DicFriendShip {
    // id
    readonly id: number;
    // 羁绊id
    readonly shipId: number;
    // 主武将ID
    readonly actorId: number;
    // 羁绊名称
    readonly name: string;
    // 羁绊等级
    readonly level: number;
    // 羁绊武将ID
    readonly hids: Array<number>;
    // 属性加成
    readonly attributes: Array<{id: number, number: number}>
    // 升到这一级所需的羁绊值
    readonly shipExp: number;
}

export const friendShips = new Map<string, { level: number, shipExp: number }[]>();
export const friendShipsByLv = new Map<string, DicFriendShip>();
export const friendShipsMax = new Map<string, number>();
export function loadFriendShip() {
    friendShips.clear();
    let arr = readFileAndParse(FILENAME.DIC_FRIEND_SHIP);

    arr.forEach(o => {
        o.attributes = parseAttribute(o.attribute);
        o.hids = parseNumberList(o.memberId);
        let key1 = `${o.actorId}_${o.shipId}`;
        let shipSumValue = 0;
        if(!friendShips.has(key1)) {
            friendShips.set(key1, []);
            shipSumValue = 0;
        }
        shipSumValue += o.shipExp;
        friendShips.get(key1).push({ level: o.level, shipExp: shipSumValue });

        if(!friendShipsMax.has(key1) || friendShipsMax.get(key1) < o.level) {
            friendShipsMax.set(key1, o.level);
        }

        let key2 = `${o.actorId}_${o.shipId}_${o.level}`;
        friendShipsByLv.set(key2, o);
    });
    arr = undefined;
}

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