#pragma once
#include <wx/wx.h>
#include <wx/spinctrl.h>
#include <wx/listctrl.h>
#include <map>
#include <string>
#include <vector>
#include "model/PlacedPiece.h"
#include "model/TrackPiece.h"
#include "geometry/Vec2.h"

class MainFrame;
namespace MC { struct Helix; }

class PropertiesPanel : public wxPanel {
public:
    PropertiesPanel(wxWindow* parent, MainFrame* frame);
    void RefreshProperties();
    void RefreshTally();
    void FocusLabelField(MC::Vec2 worldPos);

private:
    void ShowEmpty();
    void ShowPieceProperties(const MC::PlacedPiece& piece, const MC::TrackPiece& def);
    void ShowHelixProperties(const MC::Helix& h);
    void ShowStraightChain(const std::vector<MC::PlacedPiece*>& pieces);
    void ShowSceneryProperties(int sceneryId);
    void ShowShapeProperties(int shapeId);
    void RebuildSizer();

    void OnLevelChanged(wxCommandEvent&);
    void OnBuyTrack(wxCommandEvent&);
    void OnBuyTrackHelp(wxCommandEvent&);
    void OnLabelOK(wxCommandEvent&);

    MainFrame*     m_frame       = nullptr;
    wxBoxSizer*    m_sizer       = nullptr;

    wxStaticText*  m_lblName     = nullptr;
    wxStaticText*  m_lblInfo     = nullptr;
    wxStaticText*  m_lblHeight   = nullptr;
    wxStaticText*  m_lblHeightVal = nullptr;   // read-only height display
    wxStaticText*  m_lblGrade    = nullptr;
    wxStaticText*  m_lblGradeVal = nullptr;
    wxStaticText*  m_lblWarn     = nullptr;
    wxChoice*      m_levelChoice = nullptr;
    wxListCtrl*    m_tallyList   = nullptr;
    std::map<std::string,int> m_tallyCounts;

    // Label placement
    wxTextCtrl*       m_txtLabel        = nullptr;
    wxButton*         m_btnLabel        = nullptr;
    wxChoice*         m_colorChoice     = nullptr;

    void OnColorApply(wxCommandEvent&);
    MC::Vec2          m_labelPos        = {0, 0};
    bool              m_labelPosSet     = false;

    int            m_selectedId = -1;
    int            m_selectedSceneryId = -1;   // set when a scenery item is shown
    int            m_selectedHelixId   = -1;   // set when a helix is shown
    int            m_selectedShapeId   = -1;   // set when a free-form polygon shape is shown

    wxDECLARE_EVENT_TABLE();
};
