// Implementation for the class of Editor Actors
//////////////////////////////////////////////////
#include "StdAfx.h"

#include "EDACActr.hpp"

#include "EDACInst.hpp"
#include "EDACModl.hpp"
#include "EDACDgQu.hpp"
#include "EDACStrg.hpp"
#include "_Ainterf.hpp"
#include "Defines.hpp"

//-----------------IR-------------------------
#include "EDACQuer.hpp"
//--------------------------------------------

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

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
    #include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//****************************************************************
EdActors_EditorActor::EdActors_EditorActor(tdeCarActorType tdeType)
{
	//For Objetc Tables
	m_pclEnumDescriptorForObjectsTables = new CTL_Editor_EnumDescriptor(g_c_csObjectTablesEnumDescriptorName, 4/*long*/);

	m_tdeType = tdeType;

	m_ptdstActor = NULL;
	m_pclActor = NULL;

	m_fn_vConstructListOfMS();

	//Sets the parent actor for all the MS
	EdActors_ActorMiniStructureList *pclCurrentAMSList;
	POSITION AMSPos = m_clListOfAMSLists.GetHeadPosition();
	POSITION pos;
	while ( AMSPos != NULL )
	{
		pclCurrentAMSList = m_clListOfAMSLists.GetNext(AMSPos);

		pos = pclCurrentAMSList->GetHeadPosition();
		while ( pos != NULL )
			pclCurrentAMSList->GetNext(pos)->m_pclParentActor = this;
	}

	m_ucCurrentMSListIndex = 0;

	//Calls "m_fn_vGetMotorData()" for each data in the Data List


//-----------------IR-------------------------
	m_pclListOfDesignerVariableType = new CTL_Editor_DataList();
	m_pclListOfDesignerVariables=EDAC_fn_pclGetNewDesignerVariableList();
//--------------------------------------------
}

//****************************************************************
EdActors_EditorActor::~EdActors_EditorActor()
{
//-----------------IR-------------------------
	delete m_pclListOfDesignerVariableType;
	EDAC_fn_vDeleteDesignerVariableList(m_pclListOfDesignerVariables);
//--------------------------------------------

	if ( m_pclEnumDescriptorForObjectsTables != NULL )
		delete m_pclEnumDescriptorForObjectsTables;
}

//****************************************************************
CString EdActors_EditorActor::m_fn_csGetActorName()
{
	ERROR_ASSERT( m_pclActor != NULL );
	return m_pclActor->GetName();
}

//****************************************************************
CAR_EDIT_TDSTACTOR *EdActors_EditorActor::m_fn_ptdstGetMotorActor()
{
	return m_ptdstActor;
}

//****************************************************************
void EdActors_EditorActor::m_fn_vSetMotorActor(CAR_EDIT_TDSTACTOR *ptdstActor)
{
	m_ptdstActor = ptdstActor;
}

//**********************************************************************************************
void EdActors_EditorActor::m_fn_vCreateActorMSDataList()
{
	EdActors_ActorMiniStructure *pclActorMS;
	EdActors_ActorMiniStructureList *pclCurrentAMSList;
	POSITION AMSPos = m_clListOfAMSLists.GetHeadPosition();
	POSITION pos;
	while ( AMSPos != NULL )
	{
		pclCurrentAMSList = m_clListOfAMSLists.GetNext(AMSPos);

		pos = pclCurrentAMSList->GetHeadPosition();
		while ( pos != NULL )
		{
			pclActorMS = pclCurrentAMSList->GetNext(pos);

			if ( m_fn_ptdstGetMotorActor() != NULL )
			{
				ERROR_ASSERT( pclActorMS->m_td_pfn_vFillFunction != NULL );
				pclActorMS->m_td_pfn_vFillFunction(this, pclActorMS);
			}
		}
	}
}

//****************************************************************
void EdActors_EditorActor::m_fn_vSetCurrentMSList(EdActors_ActorMiniStructureList *pclActorNewCurrentMSList)
{
	m_pclActorCurrentMSList = pclActorNewCurrentMSList;
}

