import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ITEM_CHANGE_REASON, STATUS, } from '../../../consts';
import { getTaskPassData, getTaskPassDataShow } from '../../../services/activity/taskPassService';
import { ActivityTaskPassModel, ReceivedReward } from '../../../db/ActivityTaskPass';
import { RewardParam } from '../../../domain/activityField/rewardField';
import { nowSeconds } from '../../../pubUtils/timeUtil';
import { addReward, stringToConsumeParam, stringToRewardParam } from '../../../services/activity/giftPackageService';
import { ItemInter } from '../../../pubUtils/interface';
import { isNumber, pick } from 'underscore';
import { handleCost } from '../../../services/role/rewardService';

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

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

    /************************战令系统****************************/
    // 获得战令活动数据
    public async getTaskPassData(msg: { activityId: number }, session: BackendSession) {
        const { activityId } = msg;
        const roleId = session.get('roleId');
        const serverId = session.get('serverId');

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

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

    // （一键）领取
    public async receiveItems(msg: { activityId: number, pageIndex: number, lv: number }, session: BackendSession) {

        const { activityId, pageIndex, lv = 0 } = msg;
        const roleId = session.get('roleId');
        const roleName = session.get('roleName');
        const sid = session.get('sid');
        const serverId = session.get('serverId');

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

        let items = playerData.receiveItems(pageIndex, lv);
        if(!items) return resResult(STATUS.ACTIVITY_ITEM_CANNOT_RECEIVE);

        let records: ReceivedReward[] = [], rewards: RewardParam[] = [];
        for(let item of items) {
            records.push({ pageIndex: item.pageIndex, lv: item.lv, receiveTime: nowSeconds() });
            rewards.push(...stringToRewardParam(item.reward));
        }
        await ActivityTaskPassModel.receiveItems(serverId, activityId, roleId, playerData.roundIndex, records);
        let result = await addReward(roleId, roleName, sid, serverId, rewards, ITEM_CHANGE_REASON.ACT_TASK_PASS);

        return resResult(STATUS.SUCCESS, { items, ...result });
    }

    // 加速
    public async speedUp(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 getTaskPassData(activityId, serverId, roleId)
        if (!playerData) return resResult(STATUS.ACTIVITY_MISSING);

        let cost: ItemInter[] = [];
        let consumeArr = stringToConsumeParam(playerData.spdUp.cost);
        for(let consume of consumeArr) {
            cost.push({ ...consume, count: consume.count * count });
        }
        let consumeResult = await handleCost(roleId, sid, cost, ITEM_CHANGE_REASON.ACT_TASK_PASS_SPD_UP);
        if(!consumeResult) return resResult(STATUS.ROLE_MATERIAL_NOT_ENOUGH);

        let addPoint = playerData.spdUp.point * count;
        await ActivityTaskPassModel.addPoint(serverId, activityId, roleId, playerData.roundIndex, addPoint, count);
        let result = playerData.addPoint(addPoint, count);

        return resResult(STATUS.SUCCESS, result);
    }
}
