import { Application, ChannelService, HandlerService, } from 'pinus';
import { PVPConfigModel, PVPConfigType } from '../../../db/PvpConfig';
import { reloadResources } from '../../../pubUtils/data';
import { setApiIsClose } from '../../../services/chatService';
import { setHiddenData } from '../../../services/dataService';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';
import { savePvpSeasonMemory } from '../../../services/log/memoryLogService';
import { setKvToMemory } from '../../../services/pushService';
import { taflush } from '../../../services/sdkService';
import { setPvpSeasonNum, setPvpSettleSeasonNum } from '../../../services/timeTaskService';
import { errlogger } from '../../../util/logger';

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

export class BattleRemote {

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

    private channelService: ChannelService;

    /**
     * Add user into chat channel.
     *
     * @param {String} uid unique id for user
     * @param {String} sid server id
     * @param {boolean} flag channel parameter
     *
     */
    public async add(uid: string, sid: string, serverId: number, flag: boolean) {
        let name = `server-${serverId}`;
        console.log('BattleRemote add: ', name, flag);
        let channel = this.channelService.getChannel(name, flag);
        if (!!channel && !this.get(name, false).includes(uid)) {
            if (!!channel) {
                channel.add(uid, sid);
            }
        }
        return this.get(name, flag);
    }

    /**
     * Get user from chat channel.
     *
     * @param {Object} opts parameters for request
     * @param {String} name channel name
     * @param {boolean} flag channel parameter
     * @return {Array} users uids in channel
     *
     */
    private get(name: string, flag: boolean) {
        let users: string[] = [];
        let channel = this.channelService.getChannel(name, flag);
        if (!!channel) {
            users = channel.getMembers();
        }
        for (let i = 0; i < users.length; i++) {
            users[i] = users[i].split('*')[0];
        }
        return users;
    }

    /**
     * Kick user out chat channel.
     *
     * @param {String} uid unique id for user
     * @param {String} sid server id
     *
     */
    public async kick(uid: string, sid: string, serverId: number) {
        let name = `server-${serverId}`;
        let channel = this.channelService.getChannel(name, false);
        // leave channel
        if (!!channel) {
            channel.leave(uid, sid);
        }
    }

    /**
     * 重载json资源
     */
    public async reloadResources() {
        try {
            reloadResources();
        } 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}`);
        }
    }

    public async setPvpSeasonNum(pvpConfig: PVPConfigType) {
        try {
            await setPvpSeasonNum(pvpConfig);
        } 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 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}`);
        }
    }
}