#pragma once

#include <memory>
#include <string>
#include <vector>

#include <QPixmap>
#include <QWidget>

#include "ArtifactCountChangedEvent.h"
#include "BossWaveUpdatedEvent.h"
#include "BuildingBlocksChangedEvent.h"
#include "EventHandler.h"
#include "ExpansionCostChangedEvent.h"
#include "GameConfig.h"
#include "GameSpeedChangedEvent.h"
#include "Tick.h"
#include "TickAdvancedEvent.h"

class QLabel;
class QPushButton;
class ItemIconCache;
class Simulation;

class HeaderBar : public QWidget,
                  public CombinedEventHandler<TickAdvancedEvent,
                                              BuildingBlocksChangedEvent,
                                              ExpansionCostChangedEvent,
                                              GameSpeedChangedEvent,
                                              BossWaveUpdatedEvent,
                                              ArtifactCountChangedEvent>
{
    Q_OBJECT

public:
    // itemIcons is the window-wide per-item icon cache (REQ-UI-ITEM-ICON); used to
    // show the building_block icon in the stock display and expand button
    // (REQ-UI-BLOCKS-ICON, REQ-UI-EXPAND-BUTTON). Not owned; must outlive this widget.
    HeaderBar(const Simulation* sim, const GameConfig* config,
              ItemIconCache* itemIcons, QWidget* parent = nullptr);
    ~HeaderBar() override;

private slots:
    void onSpeedButton(int index);

private:
    void handleEvent(std::shared_ptr<const TickAdvancedEvent> event) override;
    void handleEvent(std::shared_ptr<const BuildingBlocksChangedEvent> event) override;
    void handleEvent(std::shared_ptr<const ExpansionCostChangedEvent> event) override;
    void handleEvent(std::shared_ptr<const GameSpeedChangedEvent> event) override;
    void handleEvent(std::shared_ptr<const BossWaveUpdatedEvent> event) override;
    void handleEvent(std::shared_ptr<const ArtifactCountChangedEvent> event) override;

    // Refreshes the Expand button caption and enabled state from the current
    // expansion cost and building block stock (REQ-UI-EXPAND-BUTTON).
    void updateExpandButton();

    // Refreshes the building blocks stock display from the simulation:
    // `Stock: <n>` with the building_block icon after it, or the
    // `Stock: <n> Blocks` text fallback when no icon file exists
    // (REQ-UI-BLOCKS-ICON).
    void updateBlocksLabel();

    // The building_block icon at the header's text height, or a null pixmap when no
    // icon file exists. Loaded once via m_itemIcons on first use.
    QPixmap blockIcon() const;

    QLabel*                  m_timeLabel;
    QLabel*                  m_blocksLabel;
    QLabel*                  m_artifactsLabel;
	QLabel*                  m_bossWaveLabel;
	QLabel*                  m_nextBossLabel;
    QPushButton*             m_expandButton;
    std::vector<QPushButton*> m_speedButtons;

    ItemIconCache*           m_itemIcons;  // Not owned; lives in MainWindow.

    // The simulation is the single source of truth for everything the header
    // displays; the change events are only refresh signals.
    const Simulation*        m_sim;

    static const double kSpeeds[];
    static const int    kSpeedCount;
};