//****************************************************************
// return value :
//	-1 if not found
//	or the list Index in whicih Data is found
char EdActors_EditorActor::m_fn_cDoesActorOwnData(CTL_Editor_Data *pclDataToSearch)
{
	EdActors_ActorMiniStructure *pclUnusedAMS;

	return m_fn_cDoesActorOwnData(pclDataToSearch, pclUnusedAMS);
}

//****************************************************************
// return value :
//	-1 if not found
//	or the list Index in which Data is found (0,1,...)
char EdActors_EditorActor::m_fn_cDoesActorOwnData(CTL_Editor_Data *pclDataToSearch,
																									EdActors_ActorMiniStructure *&pclAMS)
{
	char cIndexToReturn = -1;
	BOOL bStop = FALSE;

	EdActors_ActorMiniStructureList *pclCurrentAMSList;
	POSITION AMSPos = m_clListOfAMSLists.GetHeadPosition();
	while ( (AMSPos != NULL) && (!bStop) )
	{
		pclCurrentAMSList = m_clListOfAMSLists.GetNext(AMSPos);

		if ( m_fn_bLookForAData(pclCurrentAMSList, pclDataToSearch, pclAMS) )
			bStop = TRUE;

		cIndexToReturn ++;
	}

	return cIndexToReturn;
}

//****************************************************************
BOOL EdActors_EditorActor::m_fn_bLookForAData(EdActors_ActorMiniStructureList *pclActorMSList,
																							CTL_Editor_Data *pclDataToSearch,
																							EdActors_ActorMiniStructure *&pclAMS)
{
	BOOL bFound = FALSE;
	EdActors_ActorMiniStructure *pclCurrentAMS;
	POSITION MSpos = pclActorMSList->GetHeadPosition();
	POSITION DataPos;
	while ( (MSpos != NULL) && !bFound )
	{
		pclCurrentAMS = pclActorMSList->GetNext(MSpos);
		DataPos = pclCurrentAMS->m_pclDataList->GetHeadPosition();
		while ( (DataPos != NULL ) && (!bFound) )
			bFound = ( pclCurrentAMS->m_pclDataList->GetNext(DataPos) == pclDataToSearch);

		if ( bFound )
			pclAMS = pclCurrentAMS;
	}

	return bFound;
}

//****************************************************************
unsigned char EdActors_EditorActor::m_fn_ucGetCurrentMSListIndex()
{
	return m_ucCurrentMSListIndex;
}

//****************************************************************
void EdActors_EditorActor::m_fn_vSetCurrentMSListIndex(unsigned char cNewIndex)
{
	ERROR_ASSERT( (cNewIndex >= 0) && (cNewIndex < m_clListOfAMSLists.GetCount()) );

	m_ucCurrentMSListIndex = cNewIndex;

	m_fn_vSetCurrentMSList(m_clListOfAMSLists.m_fn_pclGetListFromIndex(cNewIndex));
}

//****************************************************************
void EdActors_EditorActor::m_fn_vDeleteAllMS()
{

	EdActors_ActorMiniStructure *pclActorMS;
	EdActors_ActorMiniStructureList *pclCurrentAMSList;
	POSITION AMSPos = m_clListOfAMSLists.GetHeadPosition();
	POSITION pos;
	while ( AMSPos != NULL )
	{
		pclCurrentAMSList = m_clListOfAMSLists.GetNext(AMSPos);

		pos = pclCurrentAMSList->GetHeadPosition();
		while ( pos != NULL )
		{
			pclActorMS = pclCurrentAMSList->GetNext(pos);

			if ( pclActorMS->m_fn_bIsAllocated() )
				pclActorMS->m_fn_bUnallocate();
		}
	}
}

