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';
import { PVPConfigType } from '../../../db/SystemConfig';
import { setDicAuctionTime } from '../../../services/guildActivityService';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';
import { taflush } from '../../../services/sdkService';
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) {
        await kickUser(this.app, uid, message);
    }

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

    public async setOtherUserRoleNameSession(params: { roleId: string, roleName: string}[]) {
        let sessionService = this.app.get('sessionService');
        for(let {roleId, roleName} of params) {
            let sessions = sessionService.getByUid(roleId);
            if(!!sessions) {
                sessions.forEach(session => {
                    sessionService.aimportAll(session.id, { roleName });
                });
            }
        }
    }

    public async setOtherUseBlockTypeSession(params: { roleId: string, blockType: string}[]) {
        let sessionService = this.app.get('sessionService');
        for(let {roleId, blockType} of params) {
            let sessions = sessionService.getByUid(roleId);
            if(!!sessions) {
                sessions.forEach(session => {
                    sessionService.aimportAll(session.id, { blockType });
                });
            }
        }
    }

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

    public setServerMainten(serverIds: number[], startTime: number, endTime: number) {
        setServerMainten(serverIds, startTime, endTime);
    }

    public stopServerMainten(serverIds: number[]) {
        stopServerMainten(serverIds);
    }

    public getServerMainten(serverId: number) {
        return getServerMainten(serverId);
    }

    public setPvpSeasonNum(pvpConfig: PVPConfigType) {
        if(pvpConfig) {
            this.app.set('pvpSeasonNum', pvpConfig.seasonNum);
            this.app.set('pvpSeasonEndTime', pvpConfig.seasonEndTime);
        }
    }

    /**
     * 改变字典表中的拍卖行时间
     */
     async setDicAuctionTime(startTime: number, endActivity: number, startGuild: number, endGuild: number, startWorld: number, endWorld: number) {
        setDicAuctionTime(startTime, endActivity, startGuild, endGuild, startWorld, endWorld);
    }

    public taflush() {
        return taflush();
    }
}
