#pragma once

#include <optional>
#include <string>
#include <vector>

#include <QLabel>
#include <QPushButton>
#include <QTimer>
#include <QWidget>

#include "entt/entity/entity.hpp"

#include "ArenaSimulation.h"
#include "EntitySelectionChangedEvent.h"
#include "EventHandler.h"
#include "GameConfig.h"
#include "GameSpeedChangedEvent.h"
#include "VisualsConfig.h"

class ArenaView;
class ShipStatsPanel;

class InspectWindow : public QWidget,
                      public CombinedEventHandler<EntitySelectionChangedEvent,
                                                  GameSpeedChangedEvent>
{
    Q_OBJECT

public:
    InspectWindow(ArenaSimulation* sim, const GameConfig* config,
                  const VisualsConfig* visuals,
                  const std::string& arenaName, QWidget* parent = nullptr);
    ~InspectWindow() override;

protected:
    void closeEvent(QCloseEvent* event) override;
    void keyPressEvent(QKeyEvent* event) override;

private:
    void handleEvent(std::shared_ptr<const EntitySelectionChangedEvent> event) override;
    void handleEvent(std::shared_ptr<const GameSpeedChangedEvent> event) override;

private slots:
    void onSpeedButton(int index);
    void pollStatus();

private:
    void updateInfoPanel(const ArenaStatus& status);
    void refreshEntityStats();

    ArenaSimulation*   m_sim;
    const GameConfig*  m_config;
    ArenaView*         m_arenaView;

    std::vector<QPushButton*> m_speedButtons;
    QLabel* m_durationLabel;
    QLabel* m_team1Header;
    QLabel* m_team2Header;
    QLabel* m_team1Threat;
    QLabel* m_team2Threat;
    QLabel* m_team1Ehp;
    QLabel* m_team2Ehp;
    QLabel* m_team1Content;
    QLabel* m_team2Content;
    QTimer* m_pollTimer;

    std::optional<entt::entity> m_selectedEntity;
    QLabel*         m_entityTitleLabel;
    ShipStatsPanel* m_entityStatsPanel;
    QLabel*         m_stationStatsLabel;

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