/**
 * 每日本相关
 */

import { resResult, shouldRefresh, } from '../pubUtils/util';
import { STATUS } from '../consts/statusCode';
import { RoleModel, RoleType } from '../db/Role';
import * as dicParam from '../pubUtils/dicParam';
import { RankParam } from '../domain/rank';
import { BattleRecordType } from '../db/BattleRecord';
import { gameData } from '../pubUtils/data';
import { DungeonFirstModel } from '../db/DungeonFirst';
import { nowSeconds } from '../pubUtils/timeUtil';

/**
 * 获取秘境本数据
 * @param role 
 */
export async function getDungeonData(role: RoleType) {
    let { dungeonCnt = 0, dungeonBuyCnt = 0, dungeonRefTime, dungeonHeroes=[] } = role;
    let curTime = new Date();
    if(shouldRefresh(dungeonRefTime, curTime)) {
        dungeonCnt = 0; dungeonBuyCnt = 0;
    }

    let nextCostGold = getDungeonBuyCountCost(dungeonBuyCnt + 1);
    return {
        nextCostGold,
        dungeonHeroes,
        battleCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_FREE + dungeonBuyCnt - dungeonCnt, 
        buyCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_BUY - dungeonBuyCnt 
    }
}

// 检查秘境本次数checkBattle使用
export async function checkDungeonNum(roleId: string, inc: number) {
    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;
    }
    if(dungeonCnt + inc > dicParam.DUNGEON_CONST.DUNGEON_CONST_FREE + dungeonBuyCnt) {
        return {status: -1, resResult: resResult(STATUS.DUNGEON_TIMES_LACK)}
    }
    await RoleModel.increaseDungeonCnt(roleId, needRefresh, 0, curTime);

    return { status: 0, data: { 
        battleCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_FREE + dungeonBuyCnt - dungeonCnt, 
        buyCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_BUY - dungeonBuyCnt 
    }};
}

/**
 * 检查秘境本次数并添加，warEnd和warSweep使用
 * @param roleId 
 * @param inc 增加的次数
 * @param isRef 在计算之前是否检查每日的刷新
 */

export async function checkDungeonAndIncrease(roleId: string, inc: number, isRef: boolean) {

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

    let needRefresh = isRef && shouldRefresh(dungeonRefTime, curTime);
    if(needRefresh) {
        dungeonCnt = 0; dungeonBuyCnt = 0;
    }
    
    if(dungeonCnt + inc > dicParam.DUNGEON_CONST.DUNGEON_CONST_FREE + dungeonBuyCnt ) {
        return { status: -1, resResult: resResult(STATUS.DUNGEON_TIMES_LACK) }
    }
    // console.log('needRefresh', needRefresh);
    await RoleModel.increaseDungeonCnt(roleId, needRefresh, inc, curTime);
    return {status: 1, data: {
        battleCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_FREE + dungeonBuyCnt - dungeonCnt - inc, 
        buyCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_BUY - dungeonBuyCnt 
    }};
}

/**
 * 秘境购买次数的花费
 * @param num 如果没有买过次数，那么这里算第一次，传1
 * @returns 
 */
export function getDungeonBuyCountCost(num:number) {
    let cost = (num - 1) * dicParam.DUNGEON_CONST.DUNGEON_CONST_UP + dicParam.DUNGEON_CONST.DUNGEON_CONST_BASE;
    if(cost > dicParam.DUNGEON_CONST.DUNGEON_CONST_TOP) {
        cost = dicParam.DUNGEON_CONST.DUNGEON_CONST_TOP;
    }
    return cost;
}

export async function saveDungeonFirst(role: RoleType, warId: number, battleRecord: BattleRecordType) {
    let userInfo = new RankParam(role, true);
    let lineup = battleRecord.record.lineup;
    let dicWar = gameData.war.get(warId);
    await DungeonFirstModel.createDungeonFirst(role.serverId, dicWar.movePoint, warId, {
        time: nowSeconds(), roleId: role.roleId, userInfo, lineup
    })
}