#include "StdAfx.h"

//External Modules
#include "ACP_Base.h"
#include "ITF.h"
#include "TAC.h"
#include "TFA.h"
#include "IncMEC.h"
#define D_State_Define
#include "IncGAM.h"
#undef D_State_Define
#include "GLI.h"
//End of External Modules

#include "EDACStrg.hpp"
#include "EDACInst.hpp"
#include "EDACModl.hpp"
#include "EDACDgIf.hpp"
#include "EDACDgPI.hpp"
#include "EDACGlob.hpp"
#include "Defines.hpp"

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

//-----------------IR-------------------------
#include "EdIRStrg.hpp"
#include "EdIRFrm.hpp"
#include "EdIRBeEn.hpp"
#include "EDACQuer.hpp"
//--------------------------------------------

//External Modules
#include "ErO.h"
//#include "CTL.h"
//End of External Modules

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
//-----------------IR-------------------------
		#include "EdIRIRD.hpp"
//--------------------------------------------
    #include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT

//*************************************************************************
// Model Loading Function (Direct)
//*************************************************************************
CPA_Actor *EdActors_MyDocument::m_fn_pclLoadModel(CString csModelName /*=""*/,
																									CPA_Actor *pclModel /*= NULL*/)
{
	CPA_Actor *pclLoadedModel = NULL;

	CString csTrueFileName;
	BOOL bMustGoOn = TRUE;

	if ( csModelName.IsEmpty() )
	{
/*		CString csFilters("Actor Model File(*.eam)|*.eam|All Files|*.*|");
		CFileDialog dial(	TRUE,
											"*.ema",
											"*.ema",
											OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
											csFilters);
		
		char a_cOldDir[MAX_PATH];
		GetCurrentDirectory(MAX_PATH, (char *)(&a_cOldDir));
		CString csOldDir(a_cOldDir);
		SetCurrentDirectory(m_csModelsSavePath);

		if ( dial.DoModal() == IDOK )
			csTrueFileName = dial.GetFileName();
		else
			bMustGoOn = FALSE;

		SetCurrentDirectory(csOldDir);
*/
	}
	else
		csTrueFileName = csModelName + g_csModelExtension;

	if ( bMustGoOn )
	{
		//Loads the Model
		CString csCompleteSectionName = csTrueFileName;
		csCompleteSectionName += '^';
		csCompleteSectionName += g_c_csActorScriptModelSectionName + ':';
		csModelName = g_c_csModelNamePrefix + csModelName;
		CString csModelTrueName = csModelName.Right(csModelName.GetLength() - csModelName.ReverseFind('\\') - 1);
		csCompleteSectionName += csModelTrueName;

		//Gives the Model to the CallBack
		SCR_M_RdL0_SetContextLong(1, 0, pclModel);

		AfxGetApp()->DoWaitCursor(1);

		BOOL bIsLoadingWorld = g_pclInterface->GetInterface()->fn_bIsLoadingWorld();
		g_pclInterface->GetInterface()->SetLoadingWorld(TRUE);

		SCR_tdst_Cxt_Values *p_stValues;
		p_stValues = SCR_fnp_st_RdL0_AnalyseSection((char *)LPCTSTR(csCompleteSectionName),
																								SCR_CDF_uw_Anl_Normal);


		ERROR_RESET_SCRIPT_INFO

		//Displays the last loaded Actor
		pclLoadedModel = (CPA_Actor *)p_stValues->a_ulValues[0];

		ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
										"Loading a Model",
										"EdActors_MyDocument::m_fn_pclLoadModel(...)",
										E_ERROR_GRAVITY_FATAL,
										"Model could not be loaded");
		ERROR_ASSERT( pclLoadedModel != NULL );
		
		pclLoadedModel->SetExistingSection(TRUE);

		g_pclInterface->GetInterface()->SetLoadingWorld(bIsLoadingWorld);

		AfxGetApp()->DoWaitCursor(-1);
	}

	g_pclInterface->m_clDocument.m_pLoadedModel = NULL;

	return pclLoadedModel;
}

////////////////////////////////
// Models : Loading functions	//
////////////////////////////////

