import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { FIRST_GIFT_STATE, ITEM_CHANGE_REASON, STATUS } from '../../../consts';
import { getPlayerFirstGiftData, getPlayerFirstGiftDataShow } from '../../../services/activity/firstGiftService';
import { RoleModel } from '../../../db/Role';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { ActivityFirstGiftModel } from '../../../db/ActivityFirstGift';
import { RewardParam } from '../../../domain/activityField/rewardField';

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

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

    /************************首充礼包****************************/

    /**首冲活动按玩家注册时间
     * 不充，首充活动就一直在那里
    * @description 获取首充礼包数据
    * @param {{ activityId: number, }} msg
    * @param {BackendSession} session
    * @memberof FirstGiftHandler
    */
    async getFirstGiftActivity(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

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

        let { totalPay } = await RoleModel.findByRoleId(roleId);
        if (totalPay > 0) {//奖励开启

        }

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

    /**
        * @description 领取礼包奖励
        * @param {{ activityId: number, index: number}} msg
        * @param {BackendSession} session
        * @memberof FirstGiftHandler
        */
    async getFirstGiftReward(msg: { activityId: number, pageIndex: number }, session: BackendSession) {
        const { activityId, pageIndex } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        

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

        let playerPageData = playerData.findPageByIndex(pageIndex);
        if (playerPageData.state === FIRST_GIFT_STATE.NOT_PAY) {
            return resResult(STATUS.ACTIVITY_FIRST_GIFT_NOT_OPEN);
        }
        let items = playerPageData.getCanReceiveItems();
        if (items.length === 0) {
            return resResult(STATUS.ACTIVITY_REWARDED);
        }
        let rewardArray: RewardParam[] = [];
        let indexArray: number[] = [];
        for (let item of items) {
            rewardArray.push(...stringToRewardParam(item.reward));
            indexArray.push(item.index)
            item.isReceive = true;
        }
        let result = await addReward(roleId, roleName, sid, serverId, rewardArray, ITEM_CHANGE_REASON.FIRST_GIFT);

        let isOver = playerPageData.isComplete();
        let playerRecord = await ActivityFirstGiftModel.addRecord(activityId, roleId, pageIndex, indexArray, isOver);
        playerPageData.setPlayerRecord(playerRecord);

        return resResult(STATUS.SUCCESS, Object.assign(result, {
            param: { activityId, pageIndex, state: playerPageData.state },
            items: items
        }));
    }

}
