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';

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}`);
        }
    }
}