//*************************************************************************
// Model Loading Function (CallBack)
//*************************************************************************
SCR_tde_Anl_ReturnValue EdActors_MyDocument::m_fn_tdeCallBackLoadModel(	SCR_tdst_File_Description *_p_stFile,
																																				char *_p_szName,
																																				char *_ap_szParams[],
																																				SCR_tde_Anl_Action _eAction)
{
	//Working Model
	static CPA_Actor *s_pclModel = NULL;

	//Info Window
	static unsigned char s_ucProgressValue = 0;
	static CString s_csMessage;
	static BOOL s_bModelAlreadyExists = FALSE;

	CString csAction(_p_szName);

	//For error Infos
	ERROR_SET_SCRIPT_INFO(_p_stFile)

	switch ( _eAction )
	{
	case SCR_EA_Anl_BeginSection:
		{
			CString csName(_p_szName);
			s_csMessage = "Actors Editor loading Model : " + csName;
			
			//Gets the 'context' Model
			unsigned long ulPassedValue;
			SCR_M_RdL0_GetContextLong(0, 0, unsigned long, ulPassedValue);

			if ( ulPassedValue == 0 )
			{
				//Creates the Model
				CPA_SuperObject *pclSO = g_pclInterface->m_clDocument.m_fn_pclCreateModelForLoad(csName);
				s_pclModel = (CPA_Actor *)pclSO->GetObject();

				s_bModelAlreadyExists = FALSE;
			}
			else
			{
				//Creates the Model
				s_pclModel = (CPA_Actor *)ulPassedValue;
				
				CPA_SuperObject *pclSO = g_pclInterface->m_clDocument.m_fn_pclCreateModelForLoad(csName, s_pclModel);
				s_pclModel = (CPA_Actor *)pclSO->GetObject();
				ERROR_ASSERT( (unsigned long)s_pclModel == ulPassedValue );

				s_bModelAlreadyExists = TRUE;
			}

			ERROR_ASSERT( s_pclModel != NULL );
			ERROR_ASSERT( s_pclModel->m_fn_bIsAModel() );
		}
		break;

	case SCR_EA_Anl_EndSection:
		//Adds in the Models list
		g_pclInterface->m_clDocument.m_fn_vAddLoadedModelInList(s_pclModel->m_fn_pclGetEditorModel(), !s_bModelAlreadyExists);

		//Re-builds section name
		s_pclModel->GetCompleteSectionName();

		SCR_M_RdL0_SetSectionLong(0, 0, (long)s_pclModel);
		s_pclModel = NULL;
		
		EDACTORS_fn_vGiveProgressInfo(s_csMessage, 100);

		s_ucProgressValue = 0;
		s_csMessage = "";
		s_bModelAlreadyExists = FALSE;

		break;

	case SCR_EA_Anl_BeginSubSection:
		SCR_M_RdL0_SetContextLong(1, 0, (long)s_pclModel);

		break;

	case SCR_EA_Anl_Entry:
		ERROR_ASSERT ( s_pclModel != NULL );

		//Analyses Model's type
		if ( csAction.CompareNoCase(g_c_csModelType) == 0 )
		{
			s_ucProgressValue += 10;
	 		EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

			s_pclModel->m_fn_vMakeGenDoor(g_c_csModelTypeGenDoor.CompareNoCase(_ap_szParams[0]) == 0);

			//Sets progress
			s_ucProgressValue += 10;
	 		EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);
		}
		//Analyses Model's Bitmap name
		else if ( csAction.CompareNoCase(g_c_csBitmapName) == 0 )
		{
			s_ucProgressValue += 10;
	 		EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

			CString csBitmapName(_ap_szParams[0]);
			
			s_pclModel->m_fn_pclGetEditorModel()->m_fn_vSetBitmapName(csBitmapName);

			//Sets progress
			s_ucProgressValue += 10;
	 		EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);
		}
		//Analyses Model's Family
		else if ( csAction.CompareNoCase(g_c_csActorFamily) == 0 )
		{
			s_ucProgressValue += 10;
	 		EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

			CPA_Family *pclFamily = (CPA_Family *)g_pclInterface->GetMainWorld()->fn_p_oFindObject(	_ap_szParams[0],
																																															C_szFamilyTypeName);

			//If Family is not loaded, calls Family DLL to load it
			ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,"Loading a Model",
										"EdActors_MyDocument::m_fn_tdeCallBackLoadModel(...)",
										E_ERROR_GRAVITY_FATAL,
										"Family for the Model has not been found");
			ERROR_ASSERT( pclFamily != NULL );

			if ( !(pclFamily->fn_bIsAvailable()) )
			{
				CString csFamilyMessage = "Actors Editor asking for Family '" + pclFamily->GetName() + "' load... Please wait !";
	 			EDACTORS_fn_vGiveProgressInfo(csFamilyMessage, -1);

				CPA_DLLBase *p_clFamilyDll = g_pclInterface->GetMainWorld()->GetDLLWithTypeName(C_szDLLFamilyName);

				ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,"Loading a Model",
											"EdActors_MyDocument::m_fn_tdeCallBackLoadModel(...)",
											E_ERROR_GRAVITY_FATAL,
											"Family DLL cannot be found");
				ERROR_ASSERT( p_clFamilyDll != NULL );

				ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,"Loading a Model",
											"EdActors_MyDocument::m_fn_tdeCallBackLoadModel(...)",
											E_ERROR_GRAVITY_FATAL,
											"Family can't be loaded");
				ERROR_VERIFY( p_clFamilyDll->fn_bLoadBaseObject(pclFamily) );
			}

			s_pclModel->m_pub_fn_vSetFamily(pclFamily);
			
			s_pclModel->m_pub_fn_vConstructEnumDescriptorForObjectsTables();

			s_ucProgressValue += 10;
	 		EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);
		}
