/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

#ifndef WEBSERVICECLIENT_H
#define WEBSERVICECLIENT_H
#include <QObject>
#include <QtNetwork>
#include "webservicereply.h"

class WebServiceClient : public QObject
{
    Q_OBJECT
public:
    WebServiceClient(QObject* parent = 0);

private:
    struct NetworkRequest
    {
        int type;
        QNetworkReply* reply;
        QNetworkAccessManager::Operation operationType;
        QString url;
        QByteArray baData;
        QList<QPair<QString, QString> > additionalHeaders;
    };
    typedef QSharedPointer<NetworkRequest> NetworkRequestPtr;
    struct RequestProperties
    {
        QString url;
        QNetworkAccessManager::Operation operationType;
    };
    QNetworkAccessManager* m_networkAccessManger;
    QHash<int, RequestProperties> m_requestProperties;
    QHash<QNetworkReply*, NetworkRequestPtr> m_requests;    
    QHash<QNetworkReply*, WebServiceReplyPtr> m_webServiceReplies;

    QNetworkReply *sendRequest(const NetworkRequestPtr request);

private slots:
    void onNetworkRequestFinished(QNetworkReply *reply);

protected slots:
    void onRequestFinished(WebServiceReplyPtr reply);
    void onRequestFailed(WebServiceReplyPtr reply, int error);
    void onRequestAborted(WebServiceReplyPtr reply);
    void onAbortRequested(WebServiceReplyPtr reply);

protected:    
    typedef QList<QPair<QString, QString> > StringPairList;
    virtual QNetworkReply* networkRequestFinished(QNetworkReply* networkReply,
                                                  WebServiceReplyPtr webServiceReply,
                                                  int networkRequestType);
    void addReply(QNetworkReply *networkReply, const WebServiceReplyPtr &reply);
    void addNetworkRequestType(int requestTypeId, const QString& url, QNetworkAccessManager::Operation operationType);
    virtual QNetworkReply *sendRequest(int requestTypeId, const QString &path = QString(),
                                       const StringPairList &parameters = StringPairList(),
                                       const StringPairList &additionalHeaders = StringPairList());
    QNetworkAccessManager* networkAccessManager() const;

signals:
    void requestFinished(WebServiceReplyPtr reply);
    void requestFailed(WebServiceReplyPtr reply);
    void requestAborted(WebServiceReplyPtr reply);
    void requestCompleted(WebServiceReplyPtr reply, WebServiceReply::CompletedState state);
};
#endif // WEBSERVICECLIENT_H
