import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { DEBUG_MAGIC_WORD, getRedisKeyByRankType, HERO_SELECT, ITEM_CHANGE_REASON, RANK_TYPE, ROLE_SELECT, STATUS } from '../../../consts';
import { pick } from 'underscore';
import { getTimeLimitRankData, getTimeLimitRankDataShow, sendRankMail, takeSnapshot } from '../../../services/activity/timeLimitRankService';
import { getRankInHandler, Rank } from '../../../services/rankService';
import { getActivityById } from '../../../services/activity/activityService';
import { TimeLimitRankData } from '../../../domain/activityField/timeLimitRankField';
import { getServerCreateTime, hasKey } from '../../../services/redisService';
import { KeyName } from '../../../domain/rank';


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

export class TimeLimitRankHandler {
    constructor(private app: Application) {
    }

    /************************幸运转盘****************************/
    /** 
    * @description 幸运转盘活动
    * @param {{ activityId: number, }} msg
    * @param {BackendSession} session
    * @memberof TimeLimitRankHandler
    */
     async getTimeLimitRankData(msg: { activityId: number }, session: BackendSession) {
        const serverId = session.get('serverId');
        const { activityId } = msg;

        let playerData = await getTimeLimitRankDataShow(activityId, serverId);
        if (!playerData) {
            return resResult(STATUS.ACTIVITY_MISSING);
        }

        return resResult(STATUS.SUCCESS, { playerData });
    }

    /** 
    * @description 获得排行榜
    * @param {{ activityId: number, }} msg
    * @param {BackendSession} session
    * @memberof TimeLimitRankHandler
    */
    async getRank(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

        let playerData = await getTimeLimitRankData(activityId, serverId);
        if (!playerData) {
            return resResult(STATUS.ACTIVITY_MISSING);
        }
        let type = playerData.rankType;

        let redisKey = getRedisKeyByRankType(type, true);
        let keyName = new KeyName(redisKey, { serverId, activityId });
        if(!(await hasKey(keyName.getName()))) {
            redisKey = getRedisKeyByRankType(type);
        }
        if (!redisKey) return resResult(STATUS.WRONG_PARMS);

        let result = await getRankInHandler(redisKey, type, { serverId, activityId: playerData.activityId }, session);
        if(!result) return resResult(STATUS.WRONG_PARMS);

        return resResult(STATUS.SUCCESS, { 
            ...pick(playerData, ['activityId', 'tabName', 'beginTime', 'endTime', 'rankEndTime', 'rankType', 'rankRewards']),
            ...result 
        });
    }
    

    async debugSendMail(msg: { magicWord: string, activityId: number }, session: BackendSession) {
        let serverId = session.get('serverId');
        const { magicWord, activityId } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        let playerData = await getTimeLimitRankData(activityId, serverId);
        await sendRankMail(playerData, serverId);
        return resResult(STATUS.SUCCESS);
    }

    async debugTakeSnapshot(msg: { magicWord: string, activityId: number }, session: BackendSession) {
        let serverId = session.get('serverId');

        const { magicWord, activityId } = msg;
        if (magicWord !== DEBUG_MAGIC_WORD) {
            return resResult(STATUS.TOKEN_ERR);
        }
        let activityData = await getActivityById(activityId);
        let serverTime = await getServerCreateTime(serverId);
        let playerData = new TimeLimitRankData(activityData, 0, serverTime);
        if(!playerData.needSnapshot) return resResult(STATUS.WRONG_PARMS);
        await takeSnapshot(playerData, activityData.groupId);
        return resResult(STATUS.SUCCESS);
    }
}
