/*=========================================================================
* 
* edit_ipo.hpp :  EditorIPO class - definition
* 
* Version			3.0
* Creation date		09/04/97	Author : Vincent Lhullier	
* Revision date		26/06/97	Author : Shaitan	
*					03/07/97	Author : Vincent Lhullier	
*					18/08/97	Author : Shaitan	
*
* Shaitan
*=======================================================================*/

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "stdafx.h"
#include "acp_base.h"
#include "ITF.h"
#include "OIT/Edit_IPO.hpp"
#include "IPO_intf.hpp"
#include "DPT.h"
#include "ISI.h"
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
DeclareTemplateStatic(IPO_tdxHandleToIPO); 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

/*===========================================================================
*		Description:	Static init (engine struct)
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:					Author:         
*=========================================================================*/
void EditorIPO::StaticInit (void)
{
	CPA_EdMot<IPO_tdxHandleToIPO>::Init
		(
			IPO_fn_hAllocInstanciatedPhysicalObject,
			NULL,
			IPO_fn_vFreeInstanciatedPhysicalObject
		);
}


//#################################################################################
// Constructor / Destructor
//#################################################################################

/*===========================================================================
*		Description:	Constructor
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:	18/08/97		Author: Shaitan     
*=========================================================================*/
EditorIPO::EditorIPO (CPA_ObjectDLLBase		*p_oDLL, 
					  IPO_tdxHandleToIPO	hObj, 
					  CString				csName, 
					  CString				csFileName, 
					  CString				csSectionName,
					  BOOL					bExistingSection,
					  unsigned long			ulType
					  )

	: CPA_SaveObject (p_oDLL,									// owner editor
					  C_szInstanciatedPhysicalObjectTypeName,	// type
					  E_ss_Responsible,							// status
					  NULL,										// owner
					  TRUE,										// validity
					  fn_szGetLevelsDataPath(),					// data path
					  EditorIPO::CallBackSaveIPO				// call back for save
					 ),

	  CPA_EdMot<IPO_tdxHandleToIPO> (hObj),
	  m_ulType(ulType)

{
	PO_tdxHandleToPhysicalObject	hPO;
	CPA_ObjectDLLBase				*p_oDLLPO;
	char							szSectionName[SCR_CV_ui_Cfg_MaxLenName];
	char							*szLevelsDataPath = fn_szGetLevelsDataPath();

	
	// section object
    SetSectionData(this);

	// check if filename is complete filename
	if (strnicmp( (char *)(LPCTSTR) csFileName, szLevelsDataPath, strlen(szLevelsDataPath) ) == 0)
		csFileName = csFileName.Mid( strlen( szLevelsDataPath ) + 1 );

	// section name
	SCR_fn_v_RdL0_ComputeSectionName
		(
			szSectionName, 
			(char *)(LPCTSTR)csFileName, 
			(char *) (LPCTSTR) csSectionName, 
			" "
		);
	SetReferencedSectionName(szSectionName);
	
	// section exists !
	SetExistingSection( bExistingSection );

	// build editor PO
	p_oDLLPO = GetMainWorld()->GetObjectDLLWithName(C_szDLLPhysicalObjectName);
	if (p_oDLLPO != NULL)
	{
		hPO = IPO_fn_hGetPhysicalObject( hObj );
		m_p_oPhysicalObject = (EditorPO *) p_oDLLPO->OnQueryAction( NULL, OPD_ActionEncapsulateObject, (LPARAM) hPO );
	}

	
	// TEMP comment by jt
	// build RLI section
//	m_pSectionRLI = new CPA_SectionObject 
//		(
//			GetRLISectionName(), 
//			fn_szGetLevelsDataPath(),
//			EditorIPO::CallBackSaveRLI, 
//			this, 
//			fn_bCanSaveRLI()
//		);

	// name
	if (fn_eRename(csName) != E_mc_None)
		SetDefaultValidName();

	fn_vUpdateSectionName();
};

