/*=========================================================================
*
* ZooSave.c	- loading functions for ZOO file
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include "ZooSave.h"

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

#include "geoobj.h"

#include "ModLib.h"
#include "ObjLoad.h"
#include "ZooLoad.h"


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

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

  if (g_bIgnoreZoo)
  {
	  sprintf(sComment, "Unit directive, the unit is stored in file result 1");
	  SCR_M_SvL0_SaveComment(p_stFile, sComment);
	  sprintf(sComment, "And correspond to the value of one unit exprimed in meter");
	  SCR_M_SvL0_SaveComment(p_stFile, sComment);
	  SCR_M_SvL0_SaveDirective(p_stFile, M_SetCurrentFileDouble,SCR_CC_C_Cfg_NoChar);
	  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%d,%.2f", 1, 1.0);
	  SCR_M_SvL0_SaveBlankLine(p_stFile);

	  sprintf(mes, "%s%s", M_ZOO_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 AllCollideSets section in ZOO file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveAllCollideSetsSection (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  xString mes;

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
  }

  if ((xAction == SCR_EA_Ntfy_AddSection) ||
	  (xAction == SCR_EA_Ntfy_RebuildSection) ||
	  (xAction == SCR_EA_Ntfy_AddOrRebuildSection))
  {
	  sprintf(mes, "%s%s", M_AllCollideSets, SCR_CC_sz_Cfg_SectionIdMark);
	  SCR_M_SvL0_SaveBeginSection(p_stFile, mes, SCR_CC_C_Cfg_EOL);

	  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
  }
}


/****************************************************************************
*		Description:	save zones section in ZOO file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveZoo (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdstGeometricObject	*p_stGeometric, *p_stObj;
  SCR_tdst_Cxt_Values		*p_stVal;
  MLT_tdstZoo				*p_stZoo;
  xString					mes, sFile, sAction, sIdent, sIdent2;
  xString					sForme, sHabillage, sName, sSection, sSection1;
  long						lNoPhy, lNoLod;
  char						*sPhysicalName;
  int						i;


  p_stGeometric = (MLT_tdstGeometricObject *)p_vPtr;
  sPhysicalName = p_stGeometric->sPhysicalSection;

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
    SCR_fn_v_SvL1_ToPrevLine(p_stFile);
  }
  else
    SCR_fn_v_SvL1_DeleteSection(p_stFile);

  SCR_g_ui_SvL0_IndentationLevel = 1;

  if (!g_bIgnoreZoo)
  {
	sprintf(sFile, "%s.zoo", "temp");
	SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_AllCollideSets, "");
	if(SCR_fn_c_RdL0_IsSectionExists(mes))
	{
		p_stVal = SCR_fnp_st_RdL0_AnalyseSection(mes, SCR_CDF_uw_Anl_Normal);
        p_stZoo = (MLT_tdstZoo*)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);

		SCR_fn_v_RdL0_SplitSectionName(sPhysicalName, sFile, sAction, sIdent);
		sprintf(sFile, "%s%s.zoo", g_sDirectorySave, g_sFileIn);
		sprintf(sIdent2, "CS_%s", sIdent);
		i=MLT_iIndex(sIdent2, p_stZoo);
		SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_CS, sIdent2);
	    SCR_fn_v_SvL1_ToEndSection(p_stFile);
		SCR_fn_v_SvL1_ToPrevLine(p_stFile);
		if ((!strstr(p_stGeometric->sName, "!NOCOL"))&&(!strstr(p_stGeometric->sName, "!nocol"))) 
		{
			SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(mes), SCR_CC_C_Cfg_EOL);
			SCR_M_SvL0_SaveEntry(p_stFile, M_ZOOActionZDR, SCR_CC_C_Cfg_NoChar);
			if (i!=-1) SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stZoo->sZDR[i]);
			else 
			{
				p_stObj = MLT_pFindInLib(MLT_p_cGetSectionName(mes));
				if(p_stObj != NULL)
				{
					sprintf(mes, "%s%s", g_sDirectorySave, p_stGeometric->sName);
					SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, mes);
				}
				else
				{
					sprintf(mes, "%s%s", g_sDirectorySave, p_stGeometric->sName);
					SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, mes);
				}	
			}
			SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
		}
		SCR_g_ui_SvL0_IndentationLevel = 0;
		return;
	}
  }

  if ((!strstr(p_stGeometric->sName, "!NOCOL"))&&(!strstr(p_stGeometric->sName, "!nocol"))) 
  {
	SCR_fn_v_RdL0_SplitSectionName(sPhysicalName, sFile, sAction, sIdent);
	sprintf(sFile, "%s%s.zoo", g_sDirectorySave, g_sFileIn);
	sprintf(sIdent2, "CS_%s", sIdent);
	SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_CS, sIdent2);

	SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(mes), SCR_CC_C_Cfg_EOL);

	SCR_fn_v_RdL0_SplitSectionName(p_stGeometric->sName, sFile, sAction, sIdent);
	MLT_vSplitModuleSectionName(sIdent, sForme, sHabillage, &lNoPhy, &lNoLod, sName);
	MLT_vComputePhysicalSectionName(sIdent, sForme, M_Collide, lNoPhy, sName);
	SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, sAction, sIdent);

	SCR_M_SvL0_SaveEntry(p_stFile, M_ZOOActionZDR, SCR_CC_C_Cfg_NoChar);
	p_stObj = MLT_pFindInLib(sSection);
	if(p_stObj != NULL)
	{
		sprintf(sSection1, "%s%s", g_sDirectorySave, sSection);
		SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection1);
	}
	else
	{
		sprintf(sSection, "%s%s", g_sDirectorySave, p_stGeometric->sName);
		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:	save zones section from OBJ file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveZooFromObj (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  MLT_tdstObjElement	*p_stGeometric;
  SCR_tdst_Cxt_Values	*p_stVal;
  MLT_tdstZoo			*p_stZoo;
  xString				mes, sFile, sAction, sIdent, sIdent2;
  int					i;

  p_stGeometric = (MLT_tdstObjElement *)p_vPtr;
  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
    SCR_fn_v_SvL1_ToPrevLine(p_stFile);
  }
  else
    if( g_bIgnoreZoo && (g_cCategoryType == C_Family))
    {
      SCR_fn_v_SvL1_DeleteSection(p_stFile);
    }
    else
      return;

  SCR_g_ui_SvL0_IndentationLevel = 1;

  if (!g_bIgnoreZoo)
  {
	sprintf(sFile, "%s.zoo", "temp");
	SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_AllCollideSets, "");
	if(SCR_fn_c_RdL0_IsSectionExists(mes))
	{
		p_stVal = SCR_fnp_st_RdL0_AnalyseSection(mes, SCR_CDF_uw_Anl_Normal);
        p_stZoo = (MLT_tdstZoo*)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);

		SCR_fn_v_RdL0_SplitSectionName(p_stGeometric->sSectionObj, sFile, sAction, sIdent);
		sprintf(sFile, "%s%s.zoo", g_sDirectorySave, g_sFileIn);
		sprintf(sIdent2, "CS_%s", sIdent);
		i=MLT_iIndex(sIdent2, p_stZoo);
		SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_CS, sIdent2);
	    SCR_fn_v_SvL1_ToEndSection(p_stFile);
		SCR_fn_v_SvL1_ToPrevLine(p_stFile);

		if (!p_stGeometric->bFlag)
		{	SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(mes), SCR_CC_C_Cfg_EOL);
			if ((i!=-1) && strcmp(p_stZoo->sZDR[i], ""))
			{	SCR_M_SvL0_SaveEntry(p_stFile, M_ZOOActionZDR, SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stZoo->sZDR[i]);
			}
			if ((i!=-1) && strcmp(p_stZoo->sZDM[i], ""))
			{	SCR_M_SvL0_SaveEntry(p_stFile, M_ZOOActionZDM, SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stZoo->sZDM[i]);
			}
			if ((i!=-1) && strcmp(p_stZoo->sZDE[i], ""))
			{	SCR_M_SvL0_SaveEntry(p_stFile, M_ZOOActionZDE, SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stZoo->sZDE[i]);
			}
			if ((i!=-1) && strcmp(p_stZoo->sZDD[i], ""))
			{	SCR_M_SvL0_SaveEntry(p_stFile, M_ZOOActionZDD, SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stZoo->sZDD[i]);
			}
			if((g_cCategoryType != C_Family) && (i==-1))
				SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stGeometric->sSectionMod);
			SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
		}

		SCR_g_ui_SvL0_IndentationLevel = 0;
		return;
	}
  }

  if (!p_stGeometric->bFlag)
  {
	SCR_fn_v_RdL0_SplitSectionName(p_stGeometric->sSectionObj, sFile, sAction, sIdent);
	sprintf(sFile, "%s%s.zoo", g_sDirectorySave, g_sFileIn);
	sprintf(sIdent2, "CS_%s", sIdent);
	SCR_fn_v_RdL0_ComputeSectionName(mes, sFile, M_CS, sIdent2);
	SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(mes), SCR_CC_C_Cfg_EOL);
  }

  if ((g_cCategoryType != C_Family) && !((g_cCategoryType == C_Bank) && g_bCreateBasicFamily))
  {
    SCR_M_SvL0_SaveEntry(p_stFile, M_ZOOActionZDR, SCR_CC_C_Cfg_NoChar);
    SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stGeometric->sSectionMod);
  }

  if (!p_stGeometric->bFlag)
  {
    SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
  }

  SCR_g_ui_SvL0_IndentationLevel = 0;
}
