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

export class ConnectorRemote {

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

    public async remoteLogin(uid: string) {
        let sessionService = this.app.get('sessionService');
        let sessions = sessionService.getByUid(uid);
        if (!!sessions) {
            sessions.forEach(session=>{
                let roleId = session.get('roleId');
                let sid = session.get('sid');
                let uids = [{uid: roleId, sid}];
                this.channelService.pushMessageByUids('onRemoteLogin', resResult(STATUS.LOGIN_ERR ), uids);
            });
        }
        await sessionService.akick(uid);
    }

    public async pushMessage( uid: string, route: string, data:any) {
        let sid = pinus.app.getServerId();
        if (!!sid) {
            let uids = [{uid, sid}];
            this.channelService.pushMessageByUids(route, resResult(STATUS.SUCCESS, {mails:data}), uids);
        }
    }

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