/*=========================================================================
*
* VseSave.c	- saving functions for VSE file
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include "VseSave.h"

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

#include "Levels.h"
#include "ModLib.h"
#include "VseLoad.h"
#include "RliSave.h"


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



/****************************************************************************
*		Description:	save VSE file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveVseFile (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  xString mes;

  CopyFile(sFileName, "temp.vse", FALSE);
  SCR_fn_v_SvL1_DeleteSection(p_stFile);

  sprintf(mes, "%s%s", M_VSE_HEADER, SCR_CC_sz_Cfg_SectionIdMark);
  SCR_M_SvL0_SaveBeginSection(p_stFile, mes, SCR_CC_C_Cfg_EOL);

  SCR_M_SvL0_SaveEntry(p_stFile, M_VersionNumber, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 2, "%d", 1);

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
}


/****************************************************************************
*		Description:	save visual set section in VSE file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveVisualSet (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdstGeometricObject *p_stGeometric;
  xString mes, sFile, sAction, sIdent, sIdent2;

  p_stGeometric = (MLT_tdstGeometricObject *)p_vPtr;
  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
  }
  else
    return;

  SCR_fn_v_RdL0_SplitSectionName(p_stGeometric->sPhysicalSection, sFile, sAction, sIdent);
  sprintf(sFile, "%s%s.vse", g_sDirectorySave, g_sFileIn);
  sprintf(sIdent2, "VSE_%s", sIdent);
  SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_VS, sIdent2);
  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_stGeometric->lNbLod);

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
}

/****************************************************************************
*		Description:	save Lod section in VSE file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveLod(SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdstVSE	*p_stOldVSE;
  xString		sIdent1;
  xString		sSection, sForme, sHabillage, sName, mes, sNoLod, sFile, sAction, sIdent, sIdent2;
  BOOL			bExist=FALSE;
  long			lNoPhy, lNoLod;
  char			*sGeometricName;
  char			*sName1;

  sGeometricName = ((MLT_tdstGeometricObject *)((char *)p_vPtr))->sName;
  sName1 = ((MLT_tdstGeometricObject *)((char *)p_vPtr))->sPhysicalSection;

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_tdst_Cxt_Values *p_stVal;
    SCR_fn_v_RdL0_SplitSectionName(sName1, sFile, sAction, sIdent);
    sprintf(sFile, "%s.vse", "temp");
    sprintf(sIdent2, "VSE_%s", sIdent);
    SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_VS, sIdent2);
 	if(SCR_fn_c_RdL0_IsSectionExists(mes))
	{
		p_stVal = SCR_fnp_st_RdL0_AnalyseSection(mes, SCR_CDF_uw_Anl_Normal);
		p_stOldVSE = (MLT_tdstVSE *)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
		bExist = TRUE;
	}

    SCR_fn_v_SvL1_ToEndSection(p_stFile);
    SCR_fn_v_SvL1_ToPrevLine(p_stFile);

  }
  else
    SCR_fn_v_SvL1_DeleteSection(p_stFile);

  SCR_fn_v_RdL0_SplitSectionName(sGeometricName, sFile, sAction, sIdent);
  MLT_vSplitModuleSectionName(sIdent, sForme, sHabillage, &lNoPhy, &lNoLod, sName);

  if(lNoLod == 0) lNoLod = 1;

  SCR_g_ui_SvL0_IndentationLevel = 1;
  
  sprintf(sNoLod, "%d", lNoLod);
  SCR_fn_v_RdL0_ComputeSectionName(mes, "toto.tot", M_LOD, sNoLod);
  SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(mes), SCR_CC_C_Cfg_EOL);

  SCR_M_SvL0_SaveEntry(p_stFile, M_VSEActionLOD_Threshold, SCR_CC_C_Cfg_NoChar);
  if (!bExist)
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 2, "%.0f", pow(5.0, (double)lNoLod));
  else
	SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 2, "%d", p_stOldVSE->iVSE);

  SCR_fn_v_RdL0_SplitSectionName(sName1, sFile, sAction, sIdent1);
  if (strstr(sIdent, "!") && (!strstr(sIdent, "!NOCOL")) && (!strstr(sIdent, "!nocol")) )
  {	sName1 = strcat(MLT_p_cGetFileNameWithoutExt(sFile), ".mod");
	SCR_fn_v_RdL0_ComputeSectionName(sIdent, sName1, "Geometric", sIdent1);
	sprintf(sSection, "%s%s", g_sDirectorySave, sIdent);
  }
  else
	sprintf(sSection, "%s%s", g_sDirectorySave, sGeometricName);

  if (strstr(sSection, "!NOCOL") || strstr(sSection, "!nocol"))
  {	  sName1 = MLT_vRemoveFromName(sSection);
	  strcpy(sSection, sName1);
  }

  SCR_M_SvL0_SaveEntry(p_stFile, M_VSEActionFichierMOD, SCR_CC_C_Cfg_NoChar);

  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection);

  if(g_bGenerateRLI && g_lLightIndex && g_bExistSpoBank)
  {
		char *cc;
		sprintf(sSection, "%s%s", g_sDirectorySave, sGeometricName);
		cc = strstr(sSection, "mod");
		if(cc) lstrcpy(cc, "rli^RLI");
		cc = strchr(sGeometricName, ':');
		if(cc) lstrcat(sSection, cc);
		SCR_M_SvL0_SaveEntry(p_stFile, M_VSEActionFichierRLI, SCR_CC_C_Cfg_NoChar);
	  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection);
	}

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

  SCR_g_ui_SvL0_IndentationLevel = 0;
}


/****************************************************************************
*		Description:	notify Lod section for save
*
*		Parameters:		_sVisualSetName	: name of visual set
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
long MLT_lSaveLodInFile(char *_sVisualSetName)
{
  MLT_tdstGeometricObject	**h_stObj;
  xString					mes, sFile, sAction, sIdent, sIdent2;
  xString					sForme, sHabillage, sName, sNoLod, sVisualSetName;
  long						lNbObj, i, lNoPhy, lNoLod, lNbLod;

  SCR_fn_v_RdL0_SplitSectionName(_sVisualSetName, sFile, sAction, sIdent);
  sprintf(sIdent2, "VSE_%s", sIdent);
  SCR_fn_v_RdL0_ComputeSectionName(sVisualSetName, sFile, sAction, sIdent2);
  MLT_vRequestAlmostInLib(sIdent, &h_stObj, &lNbObj);

  lNbLod = 0;
  for(i=0; i<lNbObj; i++)
  {
    SCR_fn_v_RdL0_SplitSectionName(h_stObj[i]->sName, sFile, sAction, sIdent);
    MLT_vSplitModuleSectionName(sIdent, sForme, sHabillage, &lNoPhy, &lNoLod, sName);
    if(lNoLod == 0) 
		lNoLod = 1;
    lNbLod = (lNbLod<lNoLod ? lNoLod : lNbLod);
    sprintf(sNoLod, "%d", lNoLod);
    SCR_fn_v_RdL0_ComputeSectionName(mes, sVisualSetName, M_LOD, sNoLod);
    SCR_fn_v_SvL1_RegisterNotify(mes, MLT_vSaveLod, h_stObj[i], SCR_EA_Ntfy_AddOrModifySection);
  }

  return lNbLod;
}

