// 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_visitor.hh"

#include "gettext.h"

#include <sgpemv2/environment.hh>
#include <sgpemv2/history.hh>
#include <sgpemv2/process.hh>
#include <sgpemv2/request.hh>
#include <sgpemv2/resource.hh>
#include <sgpemv2/serializer_error.hh>
#include <sgpemv2/string_utils.hh>
#include <sgpemv2/sub_request.hh>
#include <sgpemv2/thread.hh>

using namespace sgpem;


XMLVisitor::XMLVisitor(xmlNodePtr current)
    : _current(current)
{}

XMLVisitor::~XMLVisitor()
{}

void XMLVisitor::from_resource(const Resource& obj) throw(SerializerError)
{
  throw SerializerError(
    _("XMLVisitor: unsupported method from_resource(const Resource& obj)")
  );
}


void XMLVisitor::from_history(const History& obj) throw(SerializerError)
{
  from_history(_current, obj);
}


void XMLVisitor::from_environment(const Environment& obj) throw(SerializerError)
{
  from_environment(_current, obj);
}


void XMLVisitor::from_process(const Process& obj) throw(SerializerError)
{
  from_process(_current, obj);
}


void XMLVisitor::from_thread(const Thread& obj) throw(SerializerError)
{
  from_thread(_current, obj);
}


void XMLVisitor::from_request(const Request& obj) throw(SerializerError)
{
  from_request(_current, obj);
}


void XMLVisitor::from_subrequest(const SubRequest& obj) throw(SerializerError)
{
  from_subrequest(_current, obj);
}

void XMLVisitor::from_resource(const Resource& obj, const Glib::ustring& key) throw(SerializerError)
{
  from_resource(_current, obj, key);
}







void XMLVisitor::from_history(xmlNodePtr parent, const History& hist) throw(SerializerError)
{
  if (parent != NULL)
  {
    from_environment(parent, hist.get_last_environment());
  }
  else
  {
    throw SerializerError(_("Error trying to add data to empty XML node."));
  }
}


void XMLVisitor::from_environment(xmlNodePtr parent, const Environment& env) throw(SerializerError)
{
  if (parent == NULL)
  {
    throw SerializerError(_("Error trying to add data to empty XML node."));
  }
  //
  //Enclosing block - save resources
  //
  {
    xmlNodePtr resources_node = xmlNewChild(parent, NULL, (const xmlChar *) "resources", NULL);
    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;
      to_string<int>(static_cast<int>((*iter).first), key);
      //xvisit.from_resource(*((*iter).second), key);
      from_resource(resources_node, *((*iter).second), key);
      iter++;
    }
  }
  //
  //Enclosing block - save schedulables
  //
  {
    xmlNodePtr schedulables_node = xmlNewChild(parent, NULL, (const xmlChar *) "schedulables", NULL);
    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));
      from_process(schedulables_node, *(*iter));
      iter++;
    }
  }
}


void XMLVisitor::from_resource(xmlNodePtr parent, const Resource& obj, const Glib::ustring& key) throw(SerializerError)
{
  if (parent != NULL)
  {
    Glib::ustring id = "reskey" + key;
    Glib::ustring strPreemptible("false"); // fixed??
    Glib::ustring strArrivalTime("0");     // fixed??
    Glib::ustring strPlaces;
    to_string<int>(static_cast<int>(obj.get_places()), strPlaces);

    xmlNodePtr process_node = xmlNewChild(parent, NULL, (const xmlChar *) "resource", NULL);
    xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str());
    xmlNewProp(process_node, (const xmlChar *) "id", (const xmlChar *) id.c_str());
    xmlNewProp(process_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str());
    xmlNewProp(process_node, (const xmlChar *) "how-many", (const xmlChar *) strPlaces.c_str());
    xmlNewProp(process_node, (const xmlChar *) "pre-emptible", (const xmlChar *) strPreemptible.c_str());
  }
  else
  {
    throw SerializerError(_("Error trying to add resource to empty XML node."));
  }
}


