import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, ACTIVITY_RESOURCES_TYPE, ITEM_CHANGE_REASON } from '../../../consts';
import { handleCost } from '../../../services/role/rewardService';
import { getPlayerDailyGiftsData } from '../../../services/activity/dailyGiftsService';
import { DailyGiftItem } from '../../../domain/activityField/dailyGiftsField';
import { ActivityDailyGiftsModel } from '../../../db/ActivityDailyGifts';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';


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

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


    /************************每日特惠礼包****************************/

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

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

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

        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
        * @description 购买每日特惠礼包
        * @param {{ activityId: number, dayIndex: number, cellIndex: number}} msg
        * @param {BackendSession} session
        * @memberof DailyGiftsHandler
        */
    async buyDailyGiftsCell(msg: { activityId: number, dayIndex: number, cellIndex: number }, session: BackendSession) {
        const { activityId, dayIndex, cellIndex } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        

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

        let dailyItemData: DailyGiftItem = playerData.findDailyGiftsItem(dayIndex, cellIndex);
        if (!dailyItemData) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (!dailyItemData.canBuy()) {//最大次数
            return resResult(STATUS.ACTIVITY_MAX_COUNT);
        }
        if (dayIndex > playerData.today()) {//还未开启
            return resResult(STATUS.ACTIVITY_UNOPENED);
        }

        //货币是否足够
        let consumeData = dailyItemData.consumeRes();
        let consumeType = consumeData.type;//购买类型

        if (consumeType == ACTIVITY_RESOURCES_TYPE.GOODS) {//物品表，元宝、金币、体力
            let result = await handleCost(roleId, sid, [{ id: consumeData.id, count: consumeData.count }], ITEM_CHANGE_REASON.BUY_DAILY_GIFTS);
            if (!result) return resResult(STATUS.ACTIVITY_RES_NOT_ENOUGH);
        } else if (consumeType == ACTIVITY_RESOURCES_TYPE.RMB) {//RMB购买
            return resResult(STATUS.ACTIVITY_NEED_PAY);
        }

        await ActivityDailyGiftsModel.buyRecord(serverId, activityId, roleId, dayIndex, cellIndex, 1);

        let rewardParamArr: Array<RewardParam> = stringToRewardParam(dailyItemData.reward);
        let result = await addReward(roleId, roleName, sid, serverId, rewardParamArr, ITEM_CHANGE_REASON.BUY_DAILY_GIFTS)

        dailyItemData.buyCount += 1;
        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, dayIndex, cellIndex },
            item: dailyItemData,
        }));
    }

}