//****************************************************************
//Allocates MS if the correponding MS in the Model is allocated
void EdActors_EditorActor::m_fn_vAllocateMSAsInModel(EdActors_EditorActorModel *pclSourceModel)
{
	ERROR_ASSERT( pclSourceModel != NULL );

	EdActors_ActorMiniStructureList *pclInstanceAMSList;
	EdActors_ActorMiniStructureList *pclModelAMSList;
	POSITION InstAMSPos = m_clListOfAMSLists.GetHeadPosition();
	POSITION ModelAMSPos = pclSourceModel->m_clListOfAMSLists.GetHeadPosition();
	EdActors_ActorMiniStructure *pclInstanceAMS;
	EdActors_ActorMiniStructure *pclModelAMS;
	POSITION InstPos;
	POSITION ModelPos;

	while ( InstAMSPos != NULL )
	{
		ERROR_ASSERT( ModelAMSPos != NULL );

		pclInstanceAMSList = m_clListOfAMSLists.GetNext(InstAMSPos);
		pclModelAMSList = pclSourceModel->m_clListOfAMSLists.GetNext(ModelAMSPos);

		InstPos = pclInstanceAMSList->GetHeadPosition();
		ModelPos = pclModelAMSList->GetHeadPosition();
		while ( InstPos != NULL )
		{
			pclInstanceAMS = pclInstanceAMSList->GetNext(InstPos);
			pclModelAMS = pclModelAMSList->GetNext(ModelPos);

			if ( pclModelAMS->m_fn_bIsAllocated() )
			{
				ERROR_ASSERT ( pclInstanceAMS->m_td_pfn_vAllocationFunction != NULL );

				pclInstanceAMS->m_fn_bAllocate();
			}
		}
	}
}

//****************************************************************
void EdActors_EditorActor::m_fn_vUpdateDatas(EdActors_EditorActor *pclSourceActor,
																						 BOOL _bMustUpdateUnlinkedData /*= TRUE*/,
																						 CTL_tdeUpdateReason eReason,
																						 long _lUserDefinedReason /*= 0*/)
{
	ERROR_ASSERT( pclSourceActor != NULL );

	EdActors_ActorMiniStructureList *pclDestAMSList;
	EdActors_ActorMiniStructureList *pclSourceAMSList;
	POSITION DestAMSPos = m_clListOfAMSLists.GetHeadPosition();
	POSITION SourceAMSPos = pclSourceActor->m_clListOfAMSLists.GetHeadPosition();

	while ( DestAMSPos != NULL )
	{
		ERROR_ASSERT( SourceAMSPos != NULL );

		pclDestAMSList = m_clListOfAMSLists.GetNext(DestAMSPos);
		pclSourceAMSList = pclSourceActor->m_clListOfAMSLists.GetNext(SourceAMSPos);

		ERROR_ASSERT( pclDestAMSList->GetCount() == pclSourceAMSList->GetCount());

		m_fn_vUpdateDataInList(pclDestAMSList, pclSourceAMSList, _bMustUpdateUnlinkedData, eReason, _lUserDefinedReason);
	}
}

//****************************************************************
void EdActors_EditorActor::m_fn_vUpdateDataInList(EdActors_ActorMiniStructureList *pclDestMSList,
																									EdActors_ActorMiniStructureList *pclSourceMSList,
																									BOOL _bMustUpdateUnlinkedData /*= TRUE*/,
																									CTL_tdeUpdateReason eReason,
																									long _lUserDefinedReason /*= 0*/)
{
	EdActors_ActorMiniStructure *pclInstanceCurrentMS;
	EdActors_ActorMiniStructure *pclModelCurrentMS;
	CTL_Editor_DataList *pclInstanceDataList;
 	CTL_Editor_DataList *pclModelDataList;
	POSITION ModelMSPos = pclSourceMSList->GetHeadPosition();
	POSITION InstanceMSPos = pclDestMSList->GetHeadPosition();
	while ( ModelMSPos != NULL )
	{
		pclModelCurrentMS = pclSourceMSList->GetNext(ModelMSPos);
		pclInstanceCurrentMS = pclDestMSList->GetNext(InstanceMSPos);

		if ( pclModelCurrentMS->m_fn_bIsAllocated() )
		{
			ERROR_ASSERT( pclInstanceCurrentMS->m_fn_bIsAllocated() );

			pclInstanceDataList = pclInstanceCurrentMS->m_pclDataList;
			pclModelDataList = pclModelCurrentMS->m_pclDataList;

			ERROR_ASSERT ( pclInstanceDataList->GetCount() ==  pclModelDataList->GetCount() );

			CTL_Editor_Data *pclDataToBeUpdated;
			CTL_Editor_Data *pclSourceData;
			POSITION InstDataPos = pclInstanceDataList->GetHeadPosition();
			POSITION ModelDataPos = pclModelDataList->GetHeadPosition();
			while ( InstDataPos != NULL )
			{
				pclDataToBeUpdated = pclInstanceDataList->GetNext(InstDataPos);
				pclSourceData = pclModelDataList->GetNext(ModelDataPos);

				if	(		( _bMustUpdateUnlinkedData )
						||	( OAC_fn_bIsLinked(pclDataToBeUpdated) )
						)
					pclDataToBeUpdated->m_fn_vUpdateData(pclSourceData, eReason, _lUserDefinedReason);
			}
		}
	}
}

