import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ITEM_CHANGE_REASON, STATUS, } from '../../../consts';
import { getPlayerNewHeroGiftsData } from '../../../services/activity/newHeroService';
import { RewardParam } from '../../../domain/activityField/rewardField';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { ActivityNewHeroGiftModel } from '../../../db/ActivityNewHeroGift';

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

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

    /************************新将好礼，很多红包，用积分兑换一遍结束****************************/

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

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

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

    /**
        * @description 兑换点数
        * @param {{ activityId: number, index:number}} msg
        * @param {BackendSession} session
        * @memberof NewHeroGiftsHandler
        */
    async exchangePoint(msg: { activityId: number, index: number }, session: BackendSession) {
        const { activityId, index } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');

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

        let item = playerData.findItem(index);
        if (!item) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (item.countMax <= item.buyCount) {
            return resResult(STATUS.ACTIVITY_MAX_COUNT);
        }
        if (playerData.consumePoint + playerData.consumeTotalPoint > playerData.totalPoint) {
            return resResult(STATUS.ACTIVITY_NO_POINT);
        }
        let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.reward);
        let result = await addReward(roleId, roleName, sid, serverId, rewardParamArr, ITEM_CHANGE_REASON.NEW_HERO_EXCHANGE)
        await ActivityNewHeroGiftModel.addReceiveRecord(serverId, activityId, roleId, index, playerData.consumePoint);
        item.buyCount += 1;
        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, index },
            item: item, consumeTotalPoint: playerData.consumeTotalPoint + playerData.consumePoint
        }));
    }
}
