#ifndef MEASUREMENTDATAMANAGER_H
#define MEASUREMENTDATAMANAGER_H

#include <QObject>
#include <QHash>
#include <QUrl>

class QSettings;
class QNetworkAccessManager;
class QNetworkReply;
class WebSocketClient;
class MeasurementData;

class MeasurementDataManager : public QObject
{
    Q_OBJECT
    Q_DISABLE_COPY(MeasurementDataManager) //禁止拷贝，等价于：DataManager(const DataManager&) = delete; DataManager& operator=(const DataManager&) = delete;

public:
    static MeasurementDataManager* instance();

    void startSubscription(const QStringList& measurements);
    void stopSubscription();
    void addSubscriptionData(const QStringList& measurements);
    void removeSubscriptionData(const QStringList& measurements);
    void getSubscriptionRealtimeData();
    void queryHistoricalEvents(const QString& dataID, const QDateTime& startTime, const QDateTime& endTime, const QString& step, const QString& type = "phasor");

private slots:
    void onReceiveRealtimeData(const QString& dataMsg);
    void onHistoricalRequestFinished(const QString& dataID, QNetworkReply* reply);

private:
    struct RealtimeConfig
    {
        QString host;
        int port;
        QString httpPath;
        QString websocketPath;
        int timeout; //请求超时(毫秒)
        int maxRetries; //最大重试次数
        int retryInterval; //重试间隔

        RealtimeConfig()
            : timeout(3000)
            , maxRetries(3)
            , retryInterval(1000)
        {}
    };

    struct HistoricalConfig
    {
        QUrl baseUrl;
        int timeout; //请求超时(毫秒)
        int maxRetries; //最大重试次数
        int retryInterval; //重试间隔

        HistoricalConfig()
            : timeout(3000)
            , maxRetries(3)
            , retryInterval(1000)
        {}
    };

    struct ServiceConfig
    {
        RealtimeConfig realtimeCfg;
        HistoricalConfig historicalCfg;
    };

    explicit MeasurementDataManager();
    ~MeasurementDataManager();

    void initialize();
    void buildSubscriptionRequest(const QString& action, const QStringList& measurements);
    void processSubscriptionResponse(const QString& action, QNetworkReply* reply);
    QUrl buildHistoricalQueryUrl(const QString& dataID,const QDateTime& startTime, const QDateTime& endTime, const QString& step, const QString& type);
    void processHistoricalResponse(const QString& dataID, const QByteArray& data);

    //配置相关
    ServiceConfig m_serviceConfig;
    QSettings* m_settings;
    QString m_settingsFile;
    bool m_isVaildSettingsFile;

    //服务相关
    QString m_clientID;
    QNetworkAccessManager* m_networkManager;
    WebSocketClient* m_webSocketClient;

    //数据缓存
    QHash<QString, QSharedPointer<MeasurementData>> m_dataCache;
};

#endif
