#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QComboBox>
#include <QTabWidget>
#include <QPushButton>
#include <QWebEngineView>
#include <QWebSocketServer>
#include <QWebSocket>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QUuid>
#include <QSplitter>
#include <QScrollArea>
#include <QSet>
#include <QMap>
#include <QListWidget>
#include <QCheckBox>
#include <QTableWidget>
#include <QHeaderView>
#include <QLineEdit>
#include <QGroupBox>

#include "udpdiscoveryservice.h"
#include "opcuaserver.h"
#include "codeeditor.h"
#include "qconsole.h"
#include "qcled.h"

class ThumbnailWidget : public QWidget {
    Q_OBJECT
public:
    ThumbnailWidget(const QString& ip, QWidget* parent = nullptr);
    QWebEngineView* webView;
    QString ipAddress;
    QCheckBox* rearCamCb;
    QCheckBox* micCb;
    QCheckBox* flashCb;
    QCheckBox* buzzerCb;
    QPushButton* softResetBtn;
    bool isConnected;
    
    void setAudioVolume(int volume);
    void setAudioMuted(bool muted);

signals:
    void deleteRequested(const QString& ip);
    void flashToggled(const QString& ip, bool enable);
    void buzzerToggled(const QString& ip, bool enable);
    void softResetRequested(const QString& ip);
    void audioTrackStatusChanged(const QString& ip, bool available);
};

class MainWindow : public QMainWindow {
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void onNodesUpdated(const QList<NodeInfo>& nodes);
    void onNodeSelectionChanged();
    void onSaveClicked();
    void onPlayClicked();
    void onResetSocketsClicked();
    void createThumbnail(const QString& ip);
    void onDeleteNode(const QString& ip);
    
    void onUdpSignalingMessageReceived(const QString& senderIp, const QJsonObject& msg);

    void onNewWebSocketConnection();
    void onWebSocketMessageReceived(const QString &message);
    void onWebSocketDisconnected();

    void onAudioTrackSelectionChanged();
    void onMasterVolumeChanged(int value);
    void onMasterMuteToggled(bool checked);

    void onConnectSelectedClicked();
    void onConnectAllClicked();
    void onDisconnectSelectedClicked();
    void onManualConnectClicked();
    void onWatcherModeToggled(bool checked);
    void connectNode(const QString& ip);
    void disconnectNode(const QString& ip);

private:
    UdpDiscoveryService* m_discoveryService;
    OpcUaServer* m_opcUaServer;
    
    QWebSocketServer* m_webSocketServer;
    QList<QWebSocket*> m_clients;

    void setupUi();
    void setupSignalingServer();
    void broadcastSignalingMessage(const QJsonObject& msg, QWebSocket* exclude = nullptr);
    void sendSignalingMessage(QWebSocket* client, const QJsonObject& msg);
    void loadIniFile();
    void updateListWidget();
    void updateVoxAudioTrackList();
    void setupDarkStyle();

    bool m_isPlaying;

    QSet<QString> m_knownIps;
    QMap<QString, NodeInfo> m_nodeInfoMap;
    QMap<QString, ThumbnailWidget*> m_thumbnails;

    QSplitter* m_splitter;
    QTabWidget* m_topTabs;
    CodeEditor* m_codeEditor;
    QConsole* m_console;
    
    // Discovered Nodes UI panel
    QWidget* m_nodesPanel;
    QListWidget* m_nodeListWidget;
    QCLED* m_nodeStatusLed;
    QLabel* m_lblSelectedNodeName;
    QLabel* m_lblSelectedNodeIp;
    QTableWidget* m_nodesTable;
    QLineEdit* m_manualIpEdit;
    QPushButton* m_manualConnectBtn;
    QPushButton* m_connectSelectedBtn;
    QPushButton* m_connectAllBtn;
    QPushButton* m_disconnectSelectedBtn;
    QCheckBox* m_watcherModeCb;
    QLabel* m_watcherStatusLabel;

    // VOX Audio Track List UI components
    QWidget* m_voxPanel;
    QListWidget* m_audioTrackListWidget;
    QSlider* m_masterVolumeSlider;
    QCheckBox* m_masterMuteCb;
    QLabel* m_activeTrackLabel;
    
    QPushButton* m_saveBtn;
    QPushButton* m_loadBtn;
    QPushButton* m_playBtn;
    
    QWidget* m_thumbnailContainer;
    QLayout* m_thumbnailLayout; // Can be a flow layout or grid
};

#endif // MAINWINDOW_H