/*===========================================================================
*		Description:	Copy-Constructor
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:	18/08/97		Author: Shaitan     
*=========================================================================*/
EditorIPO::EditorIPO (EditorIPO &r_oSource)
	: CPA_SaveObject(r_oSource), 
	  CPA_EdMot<IPO_tdxHandleToIPO>()
{
	PO_tdxHandleToPhysicalObject	hPO;
	
	CString	csSectionName;
	char	szSectionName[SCR_CV_ui_Cfg_MaxLenName];
	char	szActionName[SCR_CV_ui_Cfg_MaxLenName];
	char	szFileName[SCR_CV_ui_Cfg_MaxLenName];
	char	szName[SCR_CV_ui_Cfg_MaxLenName];

	
	// copy physical object
	m_p_oPhysicalObject = r_oSource.m_p_oPhysicalObject;
	m_ulType = r_oSource.m_ulType;
	hPO = IPO_fn_hGetPhysicalObject((IPO_tdxHandleToInstanciatedPhysicalObject) r_oSource.GetEngineStruct());
	IPO_fn_vSetPhysicalObject(GetStruct(), hPO);
	// init radiosoty
	IPO_fn_vSetRadiosity(GetStruct(), NULL);

// TEMP comment by jt
//	m_pSectionRLI = new CPA_SectionObject 
//		(
//			GetRLISectionName(), 
//			fn_szGetLevelsDataPath(),
//			EditorIPO::CallBackSaveRLI, 
//			this, 
//			FALSE
//		);

	// section object
    SetSectionData(this);
	// section name
	csSectionName = r_oSource.GetReferencedSectionName();
	SCR_fn_v_RdL0_SplitSectionName((char*)(LPCTSTR) csSectionName, szFileName, szActionName, szName);
	SCR_fn_v_RdL0_ComputeSectionName(szSectionName, szFileName, szActionName, " ");
	SetReferencedSectionName(szSectionName);

	// existence
	SetExistingSection(FALSE);

	// name
	SetDefaultValidName();
	fn_vUpdateSectionName();

	// notify
	fn_vNotifySave();
}

/*===========================================================================
*		Description:	Destructor
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:					Author:		  
*=========================================================================*/
EditorIPO::~EditorIPO()
{
	if (M_GetMainApp()->m_bLeavingApplication == FALSE)
	{
		if (m_p_oPhysicalObject != NULL)
			delete m_p_oPhysicalObject;
	}
}


//#################################################################################
// RLI section
//#################################################################################


/*===========================================================================
*		Description:	Get section name for RLI
*		Creation date:	26/06/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:		  
*=========================================================================*/
CString EditorIPO::GetRLISectionName( void )
{
	CString oSectionName;
	CString oFileName, oRLIFileName;
	CString oName, oRLIName;
	char	szSectionName[SCR_CV_ui_Cfg_MaxLenName];
	char	szFileName[SCR_CV_ui_Cfg_MaxLenName];
	char	szName[SCR_CV_ui_Cfg_MaxLenName];
	char	*p_cReplace;


	// get name & file name
	oSectionName = GetReferencedSectionName();
	SCR_fn_v_RdL0_SplitSectionName((char*)(LPCSTR) oSectionName, szFileName, szSectionName, szName);
	oFileName = szFileName;
	oName = szName;
	
	// build RLI name
	oRLIFileName = oFileName.Left (oFileName.GetLength() - 4) + ".RLI";
//ANNECY Shaitan Correction 07/05/98 {
	while ((p_cReplace = strstr(szName,"IPO")) !=NULL)
	{
		*p_cReplace = 'I';
		p_cReplace++;
		*p_cReplace = 'S';
		p_cReplace++;
		*p_cReplace = 'I';
	}
	oRLIName = szName;
//ENDANNECY Shaitan Correction }
	
	// build section name
	SCR_fn_v_RdL0_ComputeSectionName
		(
			szSectionName, 
			(char*)(LPCSTR)oRLIFileName, 
			"ISI", 
			(char*)(LPCSTR)oRLIName
		);

	oSectionName = szSectionName;

	return oSectionName;
}

/*===========================================================================
*		Description:	Test if there is RLI section
*		Creation date:	26/06/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:		  
*=========================================================================*/
BOOL EditorIPO::fn_bCanSaveRLI (void)
{
	return (IPO_fn_hGetRadiosity(GetStruct()) != NULL);
}


//#################################################################################
// Notifications
//#################################################################################

