import { Application, ChannelService, HandlerService, } from 'pinus';
import { reloadResources } from '../../../pubUtils/data';
import { refundOrderFromRedisPub, settleOrderFromRedisPub } from '../../../services/orderService';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';
import { taflush } from '../../../services/sdkService';
import { errlogger } from '../../../util/logger';
import { setApiIsClose } from '../../../services/chatService';
import { setHiddenData } from '../../../services/dataService';
import { setKvToMemory } from '../../../services/pushService';

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

export class OrderRemote {

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

    private channelService: ChannelService;

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

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

    public async refundOrderFromRedisPub(message: string) {
        try {
            await refundOrderFromRedisPub(message);
        } 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}`);
        }
    }
}