import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ITEM_CHANGE_REASON, STATUS } from '../../../consts';
import { getPlayerGrowthFundData } from '../../../services/activity/growthFundService';
import { GrowthFundItem } from '../../../domain/activityField/growthFundField';
import { ActivityGrowthFundModel } from '../../../db/ActivityGrowthFund';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';


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

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

    /************************成长基金****************************/

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

        let playerData = await getPlayerGrowthFundData(activityId, serverId, roleId);

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

        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
        * @description 获取成长基金奖励
        * @param {{ activityId: number, pageIndex: number, cellIndex: number}} msg
        * @param {BackendSession} session
        * @memberof GrowthFundHandler
        */
    async getGrowthFundCellReward(msg: { activityId: number, pageIndex: number, cellIndex: number }, session: BackendSession) {
        const { activityId, pageIndex, 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 getPlayerGrowthFundData(activityId, serverId, roleId)
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        if (playerData.isVipActivity() && !playerData.isBuy(pageIndex)) {//vip高阶活动，需要购买
            return resResult(STATUS.ACTIVITY_NEED_BUY);
        }

        let growthFundItemData: GrowthFundItem = playerData.findGrowthFundItem(pageIndex, cellIndex);
        if (!growthFundItemData) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (!growthFundItemData.isComplete) {//未完成任务
            return resResult(STATUS.ACTIVITY_TASK_UNCOMPLETED);
        }
        if (playerData.isReceive(pageIndex, cellIndex)) {//已经领取过
            return resResult(STATUS.ACTIVITY_REWARDED);
        }

        await ActivityGrowthFundModel.addRecord(activityId, roleId, pageIndex, cellIndex);

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

        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, pageIndex, cellIndex },
            item: growthFundItemData
        }));
    }

}
