/*   EXTRAITS DE LA LICENCE
	Copyright CEA, contributeurs : Luc BILLARD et Damien
	CALISTE, laboratoire L_Sim, (2001-2006)
  
	Adresse mèl :
	BILLARD, non joignable par mèl ;
	CALISTE, damien P caliste AT cea P fr.

	Ce logiciel est un programme informatique servant à visualiser des
	structures atomiques dans un rendu pseudo-3D. 

	Ce logiciel est régi par la licence CeCILL soumise au droit français et
	respectant les principes de diffusion des logiciels libres. Vous pouvez
	utiliser, modifier et/ou redistribuer ce programme sous les conditions
	de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA 
	sur le site "http://www.cecill.info".

	Le fait que vous puissiez accéder à cet en-tête signifie que vous avez 
	pris connaissance de la licence CeCILL, et que vous en avez accepté les
	termes (cf. le fichier Documentation/licence.fr.txt fourni avec ce logiciel).
*/

/*   LICENCE SUM UP
	Copyright CEA, contributors : Luc BILLARD et Damien
	CALISTE, laboratoire L_Sim, (2001-2006)

	E-mail address:
	BILLARD, not reachable any more ;
	CALISTE, damien P caliste AT cea P fr.

	This software is a computer program whose purpose is to visualize atomic
	configurations in 3D.

	This software is governed by the CeCILL  license under French law and
	abiding by the rules of distribution of free software.  You can  use, 
	modify and/ or redistribute the software under the terms of the CeCILL
	license as circulated by CEA, CNRS and INRIA at the following URL
	"http://www.cecill.info". 

	The fact that you are presently reading this means that you have had
	knowledge of the CeCILL license and that you accept its terms. You can
	find a copy of this licence shipped with this software at Documentation/licence.en.txt.
*/
#include "dumpToAscii.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <visu_tools.h>

int writeDataInAsciiFormat(FileFormat *format, GString *buffer,
			   char* filename, int width, int height,
			   VisuData *dataObj,
			   voidDataFunc functionWait, gpointer data);
static gpointer waitData;
static voidDataFunc waitFunc;

DumpType* initDumpToAscii()
{
  DumpType *ascii;
  char *typeASCII[] = {"*.ascii", (char*)0};
#define descrASCII _("Ascii file (current positions)")
  FileFormat* fmt;

  ascii = g_malloc(sizeof(DumpType));
  fmt = fileFormatNew(descrASCII, typeASCII);
  if (!fmt)
    g_error("Can't initialize the ASCII dump module, aborting.\n");

  ascii->fileType = fmt;
  ascii->writeFunc = writeDataInAsciiFormat;
  
  fileFormatAdd_propertyBoolean(fmt, "comment_hidden_nodes",
				_("Comment hidden nodes"), TRUE);

  waitData = (gpointer)0;
  waitFunc = (voidDataFunc)0;

  return ascii;
}

int writeDataInAsciiFormat(FileFormat *format, GString *buffer,
			   char* filename, int width, int height,
			   VisuData *dataObj,
			   voidDataFunc functionWait, gpointer data)
{
  FILE *file;
  gchar *nom, *prevFile;
  OpenGLView *view;
  int i, j;
  char tmpChr;
  GList *prop;
  gchar* name;
  gboolean comment;

  comment = TRUE;
  prop = fileFormatGet_propertiesList(format);
  while (prop)
    {
      name = fileFormatGet_propertyName((FileFormatProperty*)(prop->data));
      if (strcmp(name, "comment_hidden_nodes") == 0)
	comment = fileFormatGet_propertyBoolean((FileFormatProperty*)(prop->data));
      prop = g_list_next(prop);
    }

  waitData = data;
  waitFunc = functionWait;

  DBG_fprintf(stderr, "Dump ASCII : begin export of current positions...\n");

  file = fopen(filename, "w");
  if (!file)
    {
      g_string_append(buffer, _("Cannot open file (to write in)\n"));
      return 1;
    }

  nom = visuDataGet_file(dataObj, 0);
  if (nom)
    {
      prevFile = g_path_get_basename(nom);
      fprintf(file, "# V_Sim export to ascii from '%s'\n", prevFile);
      g_free(prevFile);
    }
  else
    {
      g_warning("Can't get the name of the file to export.");
      fprintf(file, "# V_Sim export to ascii\n");
    }

  view = visuDataGet_openGLView(dataObj);
  fprintf(file, "%15g %15g %15g\n", view->box->p2[0], view->box->p4[0], view->box->p4[1]);
  fprintf(file, "%15g %15g %15g\n", view->box->p5[0], view->box->p5[1], view->box->p5[2]);

  if (comment)
    {
      fprintf(file, "# Statistics are valid for all nodes (hidden or not).\n");
      fprintf(file, "# Hidden nodes are printed, but commented.\n");
    }
  fprintf(file, "# Box contains %d nodes.\n", dataObj->nbOfAllStoredNodes);
  fprintf(file, "# Box contains %d elements.\n", dataObj->ntype);
  for (i = 0; i < dataObj->ntype; i++)
    fprintf(file, "#  | %d nodes for element '%s'.\n", dataObj->numberOfStoredNodes[i],
	    dataObj->fromIntToVisuElement[i]->name);

  for (i = 0; i < dataObj->ntype; i++)
    for (j = 0; j < dataObj->numberOfStoredNodes[i]; j++)
      {
	if ((dataObj->fromIntToVisuElement[i]->rendered &&
	     dataObj->nodes[i][j].rendered) || !comment)
	  tmpChr = ' ';
	else
	  tmpChr = '#';
	fprintf(file, "%c%15g %15g %15g %s\n", tmpChr,
		dataObj->nodes[i][j].x, dataObj->nodes[i][j].y,
		dataObj->nodes[i][j].z, dataObj->fromIntToVisuElement[i]->name);
      }
      
  fclose(file);

  return 0;
}
