// imagedownloader.h
#ifndef IMAGEDOWNLOADER_H
#define IMAGEDOWNLOADER_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include <QDir>
#include <QQueue>
#include <QJsonObject>

struct ImageDownloadInfo
{
    QString uuid;
    QString filename;
    QString rawUrl;
    QString localPath;
};

class ImageDownloader : public QObject
{
        Q_OBJECT
        Q_PROPERTY(bool isDownloading READ isDownloading NOTIFY isDownloadingChanged)
        Q_PROPERTY(int totalImages READ totalImages NOTIFY totalImagesChanged)
        Q_PROPERTY(int downloadedImages READ downloadedImages NOTIFY downloadedImagesChanged)
        Q_PROPERTY(QString currentFileName READ currentFileName NOTIFY currentFileNameChanged)
        Q_PROPERTY(qint64 currentFileProgress READ currentFileProgress NOTIFY currentFileProgressChanged)
        Q_PROPERTY(qint64 currentFileSize READ currentFileSize NOTIFY currentFileSizeChanged)
        Q_PROPERTY(QString downloadDirectory READ downloadDirectory WRITE setDownloadDirectory NOTIFY downloadDirectoryChanged)

    public:
        explicit ImageDownloader(QObject *parent = nullptr);
        ~ImageDownloader();

        bool isDownloading() const
        {
            return m_isDownloading;
        }
        int totalImages() const
        {
            return m_totalImages;
        }
        int downloadedImages() const
        {
            return m_downloadedImages;
        }
        QString currentFileName() const
        {
            return m_currentFileName;
        }
        qint64 currentFileProgress() const
        {
            return m_currentFileProgress;
        }
        qint64 currentFileSize() const
        {
            return m_currentFileSize;
        }
        QString downloadDirectory() const
        {
            return m_downloadDirectory;
        }

    public slots:
        // Set download directory (writable from QML)
        void setDownloadDirectory(const QString &dir);

        // Start downloading images
        void startDownload(const QString &downloadDir, const QVariantList &selectedImages);

        // Cancel current download
        void cancelDownload();

    signals:
        void isDownloadingChanged();
        void totalImagesChanged();
        void downloadedImagesChanged();
        void currentFileNameChanged();
        void currentFileProgressChanged();
        void currentFileSizeChanged();
        void downloadDirectoryChanged();

        void downloadCompleted(bool success, int successCount, int failCount, const QString &message);
        void downloadProgress(int current, int total, const QString &filename);
        void fileDownloadFailed(const QString &filename, const QString &error);

    private slots:
        void onDownloadFinished();
        void onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
        void onDownloadReadyRead();

    private:
        void downloadNextImage();
        void setIsDownloading(bool downloading);
        void setTotalImages(int total);
        void setDownloadedImages(int downloaded);
        void setCurrentFileName(const QString &filename);
        void setCurrentFileProgress(qint64 progress);
        void setCurrentFileSize(qint64 size);

        QNetworkAccessManager *m_networkManager;
        QNetworkReply *m_currentReply;
        QFile *m_currentFile;

        QQueue<ImageDownloadInfo> m_downloadQueue;

        bool m_isDownloading;
        int m_totalImages;
        int m_downloadedImages;
        int m_failedDownloads;
        QString m_currentFileName;
        qint64 m_currentFileProgress;
        qint64 m_currentFileSize;
        QString m_downloadDirectory;

        bool m_cancelRequested;
};

#endif // IMAGEDOWNLOADER_H