#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 <QPushButton>
#include <QPointer>
#include <QThreadPool>
#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;
    void paintEvent(QPaintEvent *event) override;

signals:
    void thumbnailClicked(const QString &filePath);

private:
    QString m_filePath;
    QString m_displayName; // pre-truncated filename for painting
};

class ImageGallery : public QWidget
{
    Q_OBJECT
    
public:
    explicit ImageGallery(QWidget *parent = nullptr);
    ~ImageGallery();
    
    void setDatabaseManager(DatabaseManager *dbManager);
    void displayImages(const QList<DatabaseManager::ImageItem> &images, bool clearExisting = true);
    void appendImages(const QList<DatabaseManager::ImageItem> &images);
    void clearGallery();
    void loadMoreImages();
    void setPageSize(int pageSize); // Set the number of images to load per page
    void resetPagination(); // Reset pagination state to initial values
    
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
    void handleSearchResults(const QList<DatabaseManager::ImageItem> &results, const QString &searchText, 
                            int offset, int limit, int totalCount);
    void handleSearchStarted(const QString &searchText);
    void updateSearchTextDisplay(const QString &searchText); // Update UI with search text without performing search
    void handleScrolledToBottom();
    void handleLoadMoreClicked();
    
protected:
    void resizeEvent(QResizeEvent *event) override;
    bool eventFilter(QObject *watched, QEvent *event) override;
    void wheelEvent(QWheelEvent *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
    
    // Pagination variables
    int m_currentOffset;
    int m_pageSize;
    int m_totalCount;
    bool m_isLoading;
    bool m_hasMoreImages;
    QLabel *m_loadingIndicator;
    QPushButton *m_loadMoreButton;
    
    void loadThumbnailAsync(ImageThumbnail *label, const QString &filePath);
    QPixmap createPlaceholderThumbnail(int width, int height, const QString &message);
    QString m_lastSearchQuery; // Track the last search text
    
    // Track scroll position
    int m_lastScrollPosition;
    QTimer *m_scrollTimer;
    
    // Lazy loading constants
    static constexpr int DEFAULT_PAGE_SIZE = 20;
    static constexpr int LOAD_THRESHOLD_PX = 100; // Pixels from bottom to trigger load
};

#endif // IMAGEGALLERY_H