#pragma once

#include "../synchronizer.h"
#include <QObject>
#include <QSslSocket>
#include <QStringList>
#include <QMap>

class Pop3Synchronizer : public Synchronizer
{
    Q_OBJECT
public:
    explicit Pop3Synchronizer(QObject *parent = nullptr);
    ~Pop3Synchronizer() override = default;

    // Synchronizer interface
    bool initialize(const Account &account) override;
    bool syncFolder(const Folder &folder) override;
    QVector<Folder> getFolders() const override;
    QVector<MailItem> fetchMailItems(const QString &folderId, qint64 sinceUid = 0) override;
    bool appendMailItem(const QString &folderId, const MailItem &item) override;
    bool updateMailItemFlags(const QString &folderId, const QString &itemUid, bool read, bool flagged) override;
    bool deleteMailItem(const QString &folderId, const QString &itemUid) override;

signals:
    void progressChanged(int percent);
    void statusMessage(const QString &message);

private:
    QString m_host;
    quint16 m_port{0};
    bool m_useSsl{false};
    QString m_username;
    QString m_password;

    QSslSocket *m_socket{nullptr};
    // Mapping from UIDL (as string) to message number for DELE
    QMap<QString, int> m_uidToMsgNum;
    // Highest UID seen (used for sinceUid)
    qint64 m_maxUid{0};

    QString readResponse();
    bool sendCommand(const QString &cmd, QString &response);
};