/* -*- c++ -*-
   Copyright (C) 2003 <ryu@gpul.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


*/
/*
 *
 * Copyright (C) 2007 Loic Dachary <loic@dachary.org>
 * Copyright (C) 2004, 2006 Mekensleep
 *
 *  Mekensleep
 *  24 rue vieille du temple
 *  75004 Paris
 *       licensing@mekensleep.com
 *
 * 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Authors:
 *  Cedric PINSON <cpinson@freesheep.org>
 *  Loic Dachary <loic@gnu.org>
 *  Igor Kravtchenko <igor@tsarevitch.org>
 *
 */

#ifndef __OSGCAL__CORE_MODEL_H__
#define __OSGCAL__CORE_MODEL_H__

#include <vector>
#include <map>

#include <cal3d/cal3d.h>

#include <osg/Notify>
#include <osg/Texture2D>
#include <osg/Referenced>
#include <osgCal/Export>

#include <osgDB/ReaderWriter>

extern "C" {
  struct _xmlDoc;
  struct _xmlXPathContext;
}
typedef _xmlDoc* xmlDocPtr;
typedef _xmlXPathContext* xmlXPathContextPtr;

namespace osgCal {

  class Monitor {
  public:
    virtual ~Monitor() {}
    virtual void write(const char *mess) { };
    virtual void setProgressLength(int len) { };
    virtual void progress() { };
  };

  class IOOptions : public osgDB::ReaderWriter::Options {
  public:
    IOOptions() : mon_(NULL) { };
    Monitor *getMonitor() const { return mon_; }
    void setMonitor(Monitor *_mon) { mon_ = _mon; }
  protected:
    Monitor *mon_;
  };

  /**@brief Core Model class that creates a templated core object.
  * In order to create an animated model, a cal3d core model has to
  * be created. Given this core model it is possible to instanciate it
  * by creating a model which is in fact the real animated model and
  * object which can be inserted into an osg graph.
  */
  class OSGCAL_EXPORT CoreModel: public osg::Object {
   public:
    typedef std::vector<osg::ref_ptr<osg::Texture2D> > Textures2D;
    typedef std::map<int, Textures2D > CoreMaterialId2Textures2D;
    typedef std::map<std::string, std::string> Name2Filename;

    META_Object(osgCal, CoreModel);

    CoreModel();
    CoreModel(const CoreModel&, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  protected:
    virtual ~CoreModel();
  public:

    void setCollisionDefault(bool collisionDefault) { _collisionDefault = collisionDefault; }
    bool getCollisionDefault() { return _collisionDefault; }

    void setVersion(int version) { _version = version; }
    int getVersion() { return _version; }

    /**@brief Returns the cal3d core model associated with osgCal CoreModel*/
    CalCoreModel* getCalCoreModel(void) { return &_calCoreModel; }
    
    /**@brief Cleans and releases dead objects*/
    void garbageCollect(void);

    /**Returns a texture2D* given a texture filename
    * @param  strFilename name of the texture file ( typically an image )
    * @remark If texture has been already loaded by a previous call of this function,
    * the stored texture2D pointed is returned
    */
    Textures2D* getTextures2D(const std::string& strFilename);
    /**@brief Returns the texture associated to a material
    * @param  materialId the Id of the material
    */
    Textures2D* getTextures2D(int materialId);

    bool getTexturesNameFromMaterialID(int _coreMaterialId, std::vector<std::string> &_res);

    /**@brief Sets the mesh name to mesh filename map*/
    void setMeshName2Filename(const Name2Filename& meshName2Filename) { _meshName2Filename = meshName2Filename; }
    
    /**@brief Returns the mesh name to file mesh name map*/
    const Name2Filename& getMeshName2Filename(void) { return _meshName2Filename; }

    bool SubUsingMeshId(int coreMeshId);
    bool AddUsingMeshId(int coreMeshId);

    struct ParameterDescription {
    
      struct ColorElement {
        int value[3];
        int tn[3];
        int& operator[](int i) { return value[i];}
      };

      struct OpacityDescription {
        OpacityDescription():_modifiable(false),_opacity(100) {}
        bool _modifiable;
        int _opacity;
        std::string _text;
      };

      struct ColorDescription {
        ColorDescription():_modifiable(false),_selected(-1) {}
        bool _modifiable;
        int _selected;
        std::map<int,ColorElement> _colors;
        std::string _text;
      };
    
      ColorDescription _colors;
      OpacityDescription _opacity;
      std::string _type;
    };

    struct LayerDescription {
    
    enum TextureFormat {
    TEXTUREFORMAT_RGB32,
    TEXTUREFORMAT_RGB16,
    TEXTUREFORMAT_DXT1,
    TEXTUREFORMAT_DXT3,
    TEXTUREFORMAT_DXT5,
    };
      std::string _file;
      std::string _suffix;
      std::string _mode;
      int _textureFormat;
    };

    struct SlotDescription {
      struct LayerDescription {
        std::string _layer;
        std::string _parameter;
      };

      struct TextureDescription {
        std::string _mask;
        std::string _alpha;
        std::vector<LayerDescription> _layers;
      };

      typedef std::map<std::string, TextureDescription> TextureEntries;

      TextureEntries _textures;
      std::vector<std::string> _meshes;
      const std::vector<std::string>& getMeshes() const { return _meshes;}
    };

    typedef std::map<std::string,std::string> MeshDescription;

    struct Material {
      std::string targetmap;
      std::string transparency;
      std::string envmap;
    };

    typedef std::map<std::string,SlotDescription> SlotBank;

    std::map<std::string,ParameterDescription> _parameters;
    std::map<std::string,LayerDescription> _layers;
    std::map<std::string,SlotBank> _slots;
    std::map<std::string,MeshDescription> _meshes;
    std::vector<std::string> _commonMeshes;

    std::map<std::string, Material> _materials;

    std::map<std::string, CalVector*> _name2Normal;

    const std::vector<std::string>& getCommonMeshes() const { return _commonMeshes;}
    const std::map<std::string, MeshDescription>& getMeshes() const { return _meshes;}

    SlotDescription* getSlotFromTypeAndName(const std::string& slotType,const std::string& slotName) {
      std::map<std::string,SlotBank>::iterator stype=_slots.find(slotType);
      if (stype==_slots.end())
        return 0;
      SlotBank::iterator sname=stype->second.find(slotName);
      if (sname==stype->second.end())
        return 0;
      return &(sname->second);
    }

    std::string _path;
    bool _configurable;

  const std::map<std::string,SlotBank>& getSlots() const { return _slots;}
  
  const std::string& getFilename() const { return _filename; }
  void setFilename(const std::string &fname) { _filename = fname; }

  protected:
  
    /**@brief Cal3d core model*/
    CalCoreModel _calCoreModel;
    
    /**@brief Material Id to associated texture map*/
    CoreMaterialId2Textures2D _coreMaterialId2Textures;
    
    /**@brief Mesh name to mesh filename map */
    Name2Filename _meshName2Filename;
    std::map<int,int> _mReferenceCountCoreMeshIds;

    bool _collisionDefault;
    int _version;
    osg::NotifySeverity _notify;
    
    std::string _filename;

    friend class ReaderWriterXFG;
  };

}; // namespace osgCal

#endif
