import { Application, BackendSession, HandlerService } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ITEM_CHANGE_REASON, STATUS } from '../../../consts';
import { getPlayerDailyChallengesData } from '../../../services/activity/dailyChallengesService';
import { DailyItem } from '../../../domain/activityField/dailyChallengesField';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';
import { ActivityDailyChallengesModel } from '../../../db/ActivityDailyChallenges';


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

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

    /************************今日挑战****************************/

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

        let playerData = await getPlayerDailyChallengesData(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 DailyChallengesHandler
        */
    async getDailyChallengeReward(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 getPlayerDailyChallengesData(activityId, serverId, roleId)
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        let dailyItemData: DailyItem = playerData.findDailyChallengesItem(dayIndex, cellIndex, type);
        if (!dailyItemData) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (!dailyItemData.isComplete()) {//未完成任务
            return resResult(STATUS.ACTIVITY_TASK_UNCOMPLETED);
        }
        if (!dailyItemData.canReceive()) {//已经领取过
            return resResult(STATUS.ACTIVITY_REWARDED);
        }

        await ActivityDailyChallengesModel.addCellRecord(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.DAILY_CHALLENGE_REWARD)

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

}
