#ifndef UPDATER_H
#define UPDATER_H

#include <QObject>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QTimer>

class DownloadManager;

class Updater : public QObject
{
    Q_OBJECT
public:
    explicit Updater(const QString& url,
                     const QString& version,
                     bool autoUpgrade,
                     int autoUpgradeInterval,
                     QObject *parent = nullptr);
    void createNetworkAccessManager();

    QString url() const;
    void setUrl(const QString &url);

    bool updateAvailable() const;
    void setUpdateAvailable(bool updateAvailable);

    QString downloadUrl() const;
    void setDownloadUrl(const QString &downloadUrl);

    QString latestVersion() const;
    void setLatestVersion(const QString &latestVersion);

    bool compareVersion(const QString& x, const QString& y);

    QString currentVersion() const;
    void setCurrentVersion(const QString &currentVersion);

Q_SIGNALS:
     void downloadFinished(const QString& filePath);

public Q_SLOTS:
    void checkForUpdate();
    void onReply (QNetworkReply* reply);

private:
    QTimer *m_updateTimer = Q_NULLPTR;
    bool m_autoUpgrade = false;
    int m_autoUpgradeInterval = 172800000;

    QString m_url;
    QString m_currentVersion;
    QString m_downloadUrl;
    QString m_latestVersion;
    bool m_updateAvailable = false;
    QNetworkAccessManager* m_manager = nullptr;
    DownloadManager* m_downloadManager = nullptr;
};

#endif // UPDATER_H
