import { Application, BackendSession } from 'pinus';
import { STATUS } from '../../../consts/statusCode';
import { resResult, shouldRefresh } from '../../../pubUtils/util';
import { RoleModel } from '../../../db/Role';
import { handleCost } from '../../../services/rewardService';
import { getGoldObject } from '../../../pubUtils/itemUtils';
import { getDungeonData, getDungeonBuyCountCost } from '../../../services/dungeonService';
import * as dicParam from '../../../pubUtils/dicParam';
import { DungeonFirstModel } from '../../../db/DungeonFirst';
import { DungeonResultParam } from '../../../domain/battleField/dungeon';

export default function(app: Application) {
    return new DungeonBattleHandler(app);
}

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

    // 获取关卡列表
    async getData(msg: { }, session: BackendSession) {
        let roleId = session.get('roleId');
        let role = await RoleModel.findByRoleId(roleId);
        let res = await getDungeonData(role);
        return resResult(STATUS.SUCCESS, res);
    }

    // 购买每日次数
    async buyNum(msg: { count: number }, session: BackendSession) {
        let { count } = msg;
        if( !count || count < 0) return resResult(STATUS.WRONG_PARMS);
        const roleId = session.get('roleId');
        const sid = session.get('sid');

        let { dungeonCnt = 0, dungeonBuyCnt = 0, dungeonRefTime } = await RoleModel.findByRoleId(roleId);
        let curTime = new Date();
        let needRefresh = shouldRefresh(dungeonRefTime, curTime);
        if(needRefresh) {
            dungeonCnt = 0; dungeonBuyCnt = 0;
        }

        let timesCanBuy = dicParam.DUNGEON_CONST.DUNGEON_CONST_BUY;
        let { vLv } = await RoleModel.findByRoleId(roleId);
        if(vLv > 0) {
            timesCanBuy += 0; // 暂留，用于处理vip等级增加最大购买次数
        }
        if(dungeonBuyCnt + count > timesCanBuy ) {
            return resResult(STATUS.DUNGEON_REFRESH_TIMES_LACK)
        }
        let buyCost = 0;
        for(let i = 1; i <= count; i++) {
            buyCost += getDungeonBuyCountCost(dungeonBuyCnt + i);
        }
        let {gold} = await RoleModel.findByRoleId(roleId);
        if(buyCost > gold) {
            return resResult(STATUS.DAILY_REFRESH_GOLD_LACK);
        }
        await handleCost(roleId, sid, [getGoldObject(buyCost)]);
        await RoleModel.buyCnt(roleId, needRefresh, count, curTime);

        let nextCostGold = getDungeonBuyCountCost(dungeonBuyCnt + count + 1);
        return resResult(STATUS.SUCCESS, { 
            costGold: buyCost,
            nextCostGold,
            battleCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_FREE + dungeonBuyCnt + count - dungeonCnt, 
            buyCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_BUY - dungeonBuyCnt - count
        });
    }

    // 购买每日次数
    async getFirstInfo(msg: { movePoint: number }, session: BackendSession) {
        const { movePoint } = msg;
        const serverId: number = session.get('serverId');
        const firstInfos = await DungeonFirstModel.findByMovePoint(serverId, movePoint);
        let result: DungeonResultParam[] = firstInfos.map(firstInfo => {
            return new DungeonResultParam(firstInfo);
        });
        return resResult(STATUS.SUCCESS, { infos: result });
    }
}