import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ITEM_CHANGE_REASON, STATUS } from '../../../consts';
import { getPlayerGrowthData } from '../../../services/activity/growthService';
import { GrowthItem, PointRewardItem } from '../../../domain/activityField/growthField';
import { ActivityGrowthModel } from '../../../db/ActivityGrowth';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';
import { ActivityGrowthPointModel } from '../../../db/ActivityGrowthPoint';


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

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

    /************************成长任务****************************/

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

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

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

        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
        * @description 获取成长任务每天完成的单个奖励
        * @param {{ activityId: number, dayIndex: number, cellIndex: number, type: number}} msg
        * @param {BackendSession} session
        * @memberof GrowthHandler
        */
    async getGrowthCellReward(msg: { activityId: number, dayIndex: number, cellIndex: number, type: number }, session: BackendSession) {
        const { activityId, dayIndex, cellIndex, type } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        

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

        let growthItemData: GrowthItem = playerData.findGrowthItem(dayIndex, cellIndex, type);
        if (!growthItemData) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (!growthItemData.isComplete()) {//未完成任务
            return resResult(STATUS.ACTIVITY_TASK_UNCOMPLETED);
        }
        if (!growthItemData.canReceive()) {//已经领取过
            return resResult(STATUS.ACTIVITY_REWARDED);
        }
        if(growthItemData.dayIndex < playerData.today()) {
            return resResult(STATUS.ACTIVITY_DAY_INDEX_OVER);
        }


        await ActivityGrowthModel.addCellRecord(serverId, activityId, roleId, dayIndex, cellIndex, 1);
        let rewardParamArr: Array<RewardParam> = stringToRewardParam(growthItemData.reward);
        let result = await addReward(roleId, roleName, sid, serverId, rewardParamArr, ITEM_CHANGE_REASON.GROWTH_REWARD)

        growthItemData.receiveRewardCount = 1;
        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, dayIndex, cellIndex, type },
            item: growthItemData,
        }));
    }

    /**
        * @description 成长任务每天奖章兑换奖励
        * @param {{ activityId: number, id: number}} msg
        * @param {BackendSession} session
        * @memberof GrowthHandler
        */
    async getGrowthDayReward(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');
        

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

        let pointItemData: PointRewardItem = playerData.findPointItem(id);
        if (!pointItemData) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (playerData.getTotalPoint() < pointItemData.progress) {
            return resResult(STATUS.ACTIVITY_NO_POINT);
        }
        if (pointItemData.getPointReward) {//已经领取过
            return resResult(STATUS.ACTIVITY_REWARDED);
        }

        await ActivityGrowthPointModel.addRecord(serverId, activityId, roleId, id);

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


        pointItemData.getPointReward = true;
        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, id },
            item: pointItemData,
        }));
    }
}
