import { Application, BackendSession } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, ACTIVITY_RESOURCES_TYPE, ACTIVITY_TYPE } from '../../../consts';
import { getPlayerMonthlyTicketData, makeMonthlyTicketReward, getPlayerMonthlyTicketDayReward } from '../../../services/activity/monthlyTicketService';
import moment = require('moment');


export default function (app: Application) {
    return new MonthlyTicketHandler(app);
}

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

    /************************月卡活动****************************/

    /**
    * @description 获取月卡活动数据
    * @param {BackendSession} session
    * @memberof MonthlyTicketHandler
    */
    async getMonthlyTicketActivity(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

        let playerData = await getPlayerMonthlyTicketData(activityId, serverId, roleId);
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);
        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
    * @description 领取月卡奖品
    * @param {BackendSession} session
    * @memberof MonthlyTicketHandler
    */
    async getMonthlyTicketReward(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        const funcs: number[] = session.get('funcs');

        let result = await getPlayerMonthlyTicketDayReward(roleId, roleName, sid, serverId, funcs, activityId)
        if (result) {
            return resResult(STATUS.SUCCESS, Object.assign(result, {
                param: { activityId },
            }));
        }
        return resResult(STATUS.NO_MOTHLY_TICKET_REWARD);
    }
}
