#pragma once

#include <optional>

#include <QDialog>

class BlueprintLibrary;
class ItemIconCache;
class QGridLayout;
class QWidget;

// The blueprint selection dialog (REQ-UI-BLUEPRINT-DIALOG): a frameless modal panel
// showing every saved blueprint as a card in a scrolling two-column grid. The caller
// pauses the game and raises the dim overlay while it is open.
//
// Clicking a card accepts the dialog and reports that blueprint's index; the caller
// enters placement mode afterwards, so the dialog is already closed by then
// (REQ-UI-BLUEPRINT-CARD). Deleting acts on the library immediately and leaves the
// dialog open (REQ-UI-BLUEPRINT-DELETE). Escape and the close button dismiss it with
// no other effect.
class BlueprintSelectionDialog : public QDialog
{
    Q_OBJECT

public:
    // Neither the library nor the icon cache is owned; both outlive the dialog.
    BlueprintSelectionDialog(BlueprintLibrary* library, ItemIconCache* itemIcons,
                             QWidget* parent = nullptr);

    std::optional<int> getChosenIndex() const;

private:
    void rebuildGrid();
    void onCardClicked(int index);
    void onDeleteClicked(int index);

    BlueprintLibrary*  m_library;
    ItemIconCache*     m_itemIcons;
    QWidget*           m_gridContainer;
    QGridLayout*       m_grid;
    std::optional<int> m_chosenIndex;  // nullopt = dismissed without picking
};
