import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ITEM_CHANGE_REASON, STATUS } from '../../../consts';
import { getPlayerLuckyTurntableDataShow, getPlayerLuckyTurntableData } from '../../../services/activity/luckyTurntableService';
import { addItems, handleCost } from '../../../services/role/rewardService';
import { ActivityTurntableModel } from '../../../db/ActivityTurntableRec';
import { pick } from 'underscore';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';


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

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

    /************************幸运转盘****************************/
    /** 
    * @description 幸运转盘活动
    * @param {{ activityId: number, }} msg
    * @param {BackendSession} session
    * @memberof LuckyTurntableHandler
    */
     async getTurntableData(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

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

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

    /** 
    * @description 转转盘
    * @param {{ activityId: number, }} msg
    * @param {BackendSession} session
    * @memberof LuckyTurntableHandler
    */
    async pull(msg: { activityId: number, count: number }, session: BackendSession) {
        const { activityId, count } = msg;
        const roleId = session.get('roleId');
        const roleName = session.get('roleName');
        const sid = session.get('sid');
        const serverId = session.get('serverId');

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

        let cost = playerData.getCost(count);
        let costResult = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.ACT_TURNTABLE_PULL);
        if(!costResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);

        let { result, records, goodResult } = playerData.pull(roleName, count);
        await ActivityTurntableModel.updateData(serverId, activityId, roleId, playerData.roundIndex, {...pick(playerData, ['todayCount', 'count', 'records', 'greatRewardCount']), refTodayCount: new Date()});
        await addItems(roleId, roleName, sid, goodResult, ITEM_CHANGE_REASON.ACT_TURNTABLE_PULL);

        return resResult(STATUS.SUCCESS, { result, records, todayCount: playerData.todayCount, count: playerData.count });
    }
    

    /** 
    * @description 领宝箱
    * @param {{ activityId: number, boxCount: number }} msg
    * @param {BackendSession} session
    * @memberof LuckyTurntableHandler
    */
     async receiveBox(msg: { activityId: number, boxCount: number }, session: BackendSession) {
        const { activityId, boxCount } = msg;
        const roleId = session.get('roleId');
        const roleName = session.get('roleName');
        const sid = session.get('sid');
        const serverId = session.get('serverId');

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

        let canReceive = playerData.canReceive(boxCount);
        if(!canReceive) return resResult(STATUS.BOX_CAN_NOT_RECEIVE);

        let box = playerData.findBox(boxCount);
        let rewardArray = stringToRewardParam(box.rewards)
        let result = await addReward(roleId, roleName, sid, serverId, rewardArray, ITEM_CHANGE_REASON.ACT_TURNTABLE_RECEIVE_BOX);
        await ActivityTurntableModel.receiveBox(serverId, activityId, roleId, playerData.roundIndex, boxCount);

        return resResult(STATUS.SUCCESS, { ...result, box: {...box, isReceived: true} });
    }
}
