import { Application, ChannelService, FrontendSession, pinus, RemoterClass, HandlerService, } from 'pinus';
import { STATUS } from '../../../consts/statusCode';
import { resResult } from '../../../pubUtils/util';
import { reloadResources } from '../../../pubUtils/data';
import { UserGuildType } from '../../../db/UserGuild';
import { kickUser } from '../../../services/connectorService';
export default function (app: Application) {
    new HandlerService(app, {});
    return new ConnectorRemote(app);
}

export class ConnectorRemote {

    constructor(private app: Application) {
        this.app = app;
    }
    private maintenServers: number[] = []; // 维护中的服务器

    public async remoteLogin(uid: string) {
        await kickUser(this.app, uid);
    }

    public async setOtherUserGuildSession(params: { roleId: string, userGuild: UserGuildType}[]) {
        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 });
                    }
                })
            }
        }
    }

    /**
     * 重载json资源
     */
    public async reloadResources() {
        reloadResources();
    }

    public setServerMainten(maintenServers: number[]) {
        this.app.set('maintenServers', maintenServers);
        return this.maintenServers = maintenServers;
    }

    public getServerMainten() {
        return this.maintenServers;
    }
}
