#ifndef COMPOSE_VIEW_H
#define COMPOSE_VIEW_H

#include <QWidget>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QToolBar>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QFrame>
#include <QMenu>
#include <QAction>
#include <QToolButton>
#include <QDateTimeEdit>
#include <QFontComboBox>
#include <QSpinBox>
#include <QComboBox>
#include "models/EmailCompositionModel.h"
#include "services/accountservice.h"

class AccountService;

class RichTextEditor : public QTextEdit {
    Q_OBJECT
public:
    explicit RichTextEditor(QWidget *parent = nullptr);
    void setupToolbar(QVBoxLayout *layout);

signals:
    void signatureClicked();
    void signatureEditRequested();

public slots:
    void onBold();
    void onItalic();
    void onUnderline();
    void onBulletList();
    void onNumberedList();
    void onIndent();
    void onOutdent();
    void onAlignLeft();
    void onAlignCenter();
    void onAlignRight();
    void onAlignJustify();
    void onFontChanged(const QFont &font);
    void onFontSizeChanged(int size);
    void onInsertImage();
    void onInsertTable();

private:
    QToolBar *m_toolbar;
    QFontComboBox *m_fontCombo;
    QSpinBox *m_fontSizeSpin;
    QToolButton *m_signatureButton;
    QMenu *m_signatureMenu;
};

class ComposeView : public QWidget {
    Q_OBJECT
public:
    explicit ComposeView(QWidget *parent = nullptr);
    ~ComposeView() override;

    void setAccountService(AccountService *service);

signals:
    void compositionFinished();
    void detachRequested(QWidget *widget);
    void sendRequested(const QString &to, const QString &cc, const QString &bcc,
                       const QString &subject, const QString &body,
                       const QDateTime &scheduleTime,
                       const QString &fromAddress);
    void discardRequested();

public slots:
    void initializeComposition();
    void startNewEmail(const QString &initialRecipient);
    void setTo(const QString &to);
    void setSubject(const QString &subject);
    void setBody(const QString &body);

private slots:
    void onCcToggle();
    void onBccToggle();
    void onSendClicked();
    void onScheduleClicked();
    void onDetachClicked();
    void onAccountChanged(int index);
    void onSignatureClicked();
    void onSignatureEditRequested();

private:
    void setupUI();
    void populateAccountCombo();
    void loadSignatureForCurrentAccount();

    EmailCompositionModel *m_compositionModel;
    AccountService *m_accountService = nullptr;
    // UI components
    QLineEdit *m_toField = nullptr;
    QLineEdit *m_subjectField = nullptr;
    RichTextEditor *m_bodyEditor = nullptr;
    QPushButton *m_detachButton = nullptr;
    QWidget *m_ccRow = nullptr;
    QWidget *m_bccRow = nullptr;
    QWidget *m_schedulePanel = nullptr;
    QLineEdit *m_ccField = nullptr;
    QLineEdit *m_bccField = nullptr;
    QPushButton *m_ccButton = nullptr;
    QPushButton *m_bccButton = nullptr;
    QPushButton *m_hideCcButton = nullptr;
    QPushButton *m_hideBccButton = nullptr;
    QDateTimeEdit *m_schedulePicker = nullptr;
    QPushButton *m_scheduleSendButton = nullptr;
    QPushButton *m_discardButton = nullptr;
    QMenu *m_sendMenu = nullptr;
    QAction *m_sendNowAction = nullptr;
    QAction *m_scheduleAction = nullptr;
    QToolButton *m_sendSplit = nullptr;
    QComboBox *m_accountCombo = nullptr;
    bool m_ccVisible = false;
    bool m_bccVisible = false;
    int m_currentAccountId = -1;
};

#endif // COMPOSE_VIEW_H