// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file FileWriter.h */

/** \defgroup Output Output module
 * @{ <img src="output.png"> @}
 */

#ifndef AMDIS_FILEWRITER_H
#define AMDIS_FILEWRITER_H

#include <vector>
#include <string>
#include "AMDiS_fwd.h"
#include "Mesh.h"
#include "DataCollector.h"

namespace AMDiS {

  typedef enum {
    NONE = 0,
    GZIP = 1,
    BZIP2 = 2
  } FileCompression;


  class FileWriterInterface
  {
  public:
    FileWriterInterface()
      : filename(""),
	traverseLevel(-1),
	traverseFlag(Mesh::CALL_LEAF_EL),
	writeElement(NULL)
    {}

    virtual ~FileWriterInterface() {}

    /** \brief
     * Interface. Must be overridden in subclasses.
     * \param time time index of solution vector.
     * \param force enforces the output operation for the last timestep.
     */
    virtual void writeFiles(AdaptInfo *adaptInfo, bool force,
			    int level = -1,
			    Flag traverseFlag = Mesh::CALL_LEAF_EL,
			    bool (*writeElem)(ElInfo*) = NULL) = 0;

    void setTraverseProperties(int level, 
			       Flag flag,
			       bool (*writeElem)(ElInfo*))
    {
      traverseLevel = level;
      traverseFlag |= flag;
      writeElement = writeElem;
    }

    std::string getFilename() 
    { 
      return filename; 
    }
    
    void setFilename(std::string n) 
    { 
      filename = n; 
    }

  protected:
    /// Used filename prefix.
    std::string filename;

    int traverseLevel;

    Flag traverseFlag;

    bool (*writeElement)(ElInfo*);  
  };

  /**  
   * \ingroup Output
   *
   * \brief
   * Base class of FileWriterScal and FileWriterVec. Manages the file output
   * of solution vectors.
   */
  class FileWriter : public FileWriterInterface
  {
  public:
    /// Constructor for a filewriter for one data component.
    FileWriter(std::string name, Mesh *mesh, DOFVector<double> *vec);

    /// Constructor for a filewriter with more than one data component.
    FileWriter(std::string name, 
	       Mesh *mesh,
	       std::vector< DOFVector<double>* > vecs);

    /** \brief
     * Constructor for a filewriter, when the solution vector is a vector
     * of WorldVectors.
     */
    FileWriter(std::string name,
	       Mesh *mesh,
	       DOFVector< WorldVector<double> > *vec);

    /// Destructor
    virtual ~FileWriter();

    /// Implementation of FileWriterInterface::writeFiles().
    virtual void writeFiles(AdaptInfo *adaptInfo, bool force,
			    int level = -1,
			    Flag traverseFlag = Mesh::CALL_LEAF_EL,
			    bool (*writeElem)(ElInfo*) = NULL);

  protected:
    /// Initialization of the filewriter.
    void initialize();

    /// Reads all file writer dependend parameters from the init file.
    void readParameters();

    /// Name of the writer.
    std::string name;

    /// TecPlot file extension.
    std::string tecplotExt;

    /// AMDiS mesh-file extension.
    std::string amdisMeshExt;

    /// AMDiS solution-file extension.
    std::string amdisDataExt;

    /// VTK file extension.
    std::string paraviewFileExt;

    /// Parallel VTK file extension.
    std::string paraviewParallelFileExt;

    /// Periodic file extension.
    std::string periodicFileExt;

    /// 0: Don't write TecPlot files; 1: Write TecPlot files. 
    int writeTecPlotFormat;

    /// 0: Don't write AMDiS files; 1: Write AMDiS files. 
    int writeAMDiSFormat;

    /// 0: Don't write ParaView files; 1: Write ParaView files.
    int writeParaViewFormat;

    /// 0: Don't write ParaView animation file; 1: Write ParaView animation file.
    int writeParaViewAnimation;

    /// 0: Don't write periodic files; 1: Write periodic files.
    int writePeriodicFormat;

    /// 0: Don't write png files; 1: Write png image files.
    int writePngFormat;

    /// 0: Gray color picture; 1: RGB picture.
    int pngType;

    /** \brief
     * 0: Don't append time index to filename prefix.
     * 1: Append time index to filename prefix.
     */
    int appendIndex;

    /// Total length of appended time index.
    int indexLength;

    /// Number of decimals in time index.
    int indexDecimals;

    /// Timestep modulo: write only every tsModulo-th timestep! 
    int tsModulo;

    /// Stores all writen filename to a ParaView animation file.
    std::vector< std::string > paraviewAnimationFrames;

    ///
    int timestepNumber;

    /// Mesh used for output.
    Mesh *mesh;

    /// fespace used for output.
    const FiniteElemSpace *feSpace;

    /// Pointers to the vectors which store the solution.
    std::vector< DOFVector<double>* > solutionVecs;

    /** \brief
     * Stores the number of temporal solutions vectors, which have been created
     * in the constructor. If this number is greater than zero, the vectors
     * stored in solutionVecs_ must be deleted in the destructor.
     */
    int nTmpSolutions;

    /** \brief
     * Defines if, and with what kind of compression, the file should be compressed
     * during writing.
     */
    FileCompression compression;
  };

}

#endif // AMDIS_FILEWRITER_H