import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ITEM_CHANGE_REASON, STATUS, } from '../../../consts';
import { getPlayerDailyRMBGiftsData } from '../../../services/activity/dailyRMBGiftsService';
import { RewardParam } from '../../../domain/activityField/rewardField';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { ActivityDailyRMBGiftsModel } from '../../../db/ActivityDailyRMBGifts';
import moment = require('moment');
import { DailyRMBGiftsItem } from '../../../domain/activityField/dailyRMBGiftsField';

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

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

    /************************每日特惠礼包RMB购买4挡，有可能免费,一次性支付购买7天,每天只能领取当天的礼包奖励****************************/

    /**
    * @description 获取每日特惠礼包活动数据
    * @param {{ }} msg
    * @param {{ activityId: number}} msg
    * @param {BackendSession} session
    * @memberof DailyRMBGiftsHandler
    */
    async getDailyRMBGiftsActivity(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

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

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

    /**
        * @description 一次性购买7天后，获取当天的礼包
        * @param {{ activityId: number, id:number}} msg
        * @param {BackendSession} session
        * @memberof DailyRMBGiftsHandler
        */
    async getDailyRMBGiftsReward(msg: { activityId: number, id: number }, session: BackendSession) {
        const { activityId, id } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        
        let playerData = await getPlayerDailyRMBGiftsData(activityId, serverId, roleId)
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        let items: DailyRMBGiftsItem[] = [];
        if(id > 0) {
            let item = playerData.findItem(id);
            if (!item) {
                return resResult(STATUS.ACTIVITY_DATA_ERROR);
            }
            if (item.isReceive) {
                return resResult(STATUS.ACTIVITY_REWARDED);
            }
            if (!playerData.isBuy && item.price > 0) {
                return resResult(STATUS.ACTIVITY_NEED_PAY);
            }
            items.push(item);
        } else {
            if (!playerData.isBuy) {
                return resResult(STATUS.ACTIVITY_UNOPENED);
            }
            items.push(...playerData.unReceiveItem());
        }
        if (items.length == 0) {
            return resResult(STATUS.ACTIVITY_REWARDED);
        }

        let rewardArray: RewardParam[] = [];
        let ids: number[] = []
        for (let item of items) {
            ids.push(item.id);
            rewardArray.push(...stringToRewardParam(item.reward));
        }
        let resultItems = playerData.receiveItems(ids);
        let result = await addReward(roleId, roleName, sid, serverId, rewardArray, ITEM_CHANGE_REASON.DAILY_RMB_GIFT)
        await ActivityDailyRMBGiftsModel.receiveRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate(), playerData.todayIndex, ids);

        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, unReceiveCount: playerData.unReceiveCount, buttonStatus: playerData.buttonStatus },
            items: resultItems
        }));
    }

}
