/**
 * 每日本相关
 */

import DailyRecord, { DailyRecordModel } from '../db/DailyRecord';
import { resResult } from '../pubUtils/util';
import { STATUS } from '../consts/statusCode';
import { RoleModel, RoleType } from '../db/Role';
import { gameData } from '../pubUtils/data';
import { DicWar } from '../pubUtils/dictionary/DicWar';
import { WarStar } from '../domain/dbGeneral';

/**
 * 获取全部每日关卡列表
 * @param role 
 */
export async function getDailyBattleList(role: RoleType) {
    let { roleId, warStar } = role;
    let dicDaily = gameData.daily;

    let result = new Array();
    for(let {dailyType: type, name, timesPerDay, timesCanBuy } of dicDaily) {
        let refreshResult = await DailyRecordModel.refreshRecord(roleId, type);
        let wars: {battleId: number, cost: number, star: number, status: number, name: string}[] = new Array();
        let dicDailyWar = gameData.dailyWarByType.get(type);
        for(let dic of dicDailyWar) {
            let war = getDailyWarStatus(dic, warStar);
            wars.push(war);
        }
        let checkDailyResult = await getDailyNum(refreshResult, timesPerDay, timesCanBuy);
        result.push({
            type, name, ...checkDailyResult,
            wars
        });
    }
    return result;
}

function getDailyWarStatus(dicDailyWar: DicWar, warStar: WarStar[]) {
    let {war_id, cost, gk_name, previousGk } = dicDailyWar;
    let status = 0, star = 0;
    let curBattle = warStar.find(cur => cur.id == war_id);
    if(curBattle) {
        status = 2;
        star = curBattle.star;
    } else {
        if (previousGk) {
            let preBattleRecord = warStar.find(cur => cur.id == previousGk);
            if(preBattleRecord) {
                status = 1;
            } else {
                status = 0;
            }
        } else {
            status = 1;
        }
    }
    return {
        battleId: war_id, cost, star, status, name: gk_name
    }
}

// 检查每日本次数checkBattle使用
export async function checkDaily(roleId: string, battleId: number, inc: number) {
    let dailyWar = gameData.war.get(battleId);
    if(!dailyWar) return { status: -1, resResult: resResult(STATUS.DAILY_WAR_NOT_FOUND) };
    let type = dailyWar.dailyType;

    let curDaily = gameData.daily.find(cur => cur.dailyType == type);
    if(!curDaily) return { status: -1, resResult: resResult(STATUS.DAILY_TYPE_NOT_FOUND) };
    let dailyRecord = await DailyRecordModel.refreshRecord(roleId, type);
    let { count = 0, buyCount = 0 } = dailyRecord;
    if(count + inc > curDaily.timesPerDay + buyCount ) {
        return { status: -1, resResult: resResult(STATUS.DAILY_TIMES_LACK) }
    }
    let checkDailyResult = await getDailyNum(dailyRecord, curDaily.timesPerDay, curDaily.timesCanBuy);
    return {status: 1, data: {type, ...checkDailyResult }};
}

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

export async function checkDailyAndIncrease(roleId: string, warStar: WarStar[], battleId: number, inc: number, isRef: boolean) {
    let dailyWar = gameData.war.get(battleId);
    if(!dailyWar) return { status: -1, resResult: resResult(STATUS.DAILY_WAR_NOT_FOUND) };
    let type = dailyWar.dailyType;
    let war = getDailyWarStatus(dailyWar, warStar);

    let curDaily = gameData.daily.find(cur => cur.dailyType == type);
    if(!curDaily) return { status: -1, resResult: resResult(STATUS.DAILY_TYPE_NOT_FOUND) };

    let dailyRecord: any;
    if(isRef) { 
        dailyRecord = await DailyRecordModel.refreshRecord(roleId, type);
    } else { // 如果是手动挑战，由于在进入时已经检查过了，所以即使结算时过去了一天也还是累计在上一天
        dailyRecord = await DailyRecordModel.getDailyRecordById(roleId, type);
    }
    let { count = 0, buyCount = 0 } = dailyRecord;
    if(count + inc > curDaily.timesPerDay + buyCount ) {
        return { status: -1, resResult: resResult(STATUS.DAILY_TIMES_LACK) }
    }
    let result = await DailyRecordModel.increseDailyCount(roleId, type, inc);
    let checkDailyResult = await getDailyNum(result, curDaily.timesPerDay, curDaily.timesCanBuy);
    return {status: 1, data: {type, ...checkDailyResult, war}};
}

export async function getDailyNum(dailyRecord: DailyRecord, timesPerDay?: number, timesCanBuy?: number) {
    let {roleId, type, count = 0, buyCount = 0} = dailyRecord;
    if(!timesPerDay || !timesCanBuy) {
        const curDaily = gameData.daily.find(cur => cur.dailyType == type);
        timesPerDay = curDaily.timesPerDay;
        timesCanBuy = curDaily.timesCanBuy;
    }
    let {vLv} = await RoleModel.findByRoleId(roleId);
    if(vLv > 0) {
        timesCanBuy += 0; // 暂留，用于处理vip等级增加最大购买次数
    }
    return {count: timesPerDay - count + buyCount, buyCount: timesCanBuy - buyCount}
}