#pragma once

#include <QMainWindow>
#include <QStackedWidget>
#include <QListWidget>
#include <QSplitter>
#include <QTreeView>
#include <QStatusBar>
#include <QProgressBar>
#include <QToolBar>
#include <QAction>
#include <QToolButton>
#include <QLabel>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QWindowStateChangeEvent>

#include "ui/readerview.h"
#include "ui/maillistview.h"
#include "ui/composeview.h"
#include "ui/settingsview.h"
#include "ui/contactsview.h"
#include "ui/calendarview.h"
#include "ui/models/FolderListModel.h"
#include "ui/models/EmailListModel.h"
#include "ui/categorytreewidget.h"
#include "ui/templatesmanagerdialog.h"
#include "services/accountservice.h"
#include "services/mailservice.h"

class MainMainWindow : public QMainWindow {
    Q_OBJECT

public:
    explicit MainMainWindow(QWidget *parent = nullptr);
    ~MainMainWindow() override = default;

protected:
    // Frameless window handling
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    bool nativeEvent(const QByteArray &eventType, void *message, qintptr *result) override;
    void changeEvent(QEvent *event) override;
    bool eventFilter(QObject *watched, QEvent *event) override;

private slots:
    void onNavChanged(int index);
    void onFolderSelected(const QModelIndex &index);
    void onEmailSelected(int mailId);
    void onComposeRequested();
    void onReaderReplyRequested(int mailId);
    void onReaderForwardRequested(int mailId);
    void onReaderDeleteRequested(int mailId);
    void onNewMessage();
    void openMailInIndependentWindow(int mailId);
    void onAddAccountRequested();
    void onAccountEditRequested(int accountId);
    void onAccountDeleteRequested(int accountId);
    void onProgressChanged(int percent);
    void onStatusMessage(const QString &msg);
    void onSyncRequested();
    void onSettingsRequested();
    void updateMaximizeButtonIcon();
    void onCategorySelected(const Category& cat);
    void onCategoryDoubleClicked(const Category& cat);
    void onAssignCategoryRequested(qint64 mailId, qint64 categoryId);
    void onCategoryCreated(const Category& cat);
    void onCategoryEdited(const Category& cat);
    void onCategoryDeleted(qint64 categoryId);
    void onRulesChanged();
    void onRunRulesOnFolder();
    void onRunRulesOnSelected();
    void showRulesManager();
    void showTemplatesManager();

    // Slots for embedded compose view
    void onEmbeddedSendRequested(const QString &to, const QString &cc, const QString &bcc,
                               const QString &subject, const QString &body,
                               const QDateTime &scheduleTime,
                               const QString &fromAddress,
                               const QStringList &attachmentPaths);
    void onEmbeddedDiscardRequested();
    void onEmbeddedDetachRequested(QWidget *widget);
private:
    void setupUI();
    void setupSidebar();
    void setupMailPage();
    void connectModels();
    void createTitleBar();
    void switchToPage(int pageIndex);
    QWidget *createTitleButton(const QString &iconName, const QString &tooltip, std::function<void()> slot);
    QWidget *createWindowControlButton(const QString &iconName, const QString &tooltip, std::function<void()> slot);
    void updateCategoryTreeForAccount(qint64 accountId);
    void assignCategoryToSelectedMails(qint64 categoryId);

    // Navigation
    QListWidget *m_sidebar;
    QStackedWidget *m_stack;

    enum Page {
        PageMail = 0,
        PageCompose,
        PageSettings,
        PageContacts,
        PageCalendar
    };

    // Mail page widgets
    QWidget *m_mailPage;
    QSplitter *m_folderSplitter;
    QTreeView *m_folderTree;
    CategoryTreeWidget *m_categoryTree;
    MailListView *m_mailListView;
    ReaderView *m_emailViewer;
    QStackedWidget *m_viewerStack;
    QWidget *m_placeholderWidget;
    ComposeView *m_embeddedComposeView;

    // Other pages
    ComposeView *m_composeView;
    SettingsView *m_settingsView;
    ContactsView *m_contactsView;
    CalendarView *m_calendarView;

    // Services & Models
    FolderListModel *m_folderModel;
    EmailListModel *m_emailModel;
    AccountService *m_accountService = nullptr;
    MailService *m_mailService = nullptr;

    // Frameless title bar
    QWidget *m_titleBar;
    QToolButton *m_maximizeButton;
    QToolButton *m_closeButton;
    QPoint m_dragPos;
    bool m_dragging = false;
    Qt::WindowStates m_lastWindowState;
    
    // Resize handling (item 3)
    QPoint m_resizeStartPos;
    QRect m_resizeStartGeom;
    bool m_resizing = false;
    Qt::Edges m_resizeEdges = Qt::Edges();

    int m_currentFolderId;
    int m_currentMailId;
};
