#include "BeltContent.h"

#include <QVBoxLayout>

#include "BeltItemList.h"
#include "BuildingTarget.h"
#include "ClearBeltControl.h"
#include "SelectionNames.h"

BeltContent::BeltContent(const SelectionContext& context,
                         const SelectionRequest& request, QWidget* parent)
    // Only an operational tile aggregates, so a card showing several is never a site;
    // a single selected tile still can be one.
    : SelectionContent(context,
                       request.buildings.size() == 1
                           ? asConstructionSite(context, request.buildings.front())
                           : std::nullopt,
                       parent)
    , m_ids(request.buildings)
{
    // What is there, then the button that removes it (REQ-UI-BELT-ITEMS,
    // REQ-UI-BELT-CLEAR).
    m_itemList = new BeltItemList(context, m_ids, this);
    getRuntimeLayout()->addWidget(m_itemList);
    getRuntimeLayout()->addWidget(new ClearBeltControl(context, m_ids, this));
}

void BeltContent::refreshConfiguration()
{
    const BuildingTarget target = resolveBuildingTarget(getContext(), m_ids.front());
    if (!target.isValid())
    {
        return;
    }

    // An aggregated selection may mix belts with tunnel ends and splitters, so it is
    // named after the first tile; the count says how many are held
    // (REQ-UI-SELECTION-AGGREGATE).
    setBuildingIdentity(target.type, getBuildingTypeName(target.type));
    if (m_ids.size() > 1)
    {
        setCountSlot(static_cast<int>(m_ids.size()));
    }
}

void BeltContent::refreshRuntime()
{
    m_itemList->refresh();
}