void XMLVisitor::from_process(xmlNodePtr parent, const Process& obj) throw(SerializerError)
{
  if (parent != NULL)
  {
    Glib::ustring strPriority;
    Glib::ustring strArrivalTime;
    to_string<int>(obj.get_base_priority(), strPriority);
    to_string<int>(obj.get_arrival_time(), strArrivalTime);

    xmlNodePtr process_node = xmlNewChild(parent, NULL, (const xmlChar *) "process", NULL);
    xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str());
    xmlNewProp(process_node, (const xmlChar *) "priority", (const xmlChar *) strPriority.c_str());
    xmlNewProp(process_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str());

    // make a threads subnode
    xmlNodePtr threads_node = xmlNewChild(process_node, NULL, (const xmlChar *) "threads", NULL);

    // iterate on threads
    typedef std::vector<Thread*> Threads;
    typedef std::vector<Thread*>::const_iterator thr_iterator;
    const Threads& tvect = ((Process&)obj).get_threads();
    thr_iterator iter = tvect.begin();
    thr_iterator end  = tvect.end();
    while (iter != end)
    {
      const Thread* t = *iter;

      from_thread(threads_node, *(*iter));
      iter++;
    }
  }
  else
  {
    throw SerializerError(_("Error trying to add process to empty XML node."));
  }
}


void XMLVisitor::from_thread(xmlNodePtr parent, const Thread& obj) throw(SerializerError)
{
  if (parent != NULL)
  {

    Glib::ustring strPriority;
    Glib::ustring strArrivalTime;
    Glib::ustring strLastsTime;
    to_string<int>(obj.get_base_priority(), strPriority);
    to_string<int>(obj.get_arrival_time(), strArrivalTime);
    to_string<int>(obj.get_total_cpu_time(), strLastsTime);

    xmlNodePtr thread_node = xmlNewChild(parent, NULL, (const xmlChar *) "thread", NULL);
    xmlNewProp(thread_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str());
    xmlNewProp(thread_node, (const xmlChar *) "priority", (const xmlChar *) strPriority.c_str());
    xmlNewProp(thread_node, (const xmlChar *) "arrival-delta", (const xmlChar *) strArrivalTime.c_str());
    xmlNewProp(thread_node, (const xmlChar *) "lasts-for", (const xmlChar *) strLastsTime.c_str());

    // make a requests subnode
    xmlNodePtr requests_node = xmlNewChild(thread_node, NULL, (const xmlChar *) "requests", NULL);
    // iterate on requests
    typedef std::vector<Request*> Requests;
    typedef std::vector<Request*>::const_iterator req_iterator;
    const Requests& rvect = ((Thread&)obj).get_requests();
    req_iterator iter = rvect.begin();
    req_iterator end  = rvect.end();
    while (iter != end)
    {
      const Request* r = *iter;

      from_request(requests_node, *(*iter));
      iter++;
    }
  }
  else
  {
    throw SerializerError(_("Error trying to add thread to empty XML node."));
  }
}


void XMLVisitor::from_request(xmlNodePtr parent, const Request& obj) throw(SerializerError)
{
  if (parent != NULL)
  {

    Glib::ustring strArrivalTime;
    to_string<int>(obj.get_instant(), strArrivalTime);

    xmlNodePtr request_node = xmlNewChild(parent, NULL, (const xmlChar *) "request", NULL);
    xmlNewProp(request_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str());

    // make a requests subnode
    // xmlNodePtr subrequests_node = xmlNewChild(thread_node, NULL, (const xmlChar *) "requests", NULL);
    // iterate on subrequests
    typedef std::vector<SubRequest*> SubRequests;
    typedef std::vector<SubRequest*>::const_iterator subreq_iterator;
    const SubRequests& srvect = ((Request&)obj).get_subrequests();
    subreq_iterator iter = srvect.begin();
    subreq_iterator end  = srvect.end();
    while (iter != end)
    {
      const SubRequest* sr = *iter;
      from_subrequest(request_node, *(*iter));
      iter++;
    }
  }
  else
  {
    throw SerializerError(_("Error trying to add request to empty XML node."));
  }
}


void XMLVisitor::from_subrequest(xmlNodePtr parent, const SubRequest& obj) throw(SerializerError)
{
  if (parent != NULL)
  {

    Glib::ustring strResource;
    Glib::ustring strLastsFor;
    to_string<int>(obj.get_resource_key(), strResource);
    to_string<int>(obj.get_length(), strLastsFor);

    xmlNodePtr subrequest_node = xmlNewChild(parent, NULL, (const xmlChar *) "subrequest", NULL);
    xmlNewProp(subrequest_node, (const xmlChar *) "resource", (const xmlChar *) strResource.c_str());
    xmlNewProp(subrequest_node, (const xmlChar *) "lasts-for", (const xmlChar *) strLastsFor.c_str());
  }
  else
  {
    throw SerializerError(_("Error trying to add subrequest to empty XML node."));
  }
}
