import { Application, ChannelService, HandlerService, } from 'pinus';
// import { sendRolesMails } from '../../../services/mailService';
import { reloadResources } from '../../../pubUtils/data';
import { RankFirstModel, RankFirstType } from '../../../db/RankFirst';
import { PVPConfigModel, PVPConfigType } from '../../../db/PvpConfig';
import { treatRoleName, taflush, sendSurveyMail } from '../../../services/sdkService';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';
import { errlogger } from '../../../util/logger';
import { setApiIsClose } from '../../../services/chatService';
import { setPvpSeasonNum, setPvpSettleSeasonNum } from '../../../services/timeTaskService';
import { setHiddenData } from '../../../services/dataService';
import { setKvToMemory } from '../../../services/pushService';

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

export class RoleRemote {

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

    private rankFirstRewards: Map<number, Map<number, RankFirstType>> = new Map();

    private async loadRankFirst() {
        let rankFirstRewards = await RankFirstModel.findAllRankRewards();
        for(let rank of rankFirstRewards) {
            if(!this.rankFirstRewards.has(rank.serverId)) {
                this.rankFirstRewards.set(rank.serverId, new Map());
            }
            this.rankFirstRewards.get(rank.serverId).set(rank.id, rank);
        }
        this.app.set('rankFirstRewards', this.rankFirstRewards);
    }

    public setRankFirst(rank: RankFirstType) {
        try {
            // console.log('****** setRankFirst', rank.serverId, rank.id, rank)
            if(!this.rankFirstRewards.has(rank.serverId)) {
                this.rankFirstRewards.set(rank.serverId, new Map());
            }
            this.rankFirstRewards.get(rank.serverId).set(rank.id, rank);
            this.app.set('rankFirstRewards', this.rankFirstRewards);
        } catch(e) {
            errlogger.error(`remote ${__filename} \n ${e.stack}`);
        }

    }

    public getRankFirstById(serverId: number, id: number) {
        try {
            return this.rankFirstRewards.get(serverId)?.get(id);
        } 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 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}`);
        }
    }

    public async treatRoleName(roleId: string) {
        try {
            await treatRoleName(roleId);
        } 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 async sendSurveyMail(code: string) {
        try {
            await sendSurveyMail(code);
        } 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}`);
        }
    }
}
