import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, ACTIVITY_TYPE, ITEM_CHANGE_REASON } from '../../../consts';
import { getPlayerThirtyDaysData } from '../../../services/activity/thirtyDaysService';
import { ThirtyDaysData, ThirtyDaysItem, ThirtyDaysPointItem } from '../../../domain/activityField/thirtyDaysField';
import { ActivityThirtyDaysModel } from '../../../db/ActivityThirtyDays';
import { ActivityThirtyDaysPointRewardModel } from '../../../db/ActivityThirtyDaysPointReward';
import { ActivityModel, ActivityModelType } from '../../../db/Activity';
import { ServerlistModel } from '../../../db/Serverlist';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';

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

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

    /************************30天大目标****************************/

    /**
    * @description 获取30目标活动数据
    * @param {{ }} msg
    * @param {{ activityId: number}} msg
    * @param {BackendSession} session
    * @memberof ThirtyDaysHandler
    */
    async getThirtyDaysActivity(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

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

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

    /**
        * @description 获取30天完成任务的奖励
        * @param {{ activityId: number, pageIndex: number, cellIndex: number,tab: number}} msg
        * @param {BackendSession} session
        * @memberof ThirtyDaysHandler
        */
    async getThirtyDaysCellReward(msg: { activityId: number, pageIndex: number, cellIndex: number, tab: number }, session: BackendSession) {
        const { activityId, pageIndex, cellIndex, tab } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');

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

        if (tab > 1) {
            let item = playerData.findThirtyDaysItem(pageIndex, cellIndex, tab - 1)
            if (!item.isReceive) {
                return resResult(STATUS.ACTIVITY_PRE_UNCOMPLETE);
            }
        }

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

        if (0 < playerData.todayIndex && playerData.todayIndex <= playerData.days) {//时间正常范围内

        } else if (playerData.todayIndex > playerData.days) {//活动已经过期
            // console.log('过期奖励')
            let expiredItems: Array<ThirtyDaysPointItem> = playerData.needMark();
            let cells = expiredItems.map(obj => obj.cellIndex)
            await ActivityThirtyDaysPointRewardModel.setExpired(activityId, roleId, cells);
        } else {//活动未到开启时间
            return resResult(STATUS.ACTIVITY_TIME_ERROR);
        }

        await ActivityThirtyDaysModel.addRecord(serverId, activityId, roleId, pageIndex, cellIndex, tab);

        let rewardParamArr: Array<RewardParam> = stringToRewardParam(thirtyDaysItemData.reward);
        let result = await addReward(roleId, roleName, sid, serverId, rewardParamArr, ITEM_CHANGE_REASON.THIRTHDAY_TASK)
        thirtyDaysItemData.isReceive = true;
        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, pageIndex, cellIndex },
            item: thirtyDaysItemData,
        }));
    }

    /**
       * @description 点数兑换奖励
       * @param {{ activityId: number, cellIndex: number}} msg
       * @param {BackendSession} session
       * @memberof ThirtyDaysHandler
       */
    async getThirtyDaysPointReward(msg: { activityId: number, cellIndex: number }, session: BackendSession) {
        const { activityId, 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 getPlayerThirtyDaysData(activityId, serverId, roleId)
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        let thirtyDaysPointItemData: ThirtyDaysPointItem = playerData.findThirtyDaysPointItem(cellIndex);
        if (!thirtyDaysPointItemData) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (playerData.totalPoint < thirtyDaysPointItemData.consumePoint) {//点数不足
            return resResult(STATUS.ACTIVITY_NO_POINT);
        }
        if (thirtyDaysPointItemData.isReceive) {//已经领取过
            return resResult(STATUS.ACTIVITY_REWARDED);
        }

        await ActivityThirtyDaysPointRewardModel.addRecord(activityId, roleId, cellIndex);

        let reward = thirtyDaysPointItemData.reward
        if (thirtyDaysPointItemData.isExpired && thirtyDaysPointItemData.expiredReward && thirtyDaysPointItemData.expiredReward != '') {//过期奖励
            reward = thirtyDaysPointItemData.expiredReward;
        }

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

        thirtyDaysPointItemData.isReceive = true;
        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, cellIndex },
            item: thirtyDaysPointItemData,
        }));
    }
}