//-----------------IR-------------------------
		//Analyses Brain
		else
		if(csAction.CompareNoCase(g_c_csBrainName)==0)
		{
			s_ucProgressValue += 10;
	 		EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

			CString csName(_ap_szParams[0]);

			EDAC_fn_vLoadBrain(s_pclModel,csName);

			s_ucProgressValue += 10;
	 		EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);
		}
		//BEGIN ROMTEAM Cristi Petrescu 98-06-
		//Analyses show private variable status
		else
		if(csAction.CompareNoCase(g_c_csShowPrivateVariable)==0)
		{
			CPA_EdIR_Brain *pclBrain = ((EdActors_EditorActorModel *)s_pclModel->m_fn_pclGetEditorActor())->m_pclBrain;
			pclBrain -> m_bShowPrivateVariable = atoi(_ap_szParams[0]);
		}
		//END ROMTEAM Cristi Petrescu 98-06-
//--------------------------------------------
		break;
	}						

	return SCR_ERV_Anl_NormalReturn;
}

//////////////////////////////////////
// MS : Loading functions	//
//////////////////////////////////////

//*************************************************************************
// Level Loading Function (CallBack)
//*************************************************************************
SCR_tde_Anl_ReturnValue EdActors_ActorMiniStructure::m_fn_tdeCallBackLoad(SCR_tdst_File_Description *_p_stFile,
																																					char *_p_szName,
																																					char *_ap_szParams[],
																																					SCR_tde_Anl_Action _eAction)
{
	//Gets the working Model
	long lValue;
	SCR_M_RdL0_GetContextLong(0, 0, long, lValue);
	CPA_Actor	*pclActor = (CPA_Actor *)lValue;
	
	ERROR_ASSERT( pclActor->m_fn_bIsAModel() );

	EdActors_EditorActor *pclEdActor = pclActor->m_fn_pclGetEditorActor();

	static EdActors_ActorMiniStructure *s_pclAMS = NULL;
																		 
	//For error Infos
	ERROR_SET_SCRIPT_INFO(_p_stFile);

	CString csAction(_p_szName);

	switch ( _eAction )
	{
		case SCR_EA_Anl_Entry:
			{
/*				ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
												"Loading a Mini Structure",
												"EdActors_ActorMiniStructure::m_fn_tdeCallBackLoad(...)",
												E_ERROR_GRAVITY_WARNING,
												"A field has been found in a Model's MS which is not allocated");
				ERROR_ASSERT( s_pclAMS != NULL );*/

				if ( s_pclAMS != NULL )
				{
					ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
													"Loading a Mini Structure",
													"EdActors_ActorMiniStructure::m_fn_tdeCallBackLoad(...)",
													E_ERROR_GRAVITY_FATAL,
													"A field has been found in a Model's MS which is not allocated : this should not occur !!");
					ERROR_ASSERT( s_pclAMS->m_fn_bIsAllocated() );
					
					//Searches for the Data
					BOOL bFound = FALSE;
					CTL_Editor_Data *pclData;
					POSITION pos = s_pclAMS->m_pclDataList->GetHeadPosition();
					while ( (pos != NULL) && (!bFound) )
					{
						pclData = s_pclAMS->m_pclDataList->GetNext(pos);
						bFound = ( pclData->m_pub_fn_csGetDataName().CompareNoCase(csAction) == 0 );
					}

					if ( bFound )
					{
						CString csValue(_ap_szParams[0]);

						//Analyses link state
						if ( csValue.CompareNoCase(g_c_csLinkedField) == 0 )
						 OAC_fn_tdeSetLink(pclData, OAC_LINK_MODE__LINKED);
						else if ( csValue.CompareNoCase(g_c_csNotLinkedField) == 0 )
						 OAC_fn_tdeSetLink(pclData, OAC_LINK_MODE__NOT_LINKED);
						else if ( csValue.CompareNoCase(g_c_csAlwaysLinkedField) == 0 )
						 OAC_fn_tdeSetLink(pclData, OAC_LINK_MODE__ALWAYS_LINKED);
						else if ( csValue.CompareNoCase(g_c_csNeverLinkedField) == 0 )
						 OAC_fn_tdeSetLink(pclData, OAC_LINK_MODE__NEVER_LINKED);
						else
						 ERROR_ASSERT(FALSE);

						//Analyses Value string
						CStringList cslStringList;

						// To clean all ema files
						if (csValue.CompareNoCase("Activable") != 0)
						{
							long c_lI;
							for ( c_lI = 1; _ap_szParams[c_lI] != NULL; c_lI ++ )
								cslStringList.AddTail(_ap_szParams[c_lI]);
						}
						else
							cslStringList.AddTail("TRUE");

						pclData->m_fn_vSetValueWithString(&cslStringList);
					}
				}
			}
			break;

		case SCR_EA_Anl_EndSection:
			s_pclAMS = NULL;
			break;

		case SCR_EA_Anl_BeginSection:
			{
				CString csAllocated = CString(_ap_szParams[1]);

				if ( csAllocated.CompareNoCase(g_c_csAllocatedMS) == 0 )
				{
					//Selects the right list of AMS
					EdActors_ActorMiniStructureList *pclAMSList;
					CString csGroupName = CString(_ap_szParams[0]);

					BOOL bAMSListFound = FALSE;
					POSITION ASMListPos = pclEdActor->m_clListOfAMSLists.GetHeadPosition();
					while ( (ASMListPos != NULL) && (!bAMSListFound) )
					{
						pclAMSList = pclEdActor->m_clListOfAMSLists.GetNext(ASMListPos);
						
						bAMSListFound = ( csGroupName.CompareNoCase(pclAMSList->m_fn_csGetGroupScriptName()) == 0 );
					}

					ERROR_ASSERT( bAMSListFound );

					BOOL bAMSFound = FALSE;
					CString csMSName = csAction;
					EdActors_ActorMiniStructure *pclAMS;
					POSITION pos = pclAMSList->GetHeadPosition();
					while ( (pos != NULL)  && (!bAMSFound) )
					{
						pclAMS = pclAMSList->GetNext(pos);
						bAMSFound = ( pclAMS->m_csScriptName.CompareNoCase(csMSName) == 0 );
					}

/*					ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
													"Loading a Mini Structure",
													"EdActors_ActorMiniStructure::m_fn_tdeCallBackLoad(...)",
													E_ERROR_GRAVITY_FATAL,
													"A section is referencing a MS which does not exist in Editor");
					ERROR_ASSERT( bAMSFound );*/

					//Allocates the MS
					if ( bAMSFound )
					{
						pclAMS->m_fn_bAllocate();
						s_pclAMS = pclAMS;
					}
				}
			}
			break;
	}						

	return SCR_ERV_Anl_NormalReturn;
}

