//#include "ACP_Base.h"
#include <StdAfx.h>


#include "SCR.h"
#include "ITF/cpasaveo.hpp"
#include "acp_base.h" // for CPA_Tooldllb class definition
#include "ITF/tooldllb.hpp" // for CPA_Tooldllb class definition
#include "ITF/cpainter.hpp"

#include "SNInterf.hpp"
#include "TSNEvent.h"

#include "x:\cpa\main\inc\_editid.h"


//-----------------------------------------------------------------------------
//  Name : TSN_Event:m_fn_eLoad
//-----------------------------------------------------------------------------
//  Description :   Script Callback for the event section.
//					For each event section found, creates a TSN_Event section,
//					sets its section name (with a SndEventM action name),
//					and its name for the cpa lists( SetName() entry of SndEventE).   
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue TSN_Event::m_fn_eLoad(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	TSN_Event *pEvent;
	CString csName,csTemp;
	int iPos;
	tdeMissingCriteria eAns;

	SCR_tdst_Sect_Open *pOpenSection;
	SND_tdstBlockEvent *pEngineEvent;
	switch (cType)
	{
		case SCR_EA_Anl_BeginSection:// Creation of event :
			// Create event:
			pEvent=new TSN_Event();
			SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pEvent);
			g_poSNDEDITInterface->m_EdiEventList.AddTail(pEvent);

			// Transform the SndEventE in SndEventM:
			csName=SCR_M_RdL0_GetCompleteSectionNameR(0);
			iPos=csName.Find("SndEventE");
			if (iPos!=-1) csName.SetAt(iPos+8,'M');
			// set the engine event pointer is event is loaded:
			pOpenSection=SCR_fnp_st_RdL0_GetOpenSection((char*)(LPCTSTR)csName,0);
			if (pOpenSection!=NULL) // engine evnt is loaded
			{
				pEngineEvent=(SND_tdstBlockEvent *)SCR_M_ul_RdL0_ExtractLongValue(&pOpenSection->stSectionValues,0);
				if (pEngineEvent!=pEvent->m_pEngineEvent)
				{
					pEvent->fn_vUpdateData(pEngineEvent);
					pEvent->m_pEngineEvent=pEngineEvent;
				}
				pEvent->SetAvailable(TRUE);
			}
			
			pEvent->SetReferencedSectionName(csName);
			break;

		case SCR_EA_Anl_Entry://  event data:
			SCR_M_RdL0_GetSectionLong(0,0,TSN_Event *,pEvent);
			if (strcmp(szAction,"SetName")==0)
			{
				// Compute csb name, without the '.csb' extension:
				csName=SCR_M_RdL0_GetFileNameR(0);
				csTemp=csName.Left(csName.GetLength()-4);
				// crate event name as registered in name list:
				csName.Format("SND_%s_%s",szParams[0],csTemp);
				eAns=pEvent->fn_eRename(csName);
				if (eAns!=E_mc_None)
				{
					csTemp.Format("Sound Editor Event Creation error: Name SND_%s incorrect(already exists or syntax incorrect)!",csName);
					AfxMessageBox(csTemp);
				}
				// We have the event group's name: no need to continue this section analysis:
				return  SCR_ERV_Anl_TerminateCurrentSection;
			}
			break;
	}
	return  SCR_ERV_Anl_NormalReturn;
}

//-----------------------------------------------------------------------------
//  Name : TSN_Event:GetData()
//-----------------------------------------------------------------------------
//  Description :   TSN_Event object constructor.
//					Creates a new TSN_Event object, for the event object server
//-----------------------------------------------------------------------------
//  Input :  void       
//  Output : void    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/
/*
void *TSN_Event::GetData(void)
{
	SCR_tdst_Cxt_Values* pContext;
	static BOOL bInDataModif=FALSE;
	SND_tdstBlockEvent *pNewEvent;

	// If we already stocked the event pointer, it is supposed to be good:
	if (m_pEngineEvent!=NULL) return(void *)m_pEngineEvent;
	
	
	if (g_poSNDEDITInterface->GetInterface()->fn_bIsLoadingWorld())
	return (void *)m_pEngineEvent;
	
	// This is for fn_vUpdateData, which calls GetData() and must get the old pointer:
	if (bInDataModif) return (void *)m_pEngineEvent;


	// We never LOAD the data here -> the section must have already be loaded by someone else: 
	if (SCR_fnp_st_RdL0_GetOpenSection((char*)(LPCTSTR)GetReferencedSectionName(),0)==NULL)
		return NULL;


	// The event is loaded: get its pointer :  
	pContext=SCR_fnp_st_RdL0_AnalyseSection(
		(char*)(LPCTSTR)GetReferencedSectionName(),SCR_CDF_uw_Anl_Normal);
	pNewEvent=(SND_tdstBlockEvent *)SCR_M_ul_RdL0_ExtractLongValue(pContext,0);
	if (pNewEvent!=NULL)
	{
		bInDataModif=TRUE;
		fn_vUpdateData(pNewEvent);
		m_pEngineEvent=pNewEvent;
		bInDataModif=FALSE;
	}
	
	return (void *)m_pEngineEvent;
}
*/

void *TSN_Event::GetData(void)
{
	return (void *)m_pEngineEvent;
}






//-----------------------------------------------------------------------------
//  Name : TSN_Event:TSN_Event()
//-----------------------------------------------------------------------------
//  Description :   TSN_Event object constructor.
//					Creates a new TSN_Event object, for the event object server
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/

TSN_Event::TSN_Event():
	CPA_SaveObject(g_poSNDEDITInterface,C_szSoundEventTypeName,
							E_ss_NoSave,
							NULL,  // pOwner
							TRUE,	// bAvailaible
							"",		// p_szDataPath
							NULL)	// pSaveCallback
{
		m_pEngineEvent=NULL;
		SetAvailable(FALSE);
}



BOOL TSN_Event::fn_bEngineLoad()
{
	SCR_tdst_Cxt_Values* pContext;
	SND_tdstBlockEvent *pNewEvent;

	// The event is loaded: get its pointer :  
	pContext=SCR_fnp_st_RdL0_AnalyseSection(
					(char*)(LPCTSTR)GetReferencedSectionName(),
					SCR_CDF_uw_Anl_Normal);
	pNewEvent=(SND_tdstBlockEvent *)SCR_M_ul_RdL0_ExtractLongValue(pContext,0);
	if (pNewEvent!=m_pEngineEvent)
	{
		fn_vUpdateData(pNewEvent);
		m_pEngineEvent=pNewEvent;
	}
	SetAvailable(TRUE);

	return TRUE;
}