/*===========================================================================
*		Description:	notification to save IPO
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:	26/06/97		Author:	Shaitan	  
*=========================================================================*/
void EditorIPO::fn_vNotifySave (void)
{
	// check save status
	if (!fn_bCanBeNotified())
		return;
	
	// main section
	CPA_SaveObject::fn_vNotifySave();
	// TEMP comment by jt
	// RLI section
//	if (fn_bCanSaveRLI())
//		m_pSectionRLI->fn_vWriteSection();
}

/*===========================================================================
*		Description:	notification to delete IPO
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:	26/06/97		Author:	Shaitan	  
*=========================================================================*/
void EditorIPO::fn_vNotifyUnSave (void)
{
	// check save status
	if (!fn_bCanBeNotified())
		return;
	
	// main section
	CPA_SaveObject::fn_vNotifyUnSave();
	// TEMP comment by jt
	// RLI section
//	if (fn_bCanSaveRLI())
//		m_pSectionRLI->fn_vDeleteSection();
}

/*===========================================================================
*		Description:	restore IPO notification 
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:	26/06/97		Author:	Shaitan	  
*=========================================================================*/
void EditorIPO::fn_vNotifyRestore(void)
{
	// check save status
	if (!fn_bCanBeNotified())
		return;
	
	// main section
	CPA_SaveObject::fn_vNotifyRestore();
	// TEMP comment by jt
	// RLI section
//	if (fn_bCanSaveRLI())
//		m_pSectionRLI->fn_vRestoreSection();
}

/*===========================================================================
*		Description:	notification to rename IPO
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:	26/06/97		Author:	Shaitan	  
*=========================================================================*/
void EditorIPO::fn_vNotifyRename(void)
{
	// check save status
	if (!fn_bCanBeNotified())
		return;
	
	// main section
	CPA_SaveObject::fn_vNotifyRename();
	// TEMP comment by jt
	// RLI section
//	if (fn_bCanSaveRLI())
//		m_pSectionRLI->fn_vRenameSection(GetRLISectionName());
}

/*===========================================================================
*		Description:	change sections names when IPO is renamed
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:	26/06/97		Author:	Shaitan	  
*=========================================================================*/
void EditorIPO::fn_vUpdateSectionName (void)
{
	// update main section
	CPA_SaveObject::fn_vUpdateSectionName();
	// update matrix section
	// TEMP comment by jt
//	m_pSectionRLI->SetReferencedSectionName(GetRLISectionName());
}



//#################################################################################
// Callbacks
//#################################################################################

/*===========================================================================
*		Description:	callback to save IPO
*		Creation date:	
*		Author:			Vincent Lhullier
*---------------------------------------------------------------------------
*		Revision date:	26/06/97		Author:	Shaitan	  
*=========================================================================*/
void EditorIPO::CallBackSaveIPO( SCR_tdst_File_Description *p_stFile, char *p_szSectionName, void *p_vData, SCR_tde_Ntfy_Action eAction)
{
	EditorIPO	*p_oIPO;
	char		szActionName[SCR_CV_ui_Cfg_MaxLenName];
	char		szName[SCR_CV_ui_Cfg_MaxLenName];
	char		szSection[SCR_CV_ui_Cfg_MaxLenName];

	// get parameters
	p_oIPO = (EditorIPO *) p_vData;
	
	switch (eAction)
	{
		case SCR_EA_Ntfy_AddSection:
			// add section => end of file
			SCR_fn_v_SvL1_ToEndSection(p_stFile);
			SCR_M_SvL0_SaveBlankLine (p_stFile);
		
		case SCR_EA_Ntfy_RebuildSection:
			// begin section
			SCR_fn_v_RdL0_SplitSectionName( p_szSectionName, NULL, szActionName, szName);
			SCR_fn_v_RdL0_ComputeSectionName( szSection, NULL, szActionName, szName);
			SCR_M_SvL0_SaveBeginSection(p_stFile, szSection, SCR_CC_C_Cfg_EOL);
			// PO Entry
			SCR_M_SvL0_SaveEntry(p_stFile, "PO", SCR_CC_C_Cfg_NoChar);
			SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, 
						(char *) (LPCTSTR) p_oIPO->GetPhysicalObject()->GetReferencedSectionName());
			// RLI Entry
			if (p_oIPO->fn_bCanSaveRLI())
			{
				SCR_M_SvL0_SaveEntry(p_stFile, "RLI", SCR_CC_C_Cfg_NoChar);
//		TEMP jt
//				SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, 
//						(char *) (LPCTSTR) p_oIPO->GetSectionRLI()->GetReferencedSectionName());
				SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, 
						(char *) (LPCTSTR) p_oIPO->GetRLISectionName());
//		end jt


			}
			// end section
			SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
			// update instance
			p_oIPO->fn_vSectionSaved();
			break;

		case SCR_EA_Ntfy_DeleteSection:
			// update instance
			p_oIPO->fn_vSectionDeleted();
			break;
	}
}


