import { gameData, getArmyDevelopConsumeById } from '../pubUtils/data';
import { ScienceTree, GuildRefineModel } from '../db/GuildRefine';
import { UserGuildType } from '../db/UserGuild';
import { shouldRefresh } from '../pubUtils/util';
import { Structure } from '../db/Guild';
import { GUILD_STRUCTURE } from '../consts';

export function checkEquipProduceStructureLv(structure: Structure[], developConsumeId: number) {
    let curStructure = structure.find(cur => cur.id == GUILD_STRUCTURE.EQUIP_PRODUCE);
    if(!curStructure) return curStructure;

    let dicDevelopConsume = getArmyDevelopConsumeById(developConsumeId);
    if(dicDevelopConsume.structureLevel > curStructure.lv) return false;

    return true;
}

/**
 * 开启符合条件的科技树
 * @param code 
 */
export async function openGuildRefine(code: string) {
    let developConsumes = gameData.armyDevelopConsume;
    let scienceTrees = new Array<ScienceTree>();
    developConsumes.forEach(developConsume=>{
        if (developConsume.fundConsume == 0 && developConsume.timeConsume == 0) {
            //无前置条件，无研发时间的科技树直接可以点亮
            let scienceTree = new ScienceTree();
            scienceTree.id = developConsume.id;
            scienceTree.endTime = 0
            scienceTree.assistRoleIds = [];
            scienceTrees.push(scienceTree);
        }
    });
    let guildRefine = await GuildRefineModel.createScienceTree(code, scienceTrees);
    return guildRefine;
}

export function refreshRefinCnt(userGuild: UserGuildType) {
    let { refRefineTime, refineCnt } = userGuild;
    if(shouldRefresh(refRefineTime, new Date())) {
        refRefineTime = new Date(), refineCnt = [];
    }
    return { refRefineTime, refineCnt };
}