import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ITEM_CHANGE_REASON, STATUS, } from '../../../consts';
import { getPlayerVipRechargeMoneyData, } from '../../../services/activity/vipRechargeMoneyService';
import { VipRechargeMoneyItem } from '../../../domain/activityField/vipRechargeMoneyField';
import { addReward, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { RewardParam } from '../../../domain/activityField/rewardField';
import { ActivityVipRechargeMoneyModel } from '../../../db/ActivityVipRechargeMoney';
import { UserOrderModel } from '../../../db/UserOrder';
import { dicRMB } from '../../../pubUtils/dictionary/DicRMB';
import moment = require('moment');


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

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

    /************************vip累计充值奖励****************************/

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

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

        return resResult(STATUS.SUCCESS, playerData);
    }

    /**
   * @description 获取奖励
   * @param {BackendSession} session
   * @memberof VipRechargeMoneyHandler
   */
    async getVipRechargeMoneyReward(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 getPlayerVipRechargeMoneyData(activityId, serverId, roleId);
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        let item: VipRechargeMoneyItem = playerData.findItem(id);
        if (!item) {
            return resResult(STATUS.ACTIVITY_DATA_ERROR);
        }
        if (playerData.totalRMB < item.condition) {
            return resResult(STATUS.ACTIVITY_TASK_UNCOMPLETED);
        }
        if (item.receiveCount >= item.countMax) {
            return resResult(STATUS.ACTIVITY_MAX_COUNT);
        }

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

        let rewardParamArr: Array<RewardParam> = stringToRewardParam(item.reward);
        let result = await addReward(roleId, roleName, sid, serverId, rewardParamArr, ITEM_CHANGE_REASON.VIP_RECHARGE_MONEY)
        item.receiveCount += 1;

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


    /**
   * @description 获取订单记录
   * @param {BackendSession} session
   * @memberof VipRechargeMoneyHandler
   */
    async getOrderList(msg: { begin: number, count: number }, session: BackendSession) {
        const { begin, count } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');
        const sid = session.get('sid');
        const roleName = session.get('roleName');
        

        if (begin > 0 && count > 0) {
            let orders = await UserOrderModel.findOrderList(serverId, roleId, begin - 1, count)

            for (let order of orders) {
                let productInfo = dicRMB.get(order.productID)
                if (productInfo) {
                    order['message'] = productInfo.message;
                }
                order['time'] = moment(order.createdAt).valueOf();
                delete order.createdAt;
                delete order.productID;
                delete order._id;
            }
            return resResult(STATUS.SUCCESS, { orders });
        }
        return resResult(STATUS.WRONG_PARMS);
    }
}
