import { Application, BackendSession } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, ACTIVITY_RESOURCES_TYPE, ACTIVITY_TYPE } from '../../../consts';
import { addRechargeMoney, getPlayerRechargeMoneyData } from '../../../services/activity/rechargeMoneyService';
import { RechargeMoneyItem } from '../../../domain/activityField/rechargeMoneyField';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';
import { ActivityRechargeMoneyModel } from '../../../db/ActivityRechargeMoney';


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

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

    /************************每日累计充值****************************/

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

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

        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
   * @description 获取奖励
   * @param {BackendSession} session
   * @memberof RechargeMoneyHandler
   */
    async getRechargeMoneyReward(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 playerData = await getPlayerRechargeMoneyData(activityId, serverId, roleId);
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        if (playerData.totalCount <= playerData.receiveCount) {
            return resResult(STATUS.ACTIVITY_RECHARGE_MONEY_NO_REWARD);
        }

        let item: RechargeMoneyItem = playerData.randomReward();
        if (!item) {
            return resResult(STATUS.ACTIVITY_RECHARGE_MONEY_ERROR);
        }

        let recordArray = playerData.recordArray.filter(obj => { return obj && !obj.rewardTime })
        let rechargeItem = recordArray[0];
        console.log(JSON.stringify(recordArray));
        await ActivityRechargeMoneyModel.addRecord(rechargeItem._id, item.id, item.reward)
        let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.reward);
        let result = await addReward(roleId, roleName, sid, serverId, funcs, rewardParamArr)
        if (playerData.isLast()) {
            let _ids = playerData.recordArray.map(a => a._id)
            await ActivityRechargeMoneyModel.setEndState(serverId, roleId, _ids);
        }

        item.rewardTime = new Date();
        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId },
            item: item
        }));
    }
}
