#ifndef COMPOSEVIEW_H
#define COMPOSEVIEW_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 "models/EmailCompositionModel.h"

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

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;
};

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

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);
    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();

private:
    void setupUI();
    EmailCompositionModel *m_compositionModel;
    // 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;
    bool m_ccVisible = false;
    bool m_bccVisible = false;
};

#endif // COMPOSEVIEW_H