#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QLabel>
#include <QStatusBar>
#include <QTimer>
#include <QDebug>
#include "databasemanager.h"
#include "imagegallery.h"
#include "updatedatabasedialog.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected:
    void resizeEvent(QResizeEvent *event) override;
    
private slots:
    void handleSearchTextChanged();
    void performSearch();
    void updateStatusBar();
    void handleTypingInactivityTimeout();
    void handleOpenFile();
    void handleOpenFileWith();
    void handleSettings();
    void handleUpdateDatabase();
    void applySettings();
    void loadSettings();

private:
    void createLayout();
    void initializeDatabase();
    void displayAllImages();
    
    // UI Components
    QWidget *m_centralWidget;
    QVBoxLayout *m_mainLayout;
    QLineEdit *m_searchBar;
    ImageGallery *m_imageGallery;
    
    // Data
    DatabaseManager *m_dbManager;
    QString m_lastSearchText;
    QTimer *m_typingTimer;
    QTimer *m_searchingAnimationTimer;
    int m_searchingDots;
    bool m_isTyping;
    bool m_hasValidDatabase;
    
    // Settings
    QString m_databasePath;
    QString m_screenshotsDir;
    int m_imagePreloadCount;
    
    // OCR update dialog (non-modal – keep pointer so it stays alive)
    UpdateDatabaseDialog *m_updateDialog = nullptr;

    // Settings file path
    static const QString CONFIG_FILE_PATH;
    
    // Constants
    static constexpr int SEARCH_DELAY_MS = 300; // Delay for search typing
    static constexpr int DEFAULT_WINDOW_WIDTH = 1200;
    static constexpr int DEFAULT_WINDOW_HEIGHT = 800;
    static constexpr int DEFAULT_PRELOAD_COUNT = 20;
};

#endif // MAINWINDOW_H