/*===========================================================================
*		Description:	callback to save IPO
*		Creation date:	26/06/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:	  
*=========================================================================*/
/*void EditorIPO::CallBackSaveRLI( SCR_tdst_File_Description *p_stFile, char *p_szSectionName, void *p_vData, SCR_tde_Ntfy_Action eAction)
{
	ACP_tdxHandleToRadiosity	hRLI;
	CPA_SectionObject			*pSectionRLI;
	ISI_tdstColor				*a_stVertexRLI;
	ACP_tdxIndex				xI, xNbVertex, xLod;
	EditorIPO					*p_oIPO;
	char						szActionName[SCR_CV_ui_Cfg_MaxLenName];
	char						szName[SCR_CV_ui_Cfg_MaxLenName];
	char						szSection[SCR_CV_ui_Cfg_MaxLenName];


	// get parameters
	p_oIPO = (EditorIPO *) p_vData;
	pSectionRLI = p_oIPO->GetSectionRLI();
	
	// no RLI => no section
	if (!p_oIPO->fn_bCanSaveRLI())
		return;
	
	switch (eAction)
	{
		case SCR_EA_Ntfy_AddSection:
			// add section => end of file
			SCR_fn_v_SvL1_ToEndSection(p_stFile);
			SCR_M_SvL0_SaveBlankLine (p_stFile);
		
		case SCR_EA_Ntfy_RebuildSection:
			// get section name
			SCR_fn_v_RdL0_SplitSectionName( p_szSectionName, NULL, szActionName, szName);
			SCR_fn_v_RdL0_ComputeSectionName( szSection, NULL, szActionName, szName);
			// get engine RLI
			hRLI = IPO_fn_hGetRadiosity((IPO_tdxHandleToInstanciatedPhysicalObject) p_oIPO->GetEngineStruct());
			// save begin section
			// save begin section
			SCR_M_SvL0_SaveBeginSection(p_stFile, szSection, SCR_CC_C_Cfg_NoChar);
			SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 2,"%s,%d", "NBRLOD", ISI_fn_xGetNbLOD(hRLI));
			// For all LOD.
			for (xLod  =0; xLod < ISI_fn_xGetNbLOD(hRLI); xLod++)
			{
				// get parameters
				xNbVertex = ISI_fn_xGetNbVertexRLILOD(hRLI, xLod);
				a_stVertexRLI = ISI_fn_dGetVertexRLILOD(hRLI, xLod);
				// save entries
				SCR_M_SvL0_SaveEntry(p_stFile, "AddLODRLI", SCR_CC_C_Cfg_NoChar);
				SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3,
												  "%d,%d", xLod, xNbVertex);
				// For all RLI of the LOD.
				for (xI = 0; xI < xNbVertex; xI++)    
				{
					SCR_M_SvL0_SaveEntry(p_stFile, "AddVertexRLILOD", SCR_CC_C_Cfg_NoChar);
					SCR_fn_v_SvL0_SaveParameters_MP(
						  p_stFile,
						  SCR_EF_SvL0_Scanf,
						  5,
						  "%d,%d,%d,%d",
						  xI,
						  (int)a_stVertexRLI[xI].xRed,
						  (int)a_stVertexRLI[xI].xGreen,
						  (int)a_stVertexRLI[xI].xBlue);
				}
			}
			// end section
			SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);
			// update instance
			pSectionRLI->fn_vSectionSaved();
			break;

		case SCR_EA_Ntfy_DeleteSection:
			// update instance
			pSectionRLI->fn_vSectionDeleted();
			break;
	}
}
*/