// 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 3 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, see http://www.gnu.org/licenses/.


#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)
{
  /* COMPAT: Do not genrate nodes for formatting spaces */
  LIBXML_TEST_VERSION
  xmlKeepBlanksDefault(0);

  xmlDocPtr doc;
  doc = xmlNewDoc((const xmlChar *)"1.0");
  if (doc != nullptr)
  {
    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)
{
  // TODO - all to do!!
  // DEBUG - remove me when finished

  // disable notifications over history until the end of this method
  History::LockNotify h_lock(hist);

#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 == nullptr)
  {
    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 */

}






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 = nullptr;/* 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", nullptr, (const xmlChar *) "sgpem.dtd");
  
  //TODO: check for DTD compliance??

  XMLVisitor xvisit(root_node);
  xvisit.from_history(hist);
  
}

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

  xmlNodePtr cur;
  cur = root->children;
  while (cur != nullptr)
  {
    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 == nullptr)
    return nullptr;

  XMLSerializerFactory::Parameters* par = new XMLSerializerFactory::Parameters();
  while (prop != nullptr)
  {
    if (prop->children && xmlNodeIsText(prop->children))
    {
      xmlChar *key = xmlNodeGetContent (prop->children);
      // xmlChar *key = xmlNodeListGetString(doc, prop->children, 1);
      if (key != nullptr)
      {
        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 != nullptr)
  {
    Glib::ustring node_name((const char *)cur->name);
    if (node_name == "resource")
    {
      xmlAttrPtr prop = cur->properties;
      XMLSerializerFactory::Parameters* par = read_properties(prop);
      if (par != nullptr)
      {
        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 == nullptr)
    return;

  xmlNodePtr cur;
  cur = schedulables_node->children;
  while (cur != nullptr)
  {
    Glib::ustring node_name((const char *)cur->name);
    if (node_name == "process")
    {
      xmlAttrPtr prop = cur->properties;
      XMLSerializerFactory::Parameters* par = read_properties(prop);
      if (par != nullptr)
      {
        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 == nullptr)
    return;

  xmlNodePtr cur;
  cur = threads_node->children;
  while (cur != nullptr)
  {
    Glib::ustring node_name((const char *)cur->name);
    if (node_name == "thread")
    {
      xmlAttrPtr prop = cur->properties;
      XMLSerializerFactory::Parameters* par = read_properties(prop);
      if (par != nullptr)
      {
        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 == nullptr)
  {
    return;
  }

  xmlNodePtr cur;
  cur = requests_node->children;
  while (cur != nullptr)
  {
    Glib::ustring node_name((const char *)cur->name);
    if (node_name == "request")
    {
      xmlAttrPtr prop = cur->properties;
      XMLSerializerFactory::Parameters* par = read_properties(prop);
      if (par != nullptr)
      {
        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 == nullptr)
  {
    return;
  }

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

    cur = cur->next;
  }
}

/* comment */
