#pragma once

#include <QWidget>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <vector>
#include <memory>

namespace scar {

class ScreenCapture;
class ClientConnection;

struct VideoStream {
    QString streamId;
    QString username;
    QLabel* videoLabel;
};

class VideoGridWidget : public QWidget {
    Q_OBJECT

public:
    explicit VideoGridWidget(QWidget* parent = nullptr);
    ~VideoGridWidget();
    
    void setConnection(ClientConnection* connection);
    void addStream(const QString& streamId, const QString& username);
    void removeStream(const QString& streamId);
    void updateFrame(const QString& streamId, const QPixmap& frame);
    void clear();
    
    int streamCount() const { return streams_.size(); }

public slots:
    void onScreenShareFrameReceived(std::vector<uint8_t> rgbData, int width, int height);

signals:
    void screenShareRequested();

private slots:
    void onScreenShareClicked();

private:
    void resizeEvent(QResizeEvent* event) override;
    void updateScreenShareButtonPosition();
    void updateGridLayout();
    int calculateColumns(int streamCount);
    
    QGridLayout* gridLayout_;
    QLabel* placeholderLabel_;
    std::vector<VideoStream> streams_;
    QPushButton* screenShareButton_;
    ScreenCapture* screenCapture_;
    ClientConnection* connection_;
    bool isScreenSharing_;
    bool screenShareStartSent_;
    
    static constexpr int MAX_STREAMS = 256;
};

} // namespace scar