//****************************************************************
tdeCarActorType EdActors_EditorActor::m_fn_tdeGetActorType()
{
	return m_tdeType;
}

//****************************************************************
BOOL EdActors_EditorActor::m_fn_bIsAnInstance()
{
	return (m_tdeType == EDCAR_ACTOR_TYPE_INSTANCE);
}

//****************************************************************
CTL_Editor_EnumDescriptor *EdActors_EditorActor::m_pub_fn_pclGetEnumDescriptorForObjectsTables()
{
	return m_pclEnumDescriptorForObjectsTables;
}

//****************************************************************
void EdActors_EditorActor::m_fn_vSaveActor(SCR_tdst_File_Description *p_stFile)
{

}

//****************************************************************
CTL_Editor_Data *EdActors_EditorActor::m_fn_p_oFindDataByName(CString csName)
{
	BOOL bDataFound = FALSE;
	CTL_Editor_Data *pclFoundData = NULL;

	EdActors_ActorMiniStructure *pclActorMS;
	EdActors_ActorMiniStructureList *pclCurrentAMSList;
	POSITION AMSPos = m_clListOfAMSLists.GetHeadPosition();
	POSITION MSPos;
	POSITION DataPos;
	while ( (AMSPos != NULL) && (!bDataFound) )
	{
		pclCurrentAMSList = m_clListOfAMSLists.GetNext(AMSPos);

		MSPos = pclCurrentAMSList->GetHeadPosition();
		while ( (MSPos != NULL) && (!bDataFound) )
		{
			pclActorMS = pclCurrentAMSList->GetNext(MSPos);

			DataPos = pclActorMS->m_pclDataList->GetHeadPosition();
			while ( (DataPos != NULL) && (!bDataFound) )
			{
				pclFoundData = pclActorMS->m_pclDataList->GetNext(DataPos);
				bDataFound = ( pclFoundData->m_pub_fn_csGetDataName().CompareNoCase(csName) == 0 );
			}
		}
	}

	if ( bDataFound )
		return pclFoundData;
	else
		return NULL;
}

//****************************************************************
CPA_tdoNameList *EdActors_EditorActor::m_fn_p_oGetNameList(CString csName)
{
	CPA_Editor_ObjectListData *p_oData = (CPA_Editor_ObjectListData *) m_fn_p_oFindDataByName(csName);

	return p_oData ? p_oData->m_p_oNameList : NULL;
}

//****************************************************************
EdtList *EdActors_EditorActor::m_fn_p_oGetObjectList(CString csName)
{
	CPA_Editor_ObjectListData *p_oData = (CPA_Editor_ObjectListData *) m_fn_p_oFindDataByName(csName);

	return p_oData ? p_oData->m_p_oObjectList : NULL;
}

//****************************************************************
CPA_ZonesActivatingList *EdActors_EditorActor::m_fn_p_oGetZAList(CString csName)
{
	CPA_Editor_ObjectListData *p_oData = (CPA_Editor_ObjectListData *) m_fn_p_oFindDataByName(csName);

	return p_oData ? p_oData->m_p_oZAList : NULL;
}

//****************************************************************
void EdActors_EditorActor::m_fn_vSetObjectList(CString csName, EdtList *p_oList)
{
	CPA_Editor_ObjectListData *p_oData = (CPA_Editor_ObjectListData *) m_fn_p_oFindDataByName(csName);

	if ( p_oData )
	 p_oData->m_p_oObjectList = p_oList;
}



