/***************************************************************************
 *   Copyright (C) 2003 by Sylvain Joyeux                                  *
 *   sylvain.joyeux@m4x.org                                                *
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "parsers.h"
#include "../apt.h"

#include <tdelocale.h>
#include <kdebug.h>
#include <qhtmlstream.h>
#include <tqregexp.h>

static void	policy_begin(TQHtmlStream& stream)
{
  stream
    << block("div") << endl
    << block("table", "policy") << endl
    	<< block("tr") << block("td") << endl
    		<< block("table", "curver") << endl
    			<< block("tbody");
}

static void add_button(TQHtmlStream& stream, const TQString& mode, const TQString& text, const TQString& package)
{
  stream
    << block("form")
        << param("action") << "apt:/"
        << param("method") << "get" << endl
      << block("p") << endl
        << tag("input")
          << param("type") << "hidden"
          << param("name") << "get"
          << param("value") << mode << endl
        << tag("input")
          << param("type") << "hidden"
          << param("name") << "package"
          << param("value") << package << endl
        << tag("input")
          << param("type") << "submit"
          << param("value") << text << endl
      << close()
    << close() << endl;
}

static void add_version_link(TQHtmlStream& stream, AptProtocol* slave, const TQString& package, const TQString& version)
{
  KURL url(slave->buildURL("show", package));
  url.setHTMLRef(Parsers::mangle_version(version));

  stream
    << block("a", "vtable-version")
        << param("href") << url.htmlURL()
      << data() << version
    << close();
}

namespace Parsers
{

Policy::Policy(const TQString& package, bool act)
  : m_package(package), m_act(false)
{
  m_act = act;
}

void Policy::operator() (AptProtocol* slave, const TQString& type, const TQString& value)
{
  static bool first_version = false, received_sth = false;
  static TQString buffer;
  static TQHtmlStream* stream;

  static TQRegExp rx_notinstalled("(none)");

  kdDebug(7130) << "policy : " << type << " " << value << endl;

  if (type == "begin")
  {
    stream = new TQHtmlStream(&buffer);
  	policy_begin(*stream);
  }
  else if (type == "installed")
  {
  	received_sth = true;

    attribute_begin(*stream, i18n("Installed"));
    if (rx_notinstalled.search(value) >= 0)
    {
    	m_installed = TQString();
      *stream << i18n("no");
    }
    else
  	{
    	m_installed = value.stripWhiteSpace();
      add_version_link(*stream, slave, m_package, m_installed);
  	}
    attribute_end(*stream);
 }
  else if (type == "candidate")
  {
  	received_sth = true;

    bool has_candidate = (rx_notinstalled.search(value) == -1);

    if (m_act && has_candidate)
    {
      *stream
        << block("tr") << endl
          << block("td") << param("colspan") << 2 << endl;

      if (m_installed.isEmpty())
        add_button(*stream, "install", i18n("Install"), m_package);
      else
        add_button(*stream, "remove", i18n("Remove"), m_package);
      *stream << close() << close() << endl;
    }

    attribute_begin(*stream, i18n("Candidate"));
    if (has_candidate)
      add_version_link(*stream, slave, m_package, value);
    else
      *stream << i18n("none");
    attribute_end(*stream);

    if (m_act && has_candidate
      && !m_installed.isEmpty() && m_installed != value)
    {
      *stream
        << block("tr") << endl
          << block("td") << param("colspan") << 2 << endl;
      add_button(*stream, "install", i18n("Upgrade"), m_package);
      *stream << close() << close() << endl;
    }
  }
  else if (type == "version_table")
  {
  	received_sth = true;

    *stream
    	<< close() << close() << endl // tbody, table, form
      << close() << endl; // td

  	first_version = true;
  }
  else if (type == "version")
  {
    TQString version = value.section(' ', 0, 0);
    TQString pin = value.section(' ', 1, 1);

    if (first_version)
    {
      *stream
        << block("td") << endl
          << block("div", "vtable") << endl
            << block("div", "header") << i18n("Version Table") << close() << endl
            << block("div", "versions") << endl;
    } /*else {
      *stream << close() << close();
    }*/

    TQString version_link;
    version_link = "<a href=\"apt:/show?" + m_package + "#" + mangle_version(version) + "\">"
       + version + "</a>";

    *stream << tag("br") << endl;
    add_version_link(*stream, slave, m_package, version);
    *stream << "[Pin " << block("span", "version-pin") << pin << close() << "]";

    first_version = false;
  }
  else if (type == "location")
  {
    TQStringList sections = TQStringList::split(' ', value);
    TQString pin = sections.first();
    sections.pop_front();
    // remove the "Packages" field if it is here
    if (sections.last() == "Packages")
      sections.pop_back();

    *stream << tag("br") << endl;
    *stream << block("span", "location-pin") << pin << close() << sections.join(" ") << endl;
  }
  else if (type == "end")
  {
  	if (received_sth)
    {
      *stream << close_all() << endl;
      *slave << buffer;
   	}

    buffer = TQString();
    received_sth = false;
    delete stream;
  }
}

}
