import { Application, HandlerService, } from 'pinus';
import { reloadResources } from '../../../pubUtils/data';
import { UserGuildType } from '../../../db/UserGuild';
import { incServerNum, kickUser } from '../../../services/connectorService';
import { PVPConfigModel, PVPConfigType } from '../../../db/PvpConfig';
import { setDicAuctionTime, setDicGuildActivity } from '../../../services/guildActivity/guildActivityService';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';
import { taflush } from '../../../services/sdkService';
import { errlogger } from '../../../util/logger';
import { setWeek } from '../../../pubUtils/timeUtil';
import { savePvpSeasonMemory } from '../../../services/log/memoryLogService';
import { setApiIsClose } from '../../../services/chatService';
import { setPvpSeasonNum, setPvpSettleSeasonNum } from '../../../services/timeTaskService';
import { setHiddenData } from '../../../services/dataService';
import { setKvToMemory } from '../../../services/pushService';
import { setGVGServerGroup } from '../../../services/gvg/gvgService';

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

export class ConnectorRemote {

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

    public async remoteLogin(uid: string, message?: any) {
        try {
            await kickUser(this.app, uid, message);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setOtherUserGuildSession(params: { roleId: string, userGuild: UserGuildType}[]) {
        try {
            let sessionService = this.app.get('sessionService');
            for(let {roleId, userGuild} of params) {
                let sessions = sessionService.getByUid(roleId);
                if(!!sessions) {
                    sessions.forEach(session => {
                        if(userGuild && userGuild.status == 1) {
                            sessionService.aimportAll(session.id, { guildCode: userGuild?.guildCode, guildAuth: userGuild?.auth });
                        } else {
                            sessionService.aimportAll(session.id, { guildCode: '', guildAuth: 0 });
                        }
                    })
                }
            }
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setOtherUserSession(params: { roleId: string, roleName?: string, blockType?: number, vipStartTime?: number}[]) {
        try {
            let sessionService = this.app.get('sessionService');
            for(let {roleId, roleName, blockType, vipStartTime} of params) {
                let sessions = sessionService.getByUid(roleId);
                if(!!sessions) {
                    sessions.forEach(session => {
                        if(roleName) sessionService.aimportAll(session.id, { roleName });
                        if(blockType) sessionService.aimportAll(session.id, { blockType });
                        if(vipStartTime) sessionService.aimportAll(session.id, { vipStartTime });
                    });
                }
            }
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * 重载json资源
     */
    public async reloadResources() {
        try {
            reloadResources();
        } 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 async setPvpSeasonNum(pvpConfig: PVPConfigType) {
        try {
            await setPvpSeasonNum(pvpConfig);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async setPvpSettleSeasonNum(pvpConfig: PVPConfigType) {
        try {
            await setPvpSettleSeasonNum(pvpConfig);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * 改变字典表中的拍卖行时间
     */
     async setDicAuctionTime(startTime: number, endActivity: number, startGuild: number, endGuild: number, startWorld: number, endWorld: number) {
        try {
            setDicAuctionTime(startTime, endActivity, startGuild, endGuild, startWorld, endWorld);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

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

    async setDay(week: number) {
        try {
            setWeek(week);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    /**
     * 改变字典表中的军团活动开始时间&持续时间
     * @param {number} aid 活动 
     * @param {number} startSeconds 从现在开始几秒后开启活动
     * @param {number} endSeconds 从开始之后几秒后结束活动
     */
    public async setDicGuildActivity(now: number, aid: number, startSeconds: number, endSeconds: number) {
        try {
            setDicGuildActivity(now, aid, startSeconds, endSeconds);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public async incServerNum(sid: string, inc: number) {
        try {
            incServerNum(sid, inc);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }

    public savePvpSeasonMemory() {
        try {
            return savePvpSeasonMemory();
        } 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 setGVGServerGroup() {
        try {
            return setGVGServerGroup();
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }
    }
}
