#include "StdAfx.h"

#include "LST.hpp"
#include "SCR.h"
#include "SIF.h"
#include "SifLoadS.h"

#include "ITF/cpasaveo.hpp" // For TSN_Event&SIF_InfoValue  class definition
#include "acp_base.h" // for CPA_Tooldllb class definition
#include "ITF/tooldllb.hpp" // for CPA_Tooldllb class definition
#include "SNinterf.hpp"




class SIF_InfoValue: public CPA_SaveObject
{
public:
//	virtual CString fn_csGetReferencedSectionName (void);
	SIF_InfoValue::SIF_InfoValue(
			CString _csFileName,CString _csTypeName,CString _csValueName);
	virtual void* GetData(void) { return (void*)m_lValue; };
	virtual long GetDataType(void) { return -1;}
	long m_lValue;

	static CPA_List<SIF_InfoValue> m_sObjectList;
	static void DeleteAllValues(void);
};


CPA_List<SIF_InfoValue> SIF_InfoValue::m_sObjectList;

SIF_InfoValue::SIF_InfoValue(CString _csFileName,CString _csTypeName,CString _csValueName):
			 CPA_SaveObject(	g_poSNDEDITInterface,
								"SIF_"+_csTypeName,
								E_ss_NoSave,
								NULL,  // pOwner
								TRUE,	// bAvailaible
								"",// p_szDataPath
								NULL)	// pSaveCallback
{
	tdeMissingCriteria eAns;
	CString csSectionName,csTemp;

	// Create and set object name:
	eAns=fn_eRename("SIF_"+_csValueName);
	if (eAns!=E_mc_None)
	{
		csTemp.Format("Sound Information Value Creation error: Name SIF_%s incorrect(already exists or syntax incorrect)!",_csValueName);
		AfxMessageBox(csTemp);
	}

	// Create and set section name:
	csSectionName=_csFileName+"^Sif_Type:"+_csTypeName+"^Sif_Value:"+_csValueName;
	SetReferencedSectionName(csSectionName);
	m_lValue=-1;
}

void SIF_InfoValue::DeleteAllValues(void)
{
	SIF_InfoValue* pValue;
	POSITION Pos=NULL;

	while (pValue=m_sObjectList.GetNextElement(Pos)) delete pValue;
}



BOOL TSN_fn_bInitSoundInfos(const char* _szFileName)
{
	static	BOOL bFirstInit=TRUE;
	int iInfoType;
	
	char szCompleteName[256],szFileName[256],szActionName[256],szIdName[256];
	CString csSectionName,csValueName;
	unsigned int uiPos;
	SCR_tdst_Link_Value	*p_stValue;
	unsigned long ulValue;
	char* pszSectionName;
	SCR_tdst_Link_Table *pTable;
	SIF_InfoValue *pInfoValue;


	SCR_fnp_st_RdL0_AnalyseSection((char*)_szFileName, SCR_CDF_uw_Anl_Normal);	

	// Scan each link table:
	for (iInfoType=0;iInfoType<SIF_fn_iGetSoundInfosCount();iInfoType++)
	{	
		pTable=&SIF_fn_pstGetSoundInfosDescriptor(iInfoType)->m_Table;

		// Get first element of array :
		uiPos = 0;
		SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value,uiPos,p_stValue, 
						SCR_M_st_Link_GetDynamicArray(pTable));

		while(p_stValue) // p_sValue is NULL at the end of the array 
		{
			// Test current retrieved value:
			if(SCR_M_e_Link_GetState(p_stValue) != SCR_ELS_Link_Initialized) continue;

			// Get section complete name & value:
			ulValue=SCR_M_ul_Link_GetValue(p_stValue);
			pszSectionName=SCR_M_p_sz_Link_GetKey(p_stValue);
			
			// Transform "ed_main.sif^SIF_Type:Material^SIF_Value:Wood" into:
			//		File:ed_main.sif / Type: Material /Value: Wood

				// Split the section name, first pass:
				SCR_fn_v_RdL0_SplitSectionName(pszSectionName,
												szFileName,szActionName,szIdName);
				// With the example: szFileName="ed_main.sif^SIF_Type:Material",
				//			szActionName="SIF_Value",szIdName="Wood".

				// Save the value name, & second pass:
				csValueName=szIdName;
				strcpy(szCompleteName,szFileName);
				SCR_fn_v_RdL0_SplitSectionName(szCompleteName,
												szFileName,szActionName,szIdName);
				// With the example: szFileName="ed_main.sif",
				//			szActionName="SIF_Type",szIdName="Material".
				
				// Create an InfoValue object(Value,Type,File names):
				pInfoValue= new SIF_InfoValue(szFileName,szIdName,csValueName);
				// Set the handle to the proper value(value init need not call fn_vSetValue()):
				pInfoValue->m_lValue=ulValue;

				// Get next element :
			uiPos++;
			SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value,uiPos,p_stValue, 
						SCR_M_st_Link_GetDynamicArray(pTable));
		}


	}
	return TRUE;
}

BOOL TSN_fn_bDesInitSoundInfos(void)
{
	SIF_InfoValue::DeleteAllValues();

	return TRUE;
}


