#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 "GameConfig.h"
#include "GameSpeedChangedEvent.h"
#include "ItemTooltipContext.h"
#include "Tick.h"
#include "TickAdvancedEvent.h"

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

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

public:
    // The context carries the simulation the bar reads, the config, and the icon caches
    // -- the item cache draws the building_block icon in the stock display
    // (REQ-UI-BLOCKS-ICON), and the stock display explains that item with the cache's
    // help (REQ-UI-ITEM-VALUE-TOOLTIP). Nothing in it is owned; all of it must outlive
    // this widget.
    explicit HeaderBar(const ItemTooltipContext& context, 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 GameSpeedChangedEvent> event) override;
    void handleEvent(std::shared_ptr<const BossWaveUpdatedEvent> event) override;
    void handleEvent(std::shared_ptr<const ArtifactCountChangedEvent> event) override;

    // 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();

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

    // Not owned; all of it lives in MainWindow. The simulation it names is the single
    // source of truth for everything the header displays; the change events are only
    // refresh signals.
    ItemTooltipContext       m_context;

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