/***************************************************************************
 *   Copyright (C) 2004 - 2005 by Raphael Langerhorst                      *
 *   raphael-langerhorst@gmx.at                                            *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

#ifndef GCOREXMLSERIALIZER_H
#define GCOREXMLSERIALIZER_H

#include <GElement.h>
#include <GElementID.h>
#include <GEnergy.h>
#include <GForm.h>
#include <GAgent.h>
#include <GObject.h>
#include <GMatrix44.h>
#include <GElementInfluence.h>

#include <qobject.h>
#include <qstring.h>
#include <qdom.h>
#include <qvaluelist.h>

namespace GCS
{
  class GWorldData;
}


namespace GWE
{

/**
 * \class GCoreXmlSerializer GCoreXmlSerializer.h
 * \brief Implements XML serialization of GCS data.
 * @author Raphael Langerhorst
 * 
 * @todo define the XML Schema for each part(!) and implement serialization.
 * 
 * This class is the bridge between the C++ object representation
 * of G Core System classes and XML data representation. It
 * can convert C++ objects of the G Core System into XML and
 * convert XML data into GCS C++ objects. The XML data structure
 * is defined by the XML Schema for the GCS.
 * 
 * The C++ objects are required to be able to execute elements,
 * these are then considered to be active. They constantly change
 * through their activities of the agents. Changes are also
 * reported to the GWE which in turn uses this class to convert
 * the changed parts of the C++ objects into XML.
 * 
 * For data transportation (especially through network) and
 * persistent storage, the XML representation of elements is
 * used. This makes network and storage implementations independent
 * of the C++ classes. XML also provides a standard for data
 * representation, which makes it particularly useful as
 * interchange format (as defined by XML Schemas) between
 * GWE Server instances.
 * 
 * The Serializer is able to work in pieces of data (QDomElement).
 * These pieces can be compiled together to a complete XML document.
 */
class GCoreXmlSerializer : public QObject
{
  Q_OBJECT
  
  protected:
  
    /**
     * The GCS::GWorldData object is used to create GCS::GObject objects.
     * The GObject class needs a pointer to the world data in the constructor.
     */
    
    const GCS::GWorldData* WorldData;
       
  public:
  
    /**
     * Constructor.
     */
    GCoreXmlSerializer(const GCS::GWorldData* world_data, QObject *parent = 0, const char *name = 0);
    
    /**
     * Virtual Destructor.
     */
    virtual ~GCoreXmlSerializer();
    
    /**
     * Creates an agent from XML.
     */
    GCS::GAgent* createAgent(QDomElement data);
    
    /**
     * Creates an agent list from XML.
     */
    QPtrList<GCS::GAgent>* createAgents(QDomElement data);
    
    /**
     * Creates element data from XML.
     */
    QDomDocument* createElementData(QDomElement data);
    
    /**
     * Creates an element from XML.
     * @note this method uses the other methods to
     *       compound a complete element.
     */
    GCS::GElement* createElement(QDomElement data);
    
    /**
     * Creates an element ID from XML.
     */
    GCS::GElementID createElementID(QDomElement data, bool* ok = NULL);
    
    /**
     * Creates energy from XML.
     */
    GCS::GEnergy* createEnergy(QDomElement data);
    
    /**
     * Create a GVector3 from XML.
     */
    GCS::GVector3 createVector3(QDomElement data, bool* ok = NULL);
    
    /**
     * Create a GObject from XML.
     */
    GCS::GObject* createObject(QDomElement data);
    
    /**
     * Create a form from XML.
     */
    GCS::GForm* createForm(QDomElement data);
    
    GCS::GMatrix44 createMatrix44(QDomElement data, bool* ok = NULL);
    
    GCS::GElementInfluence createElementInfluence(QDomElement data, bool* ok = NULL);
    
    GCS::GElementID getInfluenceTarget(QDomElement data, bool* ok = NULL);

    /**
     * Serializes an agent into XML.
     */
    QDomElement serializeAgent(const GCS::GAgent* agent, QString TagName, QDomDocument* document = new QDomDocument());
               
    /**
     * Serializes element data into XML.
     */
    QDomElement serializeElementData(const QDomDocument* data, QString TagName, QDomDocument* document = new QDomDocument());
    
    /**
     * Serializes an element into XML.
     * @note this method uses the other methods to
     *       compound a complete element.
     */
    QDomElement serializeElement(const GCS::GElement* element, QString TagName, QDomDocument* document = new QDomDocument());
    
    /**
     * Serializes an element identification into XML.
     */
    QDomElement serializeElementID(const GCS::GElementID& element_id, QString TagName, QDomDocument* document = new QDomDocument());
    
    /**
     * Serializes energy into XML.
     */
    QDomElement serializeEnergy(const GCS::GEnergy* energy, QString TagName, QDomDocument* document = new QDomDocument());
    
    /**
     * Serializes a form into XML.
     */
    QDomElement serializeForm(const GCS::GForm* form, QString TagName, QDomDocument* document = new QDomDocument());
    
    /**
     * Serializes a Vector3 into XML.
     */
    QDomElement serializeVector3(const GCS::GVector3* vector3, QString TagName, QDomDocument* document = new QDomDocument());
    
    /**
     * Serializes an object into XML.
     */
     QDomElement serializeObject(const GCS::GObject* object, QString TagName, QDomDocument* document = new QDomDocument());

    QDomElement serializeAgents(QValueList<const GCS::GAgent*> Agents, QString TagName, QDomDocument* document = new QDomDocument());
    
    QDomElement serializeMatrix44(const GCS::GMatrix44& matrix, QString TagName, QDomDocument* document = new QDomDocument());
    
    QDomElement serializeElementInfluence(const GCS::GElementInfluence& influence, const GCS::GElementID& target_element, QString TagName, QDomDocument* document = new QDomDocument());
};

}

#endif //GCOREXMLSERIALIZER_H
