#ifndef PQNETWORKREPLY_H
#define PQNETWORKREPLY_H

#include <QNetworkReply>
#include <pqobject.h>

class PQNetworkReply : public PQObject
{
    Q_OBJECT
    PQ_OBJECT_EX(QNetworkReply)

public:
    PQNetworkReply(QNetworkReply *reply);
    ~PQNetworkReply();
    QNetworkReply *data_ptr();

    Q_INVOKABLE void close();
    Q_INVOKABLE bool isSequential() const;

    Q_INVOKABLE qint64 readBufferSize() const;
    Q_INVOKABLE void setReadBufferSize(qint64 size);

    //QNetworkAccessManager *manager() const;
    //QNetworkAccessManager::Operation operation() const;
    //QNetworkRequest request() const;
    //NetworkError error() const;
    Q_INVOKABLE bool isFinished() const;
    Q_INVOKABLE bool isRunning() const;
    Q_INVOKABLE QString url() const;

    Q_INVOKABLE QVariant header(int header) const;

    Q_INVOKABLE bool hasRawHeader(const QByteArray &headerName) const;
    Q_INVOKABLE QList<QByteArray> rawHeaderList() const;
    Q_INVOKABLE QByteArray rawHeader(const QByteArray &headerName) const;

    //typedef QPair<QByteArray, QByteArray> RawHeaderPair;
    //const QList<RawHeaderPair>& rawHeaderPairs() const;

    Q_INVOKABLE QVariant attribute(int code) const;
    Q_INVOKABLE QByteArray readAll() const;

public slots:
    void metaDataChanged_pslot();
    void finished_pslot();
    void readyRead_pslot();
    void uploadProgress_pslot(double bytesSent, double bytesTotal);
    void downloadProgress_pslot(double bytesReceived, double bytesTotal);
    void error_pslot(int error);
    void redirected_pslot(const QString &url);

    void metaDataChanged_simplify();
    void finished_simplify();
    void readyRead_simplify();
    void uploadProgress_simplify(qint64 bytesSent, qint64 bytesTotal);
    void downloadProgress_simplify(qint64 bytesReceived, qint64 bytesTotal);
    void error_simplify(QNetworkReply::NetworkError error);
    void redirected_simplify(const QUrl &url);

    PQ_ON_SIGNAL_DECL_START {
        PQOBJECT_ON_SIGNALS();
        PQ_ON_SIGNAL(metaDataChanged_pslot());
        PQ_ON_SIGNAL(finished_pslot());
        PQ_ON_SIGNAL(readyRead_pslot());
        PQ_ON_SIGNAL(uploadProgress_pslot(double,double));
        PQ_ON_SIGNAL(downloadProgress_pslot(double,double));
        PQ_ON_SIGNAL(error_pslot(int));
        PQ_ON_SIGNAL(redirected_pslot(string));
    } PQ_ON_SIGNAL_DECL_END

signals:
    void metaDataChanged();
    void finished();
    void readyRead();
    void uploadProgress(double bytesSent, double bytesTotal);
    void downloadProgress(double bytesReceived, double bytesTotal);
    void error(int e);
    void redirected(QString url);

private:
    QNetworkReply *m_reply;
};

Q_DECLARE_METATYPE(QNetworkReply*)
Q_DECLARE_METATYPE(PQNetworkReply*)

#endif // PQNETWORKREPLY_H
