#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

#ifdef CPA_USES_LDT
#include "ldt.h"

#define lTagType        'epyT'
#define lTagBitmap      'mtiB'
#define lTagFamily      'imaF'
#define lTagEditorBrain 'tidE'
#define lTagShowPrivate 'wohS'



/*****************************************************************
Function name   : EdActors_MyDocument::fn_iCreateEditorActorModel
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int EdActors_MyDocument::fn_iCreateEditorActorModel( LDT_tdst_Link *pLink )
{
	//Working Model
	CPA_Actor *s_pclModel = NULL;
	
  //For error Infos
  //ERROR_SET_SCRIPT_INFO(_p_stFile)

	//Gets the 'context' Model
	unsigned long ulPassedValue = LDT_GetTempLong(0);
  LDT_SetLinkValue( pLink, ulPassedValue);
  char* szName = LDT_szGetSectionName();

	CString csName(pLink->szName);

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

	}
	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 );

	}


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

  pLink->pObject = (void*)s_pclModel;

  return 0;
}



/*****************************************************************
Function name   : EdActors_MyDocument::fn_iLoadEditorActorModel
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int EdActors_MyDocument::fn_iLoadEditorActorModel( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
	//Working Model
	CPA_Actor *s_pclModel = (CPA_Actor *)pLink->pObject;
	ERROR_ASSERT ( s_pclModel != NULL );
  	
  //Info Window
  CString s_csMessage;
  BOOL s_bModelAlreadyExists = FALSE;
  unsigned char s_ucProgressValue = 0;
  char * szName = LDT_szGetSectionName();

	CString csName(szName);
	s_csMessage = "Actors Editor loading Model : " + csName;

	unsigned long ulPassedValue = LDT_GetLinkValue( pLink);
	if ( ulPassedValue == 0 )
	{
    s_bModelAlreadyExists = FALSE;
  }
  else
  {
    s_bModelAlreadyExists = TRUE;
  }
    
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    switch (*(long*)szEntry)
  	    {

  	      case  lTagType : /*  */
  	      {
			      s_ucProgressValue += 10;
	 		      EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

			      s_pclModel->m_fn_vMakeGenDoor( g_c_csModelTypeGenDoor.CompareNoCase(LDT_szGetParam(1)) == 0);

			      //Sets progress
			      s_ucProgressValue += 10;
	 		      EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

  	      }
  	      break;
  	      case  lTagBitmap : /*  */
  	      {
			      s_ucProgressValue += 10;
	 		      EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

			      CString csBitmapName(LDT_szGetParam(1));
			      
			      s_pclModel->m_fn_pclGetEditorModel()->m_fn_vSetBitmapName(csBitmapName);

			      //Sets progress
			      s_ucProgressValue += 10;
	 		      EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

  	      }
  	      break;
  	      case  lTagFamily : /*  */
  	      {
			      s_ucProgressValue += 10;
	 		      EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

			      CPA_Family *pclFamily = (CPA_Family *)g_pclInterface->GetMainWorld()->fn_p_oFindObject(	LDT_szGetParam(1),
																																															      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);

  	      }
  	      break;
  	      case  lTagEditorBrain : /*  */
  	      {
			      s_ucProgressValue += 10;
	 		      EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

			      CString csName(LDT_szGetParam(1));

            // NOTE: LoadBrain calls dActors_ActorMiniStructure::m_fn_tdeCallBackLoad (the CB for 'CreateEditorIntelligence' section from .eru files)
			      EDAC_fn_vLoadBrain(s_pclModel,csName);

			      s_ucProgressValue += 10;
	 		      EDACTORS_fn_vGiveProgressInfo(s_csMessage, s_ucProgressValue);

  	      }
  	      break;
  	      case  lTagShowPrivate : /*  */
  	      {
			      CPA_EdIR_Brain *pclBrain = ((EdActors_EditorActorModel *)s_pclModel->m_fn_pclGetEditorActor())->m_pclBrain;
			      pclBrain -> m_bShowPrivateVariable = atoi(LDT_szGetParam(1));

  	      }
  	      break;
  	    }
  	  }

      break;
		  case  ParseResult_BeginSection : /* subsection MiniStructure*/
      {
				LDT_LoadSection( NULL );
      }
      break;
  	}
  }

	//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(); //???

// -> OS : THIS CODE SHOULD BE CHANGED 17-Sep-98
//	SCR_M_RdL0_SetSectionLong(0, 0, (long)s_pclModel);
// <- OS : THIS CODE SHOULD BE CHANGED 17-Sep-98

	
	EDACTORS_fn_vGiveProgressInfo(s_csMessage, 100);



  return 0;
}



/*****************************************************************
Function name   : EdActors_ActorMiniStructure::fn_iCreateActorMiniStructure
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int EdActors_ActorMiniStructure::fn_iCreateActorMiniStructure( LDT_tdst_Link *pLink )
{
  CPA_Actor	*pclActor = (CPA_Actor *)pLink->pParent->pObject;
  char* szName= LDT_szGetSectionName();

	CString csAction(szName);

	ERROR_ASSERT( pclActor->m_fn_bIsAModel() );

	EdActors_EditorActor *pclEdActor = pclActor->m_fn_pclGetEditorActor();

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

	CString csAllocated = CString(LDT_szGetParam(2));

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

		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 );
		}

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

  pLink->pObject = (void*)s_pclAMS;

  return 0;
}


/*****************************************************************
Function name   : EdActors_ActorMiniStructure::fn_iLoadActorMiniStructure
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 15-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int EdActors_ActorMiniStructure::fn_iLoadActorMiniStructure( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
  CPA_Actor	*pclActor = (CPA_Actor *)pLink->pParent->pObject;
	ERROR_ASSERT( pclActor->m_fn_bIsAModel() );
	EdActors_EditorActor *pclEdActor = pclActor->m_fn_pclGetEditorActor();
	EdActors_ActorMiniStructure *s_pclAMS = (EdActors_ActorMiniStructure *)pLink->pObject;


  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
        char* szEntry = LDT_szGetEntryName();
        CString csAction(szEntry);
				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(LDT_szGetParam(1));

						//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;
						long c_lI;
        				// To clean all ema files
        				if (csValue.CompareNoCase("Activable") != 0)
						{

                            long lNbP = LDT_iGetNbParams();
						    for ( c_lI = 2; c_lI <= (lNbP-1); c_lI ++ )
                            {
							    cslStringList.AddTail(LDT_szGetParam(c_lI));
                            }   
						}
						else
							cslStringList.AddTail("TRUE");

						pclData->m_fn_vSetValueWithString(&cslStringList);
					}
				}

  	  }
  	}
  }




  return 0;
}

#endif //CPA_USES_LDT
