/**
 * 每日本相关
 */

import { resResult, shouldRefresh, } from '../pubUtils/util';
import { STATUS } from '../consts/statusCode';
import { RoleModel, RoleType, WarStar } 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';
import { getVipDungeonCnt } from './activity/monthlyTicketService';

/**
 * 获取秘境本数据
 * @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);
    let freeCount = getVipDungeonCnt(role.vipStartTime);
    return {
        nextCostGold,
        dungeonHeroes,
        battleCount: freeCount + dungeonBuyCnt - dungeonCnt, 
        buyCount: dicParam.DUNGEON_CONST.DUNGEON_CONST_BUY - dungeonBuyCnt 
    }
}

function isFirstPass(warStar: WarStar[], warId: number) {
    let index = warStar.findIndex(cur => cur.id == warId);
    return index == -1;
}

// 检查秘境本次数checkBattle使用
export async function checkDungeonNum(roleId: string, inc: number, warId: number) {
    let { dungeonCnt = 0, dungeonBuyCnt = 0, dungeonRefTime, vipStartTime, warStar } = await RoleModel.findByRoleId(roleId);
    if(isFirstPass(warStar, warId)) inc = 0;

    let curTime = new Date();
    let needRefresh = shouldRefresh(dungeonRefTime, curTime);
    if(needRefresh) {
        dungeonCnt = 0; dungeonBuyCnt = 0;
    }
    let freeCount = getVipDungeonCnt(vipStartTime);
    if(dungeonCnt + inc > freeCount + dungeonBuyCnt) {
        return {status: -1, resResult: resResult(STATUS.DUNGEON_TIMES_LACK)}
    }
    await RoleModel.increaseDungeonCnt(roleId, needRefresh, 0, curTime);

    return { status: 0, data: { 
        battleCount: freeCount + 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, warId: number) {

    let { dungeonCnt = 0, dungeonBuyCnt = 0, dungeonRefTime, vipStartTime, warStar } = await RoleModel.findByRoleId(roleId);
    if(isFirstPass(warStar, warId)) inc = 0;
    let curTime = new Date();

    let needRefresh = isRef && shouldRefresh(dungeonRefTime, curTime);
    if(needRefresh) {
        dungeonCnt = 0; dungeonBuyCnt = 0;
    }
    let freeCount = getVipDungeonCnt(vipStartTime);
    
    if(dungeonCnt + inc > freeCount + 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: freeCount + 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) {
    if(!role) return null;
    let userInfo = new RankParam(role);
    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
    })
}