import { Application, ChannelService, HandlerService, } from 'pinus';
import { reloadResources } from '../../../pubUtils/data';
import { taflush, treatGuildName } from '../../../services/sdkService';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';
import { errlogger } from '../../../util/logger';
import { setApiIsClose } from '../../../services/chatService';
import { setHiddenData } from '../../../services/dataService';
import { setKvToMemory } from '../../../services/pushService';
import { getGVGConfig, setGVGConfig, setGVGServerGroup } from '../../../services/gvg/gvgService';
import { GVGConfigType } from '../../../db/GVGConfig';
import { catapultHurt, gvgBattleEnd, gvgBattleSeconds, gvgBattleStart, initCatapult, leaveCity } from '../../../services/gvg/gvgBattleService';
import { clearBattleMemory } from '../../../services/gvg/gvgBattleMemory';
import { GVGLeagueType } from '../../../db/GVGLeague';
import { updateLeagueNameMem, updateTeamRoleInfoMem } from '../../../services/gvg/gvgTeamService';
import { resetPeriodTime, setPeriodTime } from '../../../services/gvg/gvgFightService';

export default function (app: Application) {
    new HandlerService(app, {});
    return new GuildRemote(app);
}

export class GuildRemote {

    constructor(private app: Application) {
        this.app = app;
    }

    /**
     * 重载json资源
     */
    public async reloadResources() {
        try {
            reloadResources();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async treatGuildName(content: string) {
        try {
            await treatGuildName(content);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public setServerMainten(serverIds: number[], startTime: number, endTime: number) {
        try {
            setServerMainten(serverIds, startTime, endTime);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public stopServerMainten(serverIds: number[]) {
        try {
            stopServerMainten(serverIds);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public getServerMainten(serverId: number) {
        try {
            return getServerMainten(serverId);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public taflush() {
        try {
            return taflush();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setApiIsClose(isClose: boolean) {
        try {
            setApiIsClose(isClose);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setHiddenData(heroes: number[], goods: number[], refTime: number) {
        try {
            setHiddenData(heroes, goods, refTime);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setKvToMemory(originK: string, originV: string, aesKey: string, aesIV: string, now: number) {
        try {
            setKvToMemory(originK, originV, aesKey, aesIV, now);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setGVGConfig(config: GVGConfigType) {
        try {
            await setGVGConfig(config);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async getGVGConfig() {
        try {
            return getGVGConfig();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setGVGServerGroup() {
        try {
            return setGVGServerGroup();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async gvgBattleStart() {
        try {
            return await gvgBattleStart();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async initCatapult(cityId: number, groupKey: string, leagueCode: string, leagueName: string) {
        try {
            return await initCatapult(cityId, groupKey, leagueCode, leagueName);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async catapultHurt() {
        try {
            return await catapultHurt();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async gvgBattleSeconds() {
        try {
            return await gvgBattleSeconds();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async gvgBattleEnd() {
        try {
            return await gvgBattleEnd();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async clearBattleMemory() {
        try {
            return clearBattleMemory();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async leaveCity(isForce: boolean, roleId: string, serverId: number, guildCode: string, myLeague?: GVGLeagueType) {
        try {
            return leaveCity(isForce, roleId, serverId, guildCode, myLeague);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async updateLeagueNameMem(serverId: number, leagueCode: string, leagueName: string) {
        try {
            return updateLeagueNameMem(serverId, leagueCode, leagueName);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async updateTeamRoleInfoMem(serverId: number, roleId: string, info: { roleName?: string, lv?: number }) {
        try {
            return updateTeamRoleInfoMem(serverId, roleId, info);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public setPeriodTime(startFightTime: number, endFightTime: number) {
        try {
            return setPeriodTime(startFightTime, endFightTime);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public resetPeriodTime() {
        try {
            return resetPeriodTime();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }   
    }
}