// 武将羁绊表
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 costCoin: number;
}

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

    arr.forEach(o => {
        o.attributes = parseAttribute(o.attribute);
        o.hids = parseNumberList(o.memberId);
        friendShips.set(o.shipId + '_' + o.level, o);
        let fiendShipHidAandId = friendShipHidAandIds.get(o.shipId);
        if (!fiendShipHidAandId || fiendShipHidAandId.level < o.level)
            friendShipHidAandIds.set(o.shipId, {actorId: o.actorId, level: o.level});
    });
    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
}