import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ITEM_CHANGE_REASON, STATUS } from '../../../consts';
import { handleCost } from '../../../services/role/rewardService';
import { getPlayerDailyMealData } from '../../../services/activity/dailyMealService';
import { DailyMealItem } from '../../../domain/activityField/dailyMealField';
import { ActivityDailyMealModel } from '../../../db/ActivityDailyMeal';
import { addReward, stringToConsumeParam, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';
import moment = require('moment');


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

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


    /************************每日午饭/晚饭****************************/

    /**
        * @description 获取每日午饭/晚饭活动数据
        * @param {{ activityId: number}} msg
        * @param {BackendSession} session
        * @memberof DailyMealHandler
        */
    async getDailyMealActivity(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

        let playerData = await getPlayerDailyMealData(activityId, serverId, roleId)

        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
        * @description 领取奖励
        * @param {{ activityId: number}} msg
        * @param {BackendSession} session
        * @memberof DailyMealHandler
        */
    async getDailyMealReward(msg: { activityId: number, type: number }, session: BackendSession) {
        const { activityId, type } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');


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

        let itemData: DailyMealItem = playerData.findItem(type);
        if (!itemData) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (itemData.isReceive) {//最大次数
            return resResult(STATUS.ACTIVITY_REWARDED);
        }
        let curDate = moment(new Date()).valueOf();
        if (curDate < itemData.beginTime) {//没到饭点
            return resResult(STATUS.ACTIVITY_DAY_INDEX_OVER);
        }
        if (curDate > itemData.endTime) {//补领
            //检查资源
            let consume = stringToConsumeParam(itemData.consume)
            let consumeResult = await handleCost(roleId, sid, consume, ITEM_CHANGE_REASON.DAILY_MEAL_RECEIVE_REPAIRE);
            if (!consumeResult) return resResult(STATUS.ACTIVITY_RES_NOT_ENOUGH);
        }
        //免费期间
        let rewardParamArr: Array<RewardParam> = stringToRewardParam(itemData.reward);
        let result = await addReward(roleId, roleName, sid, serverId, rewardParamArr, ITEM_CHANGE_REASON.DAILY_MEAL_RECEIVE);
        await ActivityDailyMealModel.addReceiveRecord(serverId, activityId, roleId, type, curDate, playerData.roundIndex);
        itemData.isReceive = true;
        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, type },
            item: itemData,
        }));
    }

}
