#include "AutoProductionContent.h"

#include "Building.h"
#include "BuildingTarget.h"
#include "GameConfig.h"

AutoProductionContent::AutoProductionContent(const SelectionContext& context,
                                             const SelectionRequest& request,
                                             QWidget* parent)
    : BufferedBuildingContent(context, request.buildings.front(), parent)
{
}

BufferedBuildingContent::CycleInfo AutoProductionContent::getCycleInfo(
    const BuildingTarget& target) const
{
    CycleInfo info;
    // An auto-recipe building always runs an implicit recipe (REQ-BLD-SMELTER,
    // REQ-BLD-REPROCESSING), so its production section is always shown -- but only a
    // running cycle names a recipe, so while it is idle there is no cycle to describe.
    info.runsProduction = true;
    if (!target.building || !target.building->production.has_value())
    {
        return info;
    }

    const RecipeDef* recipe = getContext().config->recipes.findRecipeDef(
        target.building->production->recipeId, target.type);
    if (!recipe)
    {
        return info;
    }

    for (const RecipeIngredient& ingredient : recipe->inputs)
    {
        info.perCycleInputs[ingredient.item] = ingredient.amount;
    }
    for (const RecipeOutput& output : recipe->outputs)
    {
        info.perCycleOutputs[output.item] = output.amount;
    }
    info.durationSeconds = recipe->durationSeconds;
    return info;
}
