import { Application, ChannelService, HandlerService, } from 'pinus';
import { ActivityModel, ActivityModelType } from '../../../db/Activity';
import { ServerlistModel } from '../../../db/Serverlist';
import { reloadResources } from '../../../pubUtils/data';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';
import { taflush } from '../../../services/sdkService';
import { ActivityInRemote } from '../../../domain/activityField/activityField';
import { errlogger } from '../../../util/logger';
import { ActivityGroupModel } from '../../../db/ActivityGroup';
import { deleteActivities, loadActivities, saveActivitiesToGroup, updateActivities, _getActivityById, _getActivitiesByType, _getActivities, _getActivitiesByServerId, saveGroupToServer, addServerToGroup } from '../../../services/activity/activityRemoteService';
import { saveActivityMemory } from '../../../services/log/memoryLogService';
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 ActivityRemote(app);
}

export class ActivityRemote {

    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 updateActivities(activities: ActivityInRemote[]) {
        try {
            // console.log('******* activities', activities)
            updateActivities(activities);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

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

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

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

    public async saveActivitiesToGroup(groupId: number, activities: number[]) {
        try {
            saveActivitiesToGroup(groupId, activities);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

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

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

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

    public getActivities() {
        try {
            return _getActivities();
        } 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 saveActivityMemory() {
        try {
            return saveActivityMemory();
        } 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}`);
        }
    }
}