/*
Copyright 2008  Nathan Bradshaw nathanlbradshaw@gmail.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/


#include "httpquery.h"

#include "query_p.h"

#include "httpservice.h"
#include <kio/jobclasses.h>
#include <kio/scheduler.h>
#include <kdebug.h>
#include <QtCore/QByteArray>
#include <QtCore/QStringList>

namespace ServiceModel {
   
class HttpQueryPrivate : public QueryPrivate
{
   public:
      HttpService* m_httpService;
      
      void addPathToUrl(QString directory) { this->m_urlExtensions->append(directory); }
      
      bool hasPathExtensions() { return !this->m_urlExtensions->isEmpty(); }
      
      const QStringList* getPathExtensions() { return this->m_urlExtensions; }
      
      HttpQueryPrivate() { this->m_urlExtensions = new QStringList(); }
      
      bool m_trailingSlash;
      
   private :
      QStringList* m_urlExtensions;
      
};

}



using namespace ServiceModel;

HttpQuery::HttpQuery (Service *service, QObject *parent) : Query(service, parent), d_ptr(new HttpQueryPrivate())
{
   Q_D(HttpQuery);
   
   d-> m_httpService = dynamic_cast<HttpService*>(service);
   if (!d-> m_httpService) {
      kDebug() << "Oops, we couldn't d-cast the service we were given";
      //\TODO we've died here because we can't turn the service we were handed into an httpservice. can just straight out emit query completed 
      //and indicate a failure status
   }
   
   //set the trailing slash defaul value
   d->m_trailingSlash = false;
}


HttpQuery::HttpQuery (HttpQueryPrivate &dd, Service *service, QObject *parent) : Query(dd, service, parent), d_ptr(&dd)
{
   
}

HttpQuery::~HttpQuery()
{
   
}

void HttpQuery::execute ()
{
   KIO::TransferJob* fetchJob = KIO::get(this->url());
   
   connect(fetchJob, SIGNAL(data(KIO::Job*, const QByteArray&)), SLOT(receiveData(KIO::Job*, QByteArray)));
}

void HttpQuery::setTrailingSlash(bool trailingSlash) 
{
   Q_D(HttpQuery);
   d->m_trailingSlash = trailingSlash;
}

KUrl HttpQuery::url()
{
   Q_D(HttpQuery);
   
   //get the base URL from the service
   KUrl queryString = d->m_httpService->baseUrl();
   
   //add any additional directories to the base path
   if (d->hasPathExtensions()) {
      const QStringList* additionalPaths = d->getPathExtensions();
      
      QStringList::const_iterator it;
      for (it = additionalPaths->constBegin(); it != additionalPaths->constEnd(); ++it) {
         queryString.addPath((*it).toLocal8Bit().constData());
      }
   }
   
   if (d->m_trailingSlash) {
      queryString.adjustPath(KUrl::AddTrailingSlash);
   }
   
   QHash<QString, QVariant> params = this->parameters();
   if (!params.isEmpty()) {
      QHash<QString, QVariant>::const_iterator i = params.constBegin();
      //loop through the params and add em to the query string, if they are of a type that makes sense to do so... strings, numbers etc
      while (i != params.constEnd()) {
         //if we can convert it to a string, we'll include the parameter...
         if (i.value().canConvert(QVariant::String)) {
            queryString.addQueryItem(i.key(), i.value().toString());
         }
         ++i;
      }
   }
   
   kDebug() << queryString;
   
   return queryString;
}

ServiceModel::Response* HttpQuery::response ()
{
    return 0;
}

void HttpQuery::addDirectoryToUrl(QString directory)
{
   Q_D(HttpQuery);
   
   d->addPathToUrl(directory);
}

void HttpQuery::receiveData(KIO::Job*, const QByteArray data)
{
   kDebug() << data;
}
