/*=========================================================================
*
* PlgLoad.h	- Loading functions for Plg file
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include "PlgLoad.h"

#include <time.h>

#include "conventi.h"
#include "print.h"
#include "system.h"


//--- Global statics --------------------------------------------------------

MLT_tdstVertexColor	**gs_hVertexColorInFile	= NULL;
long				  gs_lVertexColorIndex	= 0;

BOOL g_bExistPLG = TRUE;



//--------------------------------------------------------------------

/****************************************************************************
*		Description:	register the loading function for the PLG file
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vInitPLGLoad()
{
  SCR_fn_v_RdL0_RegisterCallback(M_VertexColor,MLT_xLoadVertexColor, SCR_CRC_c_RdL0_ForSection);
  SCR_fn_v_RdL0_RegisterCallback(M_FichierPLG,MLT_xLoadPLGCallback, SCR_CRC_c_RdL0_ForFile);
}


/****************************************************************************
*		Description:	save color in PLG file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveVertexColor(SCR_tdst_File_Description *p_stFile, MLT_tdstVertexColor *p_stVertexColor)
{
  xString mes, sFile, sAction, sIdent;
  int i;

  SCR_fn_v_RdL0_SplitSectionName(p_stVertexColor->sName, sFile, sAction, sIdent);
  sprintf(sFile, "%s%s.plg", g_sDirectorySave, g_sFileIn);
  SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_VertexColor, sIdent);
  SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(mes), SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 2, "%d", p_stVertexColor->xNbPoints);

  for (i=0; i<p_stVertexColor->xNbPoints; i++)
  { 
	SCR_M_SvL0_SaveEntry(p_stFile, M_PLGActionAddVertexColor, SCR_CC_C_Cfg_NoChar);
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 5, "%d,%f,%f,%f",i,p_stVertexColor->d_stListOfPoints[i].fR,
																					p_stVertexColor->d_stListOfPoints[i].fG, 
																					p_stVertexColor->d_stListOfPoints[i].fB);
  }

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
}

/****************************************************************************
*		Description:	load color section in PLG file
*
*		Parameters:		p_stFile	: script file pointer
*						szAction	: section or entry name
*						szParams	: parameters
*						cType		: action type
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
SCR_tde_Anl_ReturnValue MLT_xLoadVertexColor(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstVertexColor	*p_stVertexColor;
  xString				sFile, sAction, sIdent;
  char					*p_Char1;
  static long			lNbPoints;
  long					xIndex;

  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_stVertexColor=(MLT_tdstVertexColor*)malloc(sizeof(MLT_tdstVertexColor));
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_stVertexColor);
      SCR_M_RdL0_SetContextLong(0,0,(long)p_stVertexColor);
      p_stVertexColor->xNbPoints=atoi(szParams[0]);
      p_stVertexColor->d_stListOfPoints=(MLT_tdstVColor*)malloc(p_stVertexColor->xNbPoints*sizeof(MLT_tdstVColor));
      sprintf(p_stVertexColor->sName,"%s",SCR_M_RdL0_GetCompleteSectionNameR(0));
      SCR_fn_v_RdL0_SplitSectionName(p_stVertexColor->sName, sFile, sAction, sIdent);
	  p_Char1=sIdent;
	  MLT_vRemoveFlags(&p_Char1);
	  strcpy(sIdent, p_Char1);
	  p_Char1=sIdent;
	  MLT_vRemoveGeomProp(&p_Char1);
	  strcpy(sIdent, p_Char1);
      SCR_fn_v_RdL0_ComputeSectionName(p_stVertexColor->sName, sFile, sAction, sIdent);
      lNbPoints = 0;
      break;
                
    case SCR_EA_Anl_Entry:
      // Recuperation du LoadGeometric a remplir
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstVertexColor*,p_stVertexColor);

      // Loading of object's VertexColor
      if (strcmp(szAction,M_PLGActionAddVertexColor)==0)
      {
        lNbPoints ++;
        xIndex = (long)atoi(szParams[0]);
        p_stVertexColor->d_stListOfPoints[xIndex].fR=(float)atof(szParams[1]);
        p_stVertexColor->d_stListOfPoints[xIndex].fG=(float)atof(szParams[2]);
        p_stVertexColor->d_stListOfPoints[xIndex].fB=(float)atof(szParams[3]);
      } 
      break;

    case SCR_EA_Anl_EndSection:
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstVertexColor*,p_stVertexColor);
      p_stVertexColor->xNbPoints=lNbPoints;
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}


/****************************************************************************
*		Description:	load PLG file
*
*		Parameters:		p_stFile	: script file pointer
*						szAction	: section or entry name
*						szParams	: parameters
*						cType		: action type
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
SCR_tde_Anl_ReturnValue MLT_xLoadPLGCallback(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstVertexColor **p_xVertexColorInFile;
  long					xNbVertexColor;
  
  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_xVertexColorInFile = (MLT_tdstVertexColor**)malloc(sizeof(MLT_tdstVertexColor*));
    
      SCR_M_RdL0_SetContextLong(0,0,(long)p_xVertexColorInFile);
      SCR_M_RdL0_SetContextLong(0,1,(long)0);
      break;

    case SCR_EA_Anl_EndSubSection:
      if (strcmp(szParams[0],M_VertexColor)==0)
      {
        MLT_tdstVertexColor *hVertexColor;

        SCR_M_RdL0_GetContextLong(0,0,MLT_tdstVertexColor**,p_xVertexColorInFile);
        SCR_M_RdL0_GetContextLong(0,1,long,xNbVertexColor);

        p_xVertexColorInFile = (MLT_tdstVertexColor**)realloc(p_xVertexColorInFile,(++xNbVertexColor)*sizeof(MLT_tdstVertexColor*));

        SCR_M_RdL0_GetSectionLong(1,0,MLT_tdstVertexColor*,hVertexColor);
        p_xVertexColorInFile[xNbVertexColor-1]=hVertexColor;

        SCR_M_RdL0_SetContextLong(0,0,(long)p_xVertexColorInFile);
        SCR_M_RdL0_SetContextLong(0,1,(long)xNbVertexColor);
      }
      break;

    case SCR_EA_Anl_EndSection:
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstVertexColor**,p_xVertexColorInFile);
      SCR_M_RdL0_GetContextLong(0,1,long,xNbVertexColor);
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_xVertexColorInFile);
      SCR_M_RdL0_SetSectionLong(0,1,(long)xNbVertexColor);
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}




/****************************************************************************
*		Description:	free all the PLG in the library
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vErasePLGLib()
{
  int i;

  if (gs_hVertexColorInFile == NULL) 
	  return;

  for (i=0; i<gs_lVertexColorIndex; i++)
  {
    if (gs_hVertexColorInFile[i] == NULL) 
		continue;
    free(gs_hVertexColorInFile[i]->d_stListOfPoints);
    free(gs_hVertexColorInFile[i]);
  
  }

  gs_lVertexColorIndex = 0;
  free(gs_hVertexColorInFile);
  gs_hVertexColorInFile = NULL;
}

/****************************************************************************
*		Description:	load PLG file in library
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vLoadPLGFileInLib()
{
  SCR_tdst_Cxt_Values	*p_stVal;
  xString				sDirectory;
  xString				szFileName;

  // save directory
  GetCurrentDirectory(256, sDirectory);

  //Inits 
  sprintf(szFileName, "%s.plg", g_sFileIn);

  if(SCR_fn_c_RdL0_IsSectionExists(szFileName))
  {
	p_stVal = SCR_fnp_st_RdL0_AnalyseSection(szFileName, SCR_CDF_uw_Anl_ForceAnalyse);

	gs_hVertexColorInFile = (MLT_tdstVertexColor **)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
	gs_lVertexColorIndex = (long)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,1);
    g_bExistPLG = TRUE;
  }
  else
	g_bExistPLG = FALSE;

  // restore directory
  SetCurrentDirectory(sDirectory); 
}


/****************************************************************************
*		Description:	save library in Plg file 
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveLibInPLGFile()
{
  SCR_tdst_File_Description	stFile;
  long i;
  xString sComment, sTime, sDate;

  if (! g_bExistPLG) 
	  return;

  sprintf(stFile.a_szFileName,"%s.plg", g_sFileIn);
  
  // to delete the file
  remove(stFile.a_szFileName);

  strcpy(stFile.a_szOpenFileName, stFile.a_szFileName);
  stFile.p_stHandle = (SCR_tdst_File_Handle*)malloc(sizeof(SCR_tdst_File_Handle));
	stFile.p_stHandle->xHandle = (FILE*)_open(stFile.a_szFileName, O_WRONLY | O_CREAT | _O_TEXT, S_IREAD | S_IWRITE);
	stFile.p_stHandle->p_cBase = stFile.p_stHandle->p_cCurrent = NULL;
	stFile.p_stHandle->iCnt = 0;
  stFile.d_stFirstPage = stFile.d_stLastPage = stFile.d_stCurPage = NULL;
	stFile.iCurPosInPage = 0;
  if(stFile.p_stHandle->xHandle == 0)
  {
	  MLT_vOutput( C_ComRes_cErrorLine, "\nError : Can't open file %s", stFile.a_szFileName);
	  return;
  }
  
  SCR_M_SvL0_SaveScriptFileHeader(&stFile);
  sprintf(sComment, ".PLG (Vertex Colors) : %s", stFile.a_szFileName);
  SCR_M_SvL0_SaveComment(&stFile, sComment);
  SCR_M_SvL0_SaveBlankLine(&stFile);
  sprintf(sComment, "Generated by Max23Dos Moulinette");
  SCR_M_SvL0_SaveComment(&stFile, sComment);
  SCR_M_SvL0_SaveBlankLine(&stFile);
  _strtime(sTime);
  _strdate(sDate);
  sprintf(sComment, "Created date : %s %s", sDate, sTime);
  SCR_M_SvL0_SaveComment(&stFile, sComment);
  SCR_M_SvL0_SaveBlankLine(&stFile);
  sprintf(sComment, "Version directive, the version number is stored in file result 0");
  SCR_M_SvL0_SaveComment(&stFile, sComment);
  SCR_M_SvL0_SaveDirective(&stFile, M_SetCurrentFileDouble,SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(&stFile, SCR_EF_SvL0_Scanf, 3, "%d,%.2f", 0, 1.0);
  SCR_M_SvL0_SaveBlankLine(&stFile);

  for(i=0; i<gs_lVertexColorIndex; i++)
    MLT_vSaveVertexColor(&stFile, gs_hVertexColorInFile[i]);
  
  _close((int)stFile.p_stHandle->xHandle);
  free(stFile.p_stHandle);
}









