// src/backend/serialize_visitor.cc - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This 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.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include "xml_serializer.hh"
#include "xml_serializer_factory.hh"
#include "xml_visitor.hh"

#include <sgpemv2/environment.hh>
#include <sgpemv2/history.hh>
#include <sgpemv2/process.hh>
#include <sgpemv2/serializer_error.hh>
#include <sgpemv2/string_utils.hh>

using namespace sgpem;

XMLSerializer::~XMLSerializer()
{}

XMLSerializer::XMLSerializer()
{}



void XMLSerializer::save_snapshot(const Glib::ustring& filename, const History& hist) throw(SerializerError)
{
  /* COMPAT: Do not genrate nodes for formatting spaces */
  LIBXML_TEST_VERSION
  xmlKeepBlanksDefault(0);

  xmlDocPtr doc;
  doc = xmlNewDoc((const xmlChar *)"1.0");
  if (doc != NULL)
  {
    fill_doc(doc, hist);
    int nwritten = xmlSaveFormatFile (filename.c_str(), doc, 1);
    if (nwritten < 0)
    {
      throw SerializerError("Error writing xml doc to output stream.");
    }
  }
  else
  {
    throw SerializerError("Internal Error creating xml doc.");
  }

  /* Clean up everything else before quitting. */
  xmlCleanupParser();
}

void XMLSerializer::restore_snapshot(const Glib::ustring& filename, History& hist) throw(SerializerError)
{
  // TODO - all to do!!
  // DEBUG - remove me when finished

  // disable notifications over history until the end of this method
  bool was_enabled = hist.set_notify_enabled(false);

#ifdef LIBXML_SAX1_ENABLED
  xmlDocPtr doc;

  LIBXML_TEST_VERSION
  xmlKeepBlanksDefault(0);

  /*
   * build an XML tree from a the file;
   */
  doc = xmlParseFile(filename.c_str());
  if (doc == NULL)
  {
    xmlCleanupParser();
    throw SerializerError("Parsing Error: doc is invalid.");
  }

  hist.clear();
  XMLSerializerFactory fact(hist);

  // read all elements and fill hist
  read_doc(doc, fact);

  // frees all xml related data
  xmlFreeDoc(doc);

  /* Clean up everything else before quitting. */
  xmlCleanupParser();

#else
  /*
  * the library has been compiled without some of the old interfaces
  */
#error Compilation of LIBXML with SAX1 support must be enabled
#endif /* LIBXML_SAX1_ENABLED */

  // Re-enable notifications over history observers
  hist.set_notify_enabled(was_enabled);
}






const Glib::ustring XMLSerializer::get_filename_extension()
{
  return Glib::ustring("xgp");
}


const Glib::ustring XMLSerializer::get_filename_description()
{
  return Glib::ustring("SGPEMv2 XML savefile");
}

void XMLSerializer::fill_doc(xmlDocPtr doc, const History& hist)
{
  xmlNodePtr root_node = NULL;/* node pointers */
  /*
   * Creates a new document, a node and set it as a root node
   */
  root_node = xmlNewNode(NULL, (const xmlChar *) "sgpem");
  xmlDocSetRootElement(doc, root_node);

  /*
   * Creates a DTD declaration. Isn't mandatory. 
   */
  xmlDtdPtr dtd = xmlCreateIntSubset(doc, (const xmlChar *) "sgpem", NULL, (const xmlChar *) "sgpem.dtd");

  XMLVisitor xvisit(root_node);
  xvisit.from_history(hist);
  /*
  // 
  // xmlNewChild() creates a new node, which is "attached" as child node
  // of root_node node.
  //
  xmlNodePtr resources_node = xmlNewChild(root_node, NULL, (const xmlChar *) "resources", NULL);
  // 
  // The same as above, but the new child node doesn't have a content
  //
  xmlNodePtr schedulables_node = xmlNewChild(root_node, NULL, (const xmlChar *) "schedulables", NULL);

  fill_resources(resources_node, hist);
  fill_schedulables(schedulables_node, hist);
  */
}

/*
void XMLSerializer::fill_resources(xmlNodePtr resources_node, const History& hist)
{


    const Environment& env = hist.get_last_environment();

    const Environment::Resources& rvect = env.get_resources();
    typedef Environment::Resources::const_iterator res_iterator;

    res_iterator iter = rvect.begin();
    res_iterator end = rvect.end();
    while(iter!=end)
    {
      XMLVisitor xvisit(resources_node);
      Glib::ustring key;
      int_to_string((int)(*iter).first, key);
      xvisit.from_resource(*((*iter).second), key);
      iter++;
    }
}

void XMLSerializer::fill_schedulables(xmlNodePtr schedulables_node, const History& hist)
{


    const Environment& env = hist.get_last_environment();

    const Environment::Processes& pvect = env.get_processes();
    typedef std::vector<Process*>::const_iterator proc_iterator;

    proc_iterator iter = pvect.begin();
    proc_iterator end = pvect.end();
    while(iter!=end)
    {
      XMLVisitor xvisit(schedulables_node);
      xvisit.from_process(*(*iter));
      iter++;
    }
}
*/

