#include "RecipeProductionContent.h"

#include <QVBoxLayout>

#include "BuildingTarget.h"
#include "GameConfig.h"
#include "RecipeSelectionControl.h"

RecipeProductionContent::RecipeProductionContent(const SelectionContext& context,
                                                 const SelectionRequest& request,
                                                 QWidget* parent)
    : BufferedBuildingContent(context, request.buildings.front(), parent)
{
    const BuildingTarget target = resolveBuildingTarget(context, getBuildingId());

    // Shown for a construction site too: a site is configured exactly like the building
    // it will become (REQ-BLD-SITE-CONFIG).
    m_recipeControl = new RecipeSelectionControl(context, getBuildingId(), target.type,
                                                 this);
    getConfigurationLayout()->insertWidget(0, m_recipeControl);
}

void RecipeProductionContent::refreshControls(const BuildingTarget& target)
{
    m_recipeControl->setRecipeId(target.recipeId);
}

BufferedBuildingContent::CycleInfo RecipeProductionContent::getCycleInfo(
    const BuildingTarget& target) const
{
    CycleInfo info;
    const RecipeDef* recipe = target.recipeId.empty()
        ? nullptr
        : getContext().config->recipes.findRecipeDef(target.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.runsProduction  = true;
    info.durationSeconds = recipe->durationSeconds;
    return info;
}
