import { pinus } from "pinus";
import { GUILD_ACTIVITY_TYPE, MEMORY_LOG_TYPE } from "../../consts";
import { ActivityInRemote } from "../../domain/activityField/activityField";
import { MemComBtlTeam } from "../../domain/battleField/ComBattleTeamField";
import { saveMemoryLog, saveMemoryLogs } from "../../pubUtils/logUtil";
import { getGAIndexInPinus } from "../guildActivity/guildActivityService";

// 将内存打印下来
export async function saveMemory(type: MEMORY_LOG_TYPE) {
    switch (type) {
        case MEMORY_LOG_TYPE.ACTIVITY:
            await _saveActivityMemory();
            break;
        case MEMORY_LOG_TYPE.PVP_SEASON:
            await _savePvpSeasonMemory();
            break;
        case MEMORY_LOG_TYPE.GA_INDEX:
            await _saveGuildActivityIndexMemory();
            break;
        case MEMORY_LOG_TYPE.COMBATTLE:
            await _saveComBattleMemory();
            break;
    }
}

async function _saveActivityMemory() {
    await pinus.app.rpc.activity.activityRemote.saveActivityMemory.broadcast();
}

// activity服
export async function saveActivityMemory() {
    await saveMemoryLogs(pinus.app.getServerId(), [
        { memoryLogField: 'activities', memoryData: treatActivities(pinus.app.get('activities')) },
        { memoryLogField: 'activityByServer', memoryData: treatActivityByServer(pinus.app.get('activityByServer')) },
        { memoryLogField: 'activityByType', memoryData: treatActivityType(pinus.app.get('activityByType')) },
        { memoryLogField: 'groupToServer', memoryData: treatGroupToServer(pinus.app.get('groupToServer')) },
    ]);
}

function treatActivities(activities: Map<number, ActivityInRemote> = new Map()) {
    let result: ActivityInRemote[] = [];
    for(let [_, activity] of activities) {
        result.push(activity);
    }
    return JSON.stringify(result);
}

function treatActivityByServer(activityByServer: Map<number, number[]> = new Map()) {
    let result: { serverId: number, activityIds: number[] }[] = [];
    for(let [ serverId, activityIds ] of activityByServer) {
        result.push({ serverId, activityIds });
    }
    return JSON.stringify(result);
}

function treatActivityType(activityByType: Map<number, Map<number, number[]>> = new Map()) {
    let result: { serverId: number, type: number, activityIds: number[] }[] = [];
    for(let [ serverId, typeToActivity ] of activityByType) {
        for(let [ type, activityIds ] of typeToActivity) {
            result.push({ serverId, type, activityIds });
        }
    }
    return JSON.stringify(result);
}

function treatGroupToServer(groupToServer: Map<number, number[]> = new Map()) {
    let result: { groupId: number, serverIds: number[] }[] = [];
    for(let [groupId, serverIds] of groupToServer) {
        result.push({ groupId, serverIds });
    }
    return JSON.stringify(result);
}

async function _savePvpSeasonMemory() {
    await pinus.app.rpc.battle.battleRemote.savePvpSeasonMemory.broadcast();
    await pinus.app.rpc.connector.connectorRemote.savePvpSeasonMemory.broadcast();
}

export async function savePvpSeasonMemory() {
    let data = { 
        pvpSeasonNum: pinus.app.get('pvpSeasonNum'),
        pvpSeasonStartTime: pinus.app.get('pvpSeasonStartTime'),
        pvpSeasonEndTime: pinus.app.get('pvpSeasonEndTime'),
        pvpSeasonRewardTime: pinus.app.get('pvpSeasonRewardTime'),
        pvpSettleSeasonNum: pinus.app.get('pvpSettleSeasonNum')
    }
    await saveMemoryLog(pinus.app.getServerId(), 'pvpSeason', JSON.stringify(data));
}

async function _saveGuildActivityIndexMemory() {
    await pinus.app.rpc.guild.guildActivityRemote.saveGuildActivityIndexMemory.broadcast();
}

export async function saveGuildActivityIndexMemory() {
    let data = {
        gateActivity: getGAIndexInPinus(GUILD_ACTIVITY_TYPE.GATE_ACTIVITY),
        cityActivity: getGAIndexInPinus(GUILD_ACTIVITY_TYPE.CITY_ACTIVITY),
        raceActivity: getGAIndexInPinus(GUILD_ACTIVITY_TYPE.RACE_ACTIVITY),
    }
    await saveMemoryLog(pinus.app.getServerId(), 'gaIndex', JSON.stringify(data));
}

async function _saveComBattleMemory() {
    await pinus.app.rpc.battle.comBattleRemote.saveComBattleMemory.broadcast();
}

export async function saveComBattleMemory() {
    await saveMemoryLog(pinus.app.getServerId(), 'comBattle', treatTeamMap(pinus.app.get('teamMap')));
}

function treatTeamMap(teamMap: Map<string, MemComBtlTeam> = new Map()) {
    let result: MemComBtlTeam[] = [];
    for(let [_teamCode, team] of teamMap) {
        result.push(team);
    }
    return JSON.stringify(result);
}