#ifndef IMAGEGALLERY_H
#define IMAGEGALLERY_H

#include <QWidget>
#include <QGridLayout>
#include <QLabel>
#include <QPixmap>
#include <QList>
#include <QScrollArea>
#include <QProcess>
#include <QDebug>
#include <QPushButton>
#include <QResizeEvent>
#include "databasemanager.h"

// Global constants
static const int THUMBNAIL_WIDTH = 256;
static const int THUMBNAIL_HEIGHT = 256;
static const int THUMBNAIL_SPACING = 2; // Reduced spacing between thumbnails

class ImageThumbnail : public QLabel
{
    Q_OBJECT
    
public:
    explicit ImageThumbnail(const QString &filePath, QWidget *parent = nullptr);
    
    QString getFilePath() const { return m_filePath; }

protected:
    void mousePressEvent(QMouseEvent *event) override;
    
signals:
    void thumbnailClicked(const QString &filePath);
    
private:
    QString m_filePath;
};

class ImageGallery : public QWidget
{
    Q_OBJECT
    
public:
    explicit ImageGallery(QWidget *parent = nullptr);
    ~ImageGallery();
    
    void setDatabaseManager(DatabaseManager *dbManager);
    void displayImages(const QList<DatabaseManager::ImageItem> &images);
    void clearGallery();
    
public slots:
    void handleSearchTextChanged(const QString &searchText);
    void handleThumbnailClicked(const QString &filePath);
    void handleContainerResized(); // New slot to handle resize events
    void updateGridLayout(); // Adjusts grid based on current window size
    
protected:
    void resizeEvent(QResizeEvent *event) override;
    bool eventFilter(QObject *watched, QEvent *event) override;
    
private:
    QGridLayout *m_gridLayout;
    QScrollArea *m_scrollArea;
    QWidget *m_containerWidget;
    DatabaseManager *m_dbManager;
    QList<ImageThumbnail*> m_thumbnails;
    int m_columnsCount;  // Dynamic column count based on window size
    
    QPixmap createThumbnail(const QString &filePath, int width, int height);
    QPixmap createPlaceholderThumbnail(int width, int height, const QString &message);
};

#endif // IMAGEGALLERY_H