#ifndef EBAY_H
#define EBAY_H

#include <kio/scheduler.h>
#include <kurl.h>
#include <kio/jobclasses.h>
#include <kmessagebox.h>

#include <QDomDocument>
#include <QDesktopServices>
#include <QCache>
#include <QDebug>

#include "auction.h"

class KConfig;

class eBay : public QObject
{
  Q_OBJECT

  public:
    eBay(QWidget* parent);

    void setSiteId(const int &siteId);
    QString authToken(void) const;
    void setAuthToken(const QString &authToken);

  public slots:
    void slotSendGetWatchList();
    //void slotSendGetOfficialTime();
    void slotSendGetSessionID();

    void slotSendRemoveFromWatchList(const QString &itemId);
    
    // revokes and resets token, writes config and emits new (empty) token
    void slotSendRevokeToken();


    
  private slots:
    void slotReceivedData(KIO::Job *job, const QByteArray &data);

    
    void slotSendFetchToken();

    void slotReceivedGetWatchList(KJob *job);
    //void slotReceivedGetOfficialTime(KJob *job);
    void slotReceivedGetSessionID(KJob *job);
    void slotReceivedFetchToken(KJob *job);
    void slotReceivedRevokeToken(KJob *job);
    void slotReceivedItemPicture(KJob *job);
    void slotReceivedRemoveFromWatchList(KJob *job);

  private:
    QList<Auction*> m_receivedAuctions;

    QUrl m_eBayEndpointUrl;
    QString m_authToken, m_ruName, m_devName, m_appName, m_certName, m_apiVersion, m_sessionId, m_siteId;
    QWidget *m_parent;
    QMap<KIO::TransferJob*, QByteArray*> m_jobsToBuffers;
    QMap<KIO::TransferJob*, Auction*> m_imageRetrievalJobsToAuctions;

    QCache<QString, QPixmap> pictureCache;

    void addRequestHeaders(KIO::TransferJob *job, const QString &callName);
    bool createDomDocument(QDomDocument &document, const QByteArray &data);
    QByteArray getJobData(KJob *job);

  signals:
    void receivedWatchList(const QList<Auction*> auctions);
//     void receivedOfficialTime(const QDateTime datetime);

    // if token is empty, thats confirmation that it has been successfully revoked.
    void authTokenChanged(const QString token);
    void logMessage(const QString message);

};

#endif