void XMLSerializer::read_doc(xmlDocPtr doc, XMLSerializerFactory& fact) throw(SerializerError)
{
  /*
   * Check the document is of the right kind
   */
  xmlNodePtr root;
  root = xmlDocGetRootElement(doc);
  if (root == NULL)
  {
    xmlFreeDoc(doc);
    xmlCleanupParser();
    throw SerializerError("Reading Error: xml doc is empty.");
  }

  xmlNodePtr cur;
  cur = root->children;
  while (cur != NULL)
  {
    Glib::ustring name((const char *)cur->name);
    if (name == "resources")
    {
      read_resources(cur, fact);
    }
    if (name == "schedulables")
    {
      read_schedulables(cur, fact);
    }
    cur = cur->next;
  }
}

XMLSerializerFactory::Parameters* read_properties(xmlAttrPtr prop)
{
  if (prop == NULL)
    return NULL;

  XMLSerializerFactory::Parameters* par = new XMLSerializerFactory::Parameters();
  while (prop != NULL)
  {
    if (prop->children && xmlNodeIsText(prop->children))
    {
      xmlChar *key = xmlNodeGetContent (prop->children);
      // xmlChar *key = xmlNodeListGetString(doc, prop->children, 1);
      if (key != NULL)
      {
        std::pair<Glib::ustring, Glib::ustring> key_value(Glib::ustring((const char *)prop->name), Glib::ustring((const char *)key));
        par->insert(key_value);
        xmlFree(key);
      }
    }
    prop = prop->next;
  }
  return par;
}

void XMLSerializer::read_resources(xmlNodePtr resources_node, XMLSerializerFactory& fact)
{
  xmlNodePtr cur;
  cur = resources_node->children;
  while (cur != NULL)
  {
    Glib::ustring node_name((const char *)cur->name);
    if (node_name == "resource")
    {
      xmlAttrPtr prop = cur->properties;
      XMLSerializerFactory::Parameters* par = read_properties(prop);
      if (par != NULL)
      {
        fact.factory_method(Glib::ustring("Resource"), *par);
      }
      // fact.create_resource(*par);
    }

    cur = cur->next;
  }
}
void XMLSerializer::read_schedulables(xmlNodePtr schedulables_node, XMLSerializerFactory& fact)
{
  if (schedulables_node == NULL)
    return;

  xmlNodePtr cur;
  cur = schedulables_node->children;
  while (cur != NULL)
  {
    Glib::ustring node_name((const char *)cur->name);
    if (node_name == "process")
    {
      xmlAttrPtr prop = cur->properties;
      XMLSerializerFactory::Parameters* par = read_properties(prop);
      if (par != NULL)
      {
        fact.factory_method(Glib::ustring("Process"), *par);
      }
      // pass the "threads node"
      read_threads(cur->children, fact);
    }

    cur = cur->next;
  }
}

void XMLSerializer::read_threads(xmlNodePtr threads_node, XMLSerializerFactory& fact)
{
  if (threads_node == NULL)
    return;

  xmlNodePtr cur;
  cur = threads_node->children;
  while (cur != NULL)
  {
    Glib::ustring node_name((const char *)cur->name);
    if (node_name == "thread")
    {
      xmlAttrPtr prop = cur->properties;
      XMLSerializerFactory::Parameters* par = read_properties(prop);
      if (par != NULL)
      {
        fact.factory_method(Glib::ustring("Thread"), *par);
      }
      // pass the "requests node"
      read_requests(cur->children, fact);
    }

    cur = cur->next;
  }
}

void XMLSerializer::read_requests(xmlNodePtr requests_node, XMLSerializerFactory& fact)
{
  if (requests_node == NULL)
  {
    return;
  }

  xmlNodePtr cur;
  cur = requests_node->children;
  while (cur != NULL)
  {
    Glib::ustring node_name((const char *)cur->name);
    if (node_name == "request")
    {
      xmlAttrPtr prop = cur->properties;
      XMLSerializerFactory::Parameters* par = read_properties(prop);
      if (par != NULL)
      {
        fact.factory_method(Glib::ustring("Request"), *par);
      }
      // pass the "subrequest nodes"
      read_subrequests(cur->children, fact);
    }

    cur = cur->next;
  }
}

void XMLSerializer::read_subrequests(xmlNodePtr subrequest_node, XMLSerializerFactory& fact)
{
  if (subrequest_node == NULL)
  {
    return;
  }

  xmlNodePtr cur;
  cur = subrequest_node;
  while (cur != NULL)
  {
    Glib::ustring node_name((const char *)cur->name);
    if (node_name == "subrequest")
    {
      xmlAttrPtr prop = cur->properties;
      XMLSerializerFactory::Parameters* par = read_properties(prop);
      if (par != NULL)
      {
        fact.factory_method(Glib::ustring("SubRequest"), *par);
      }
    }

    cur = cur->next;
  }
}

/* comment */
