import { Application, BackendSession } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, } from '../../../consts';
import { getPlayerDailyRMBGiftsData, makeDailyRMBGiftsReward } 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');

export default function (app: Application) {
    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');
        const funcs: number[] = session.get('funcs');

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

        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 (item.price > 0) {
                return resResult(STATUS.ACTIVITY_NEED_PAY);
            }
            let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.reward);
            let result = await addReward(roleId, roleName, sid, serverId, funcs, rewardParamArr)
            await ActivityDailyRMBGiftsModel.receiveRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate(), [item.id]);
            item.isReceive = true;
            return resResult(STATUS.SUCCESS, Object.assign(result, {
                param: { activityId, id },
                items: [item]
            }));
        } else {//一键领取全天的奖励
            if (!playerData.isBuy) {
                return resResult(STATUS.ACTIVITY_UNOPENED);
            }
            let items = playerData.unReceiveItem();
            if (items.length == 0) {
                return resResult(STATUS.ACTIVITY_REWARDED);
            }
            let rewardArray = [];
            let ids = []
            for (let item of items) {
                ids.push(item.id)
                let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.reward);
                rewardArray = rewardArray.concat(rewardParamArr);
                item.isReceive = true;
            }
            let result = await addReward(roleId, roleName, sid, serverId, funcs, rewardArray)
            await ActivityDailyRMBGiftsModel.receiveRecord(serverId, activityId, roleId, moment(playerData.beginTime).toDate(), ids);
            return resResult(STATUS.SUCCESS, Object.assign(result, {
                param: { activityId },
                items: items
            }));
        }
    }

}
