// Implementation for the class CMyDocument
/////////////////////////////////////////////
#include "StdAfx.h"
#define HieFriend

#include "SCR.h"

#include "IADDoc.hpp"

#include "IADVwMn.hpp"
#include "IADVwRst.hpp"
#include "_IADItrf.hpp"
#include "IADDgStp.hpp"
#include "IADLnkMt.hpp"

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

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

long g_lCurrentId = 2000;

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

//****************************************************************************
IAD_MyDocument::IAD_MyDocument()
{
//	AfxEnableMemoryTracking(TRUE);

	//Initializes editor's state
	
	m_pclControlView = NULL;
	m_pclResultView = NULL;
	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	m_pclVariableView = NULL;
	//END ROMTEAM Cristi Petrescu 98-05-

	m_pri_lCurrentIndexInFoundItemsList = -1;

	m_pri_p_stSelectedActor = NULL;

	m_pri_tdstFindInfo.bAllLevels = TRUE;
	m_pri_tdstFindInfo.lBeginningLevel = 0;
	m_pri_tdstFindInfo.lEndLevel = 10;

	m_pri_bDebuggerIsOn = TRUE;

	m_pub_bIsDeletingTree = FALSE;

	//Editor's Setup var. Init.
	//...

	IAD_g_fn_vInitGetStringCallBacks();

	//Init. the INI file name
	m_csIniFileName = M_GetMainApp()->m_csEditorDataPath + "Tools\\IA_Debug\\IA_Debug.ini";

	//Reads INI file
	m_fn_bReadIniFile();

	//PROVISOIRE...
	m_csHelpFileNameAndPath = M_GetMainApp()->m_csEditorDataPath + "Tools\\IA_Debug\\IADebug.hlp >Normale";
}

//****************************************************************************
IAD_MyDocument::~IAD_MyDocument()
{
//	m_fn_bWriteIniFile();
}


//****************************************************************************
HTREEITEM IAD_MyDocument::m_pub_fn_hFindInTree(IAD_EditorTreeNode *_pclRoot,
																							 TID_tdstFindInfo *_p_tdstFindInfo)
{
	//Inits recursive search
	m_pri_lCurrentIndexInFoundItemsList = 0;
	m_pri_clListOfFoundNodes.RemoveAll();
	m_pri_fn_vFindInOneLevelOfTree(_pclRoot, _p_tdstFindInfo);

	if ( m_pri_clListOfFoundNodes.GetCount() > 0 )
	{
		HTREEITEM hFirstItem = m_pri_clListOfFoundNodes.GetHead()->m_pub_fn_hGetTreeItem();
		
		m_pclResultView->m_fn_vEnableFindPrevButton(FALSE);
		m_pclResultView->m_fn_vEnableFindNextButton( m_pri_clListOfFoundNodes.GetCount() > 1 );
		
		CString csStatusText;
		csStatusText.Format("Finder has selected occurence %ld on %ld",	m_pri_lCurrentIndexInFoundItemsList + 1,
																																		m_pri_clListOfFoundNodes.GetCount());
		m_pclResultView->m_pub_fn_vSetFindStatusText(csStatusText);
		
		return hFirstItem;
	}
	else
	{
		m_pclResultView->m_pub_fn_vSetFindStatusText("No occurence found in Tree");

		m_pclResultView->m_fn_vEnableFindNextButton(FALSE);
		m_pclResultView->m_fn_vEnableFindPrevButton(FALSE);
		return NULL;
	}
}

//****************************************************************************
HTREEITEM IAD_MyDocument::m_pub_fn_hFindNextInTree()
{
 	m_pri_lCurrentIndexInFoundItemsList ++;
	
	POSITION pos = m_pri_clListOfFoundNodes.FindIndex(m_pri_lCurrentIndexInFoundItemsList);
	HTREEITEM hNextItem = m_pri_clListOfFoundNodes.GetAt(pos)->m_pub_fn_hGetTreeItem();

	m_pclResultView->m_fn_vEnableFindNextButton( m_pri_lCurrentIndexInFoundItemsList < (m_pri_clListOfFoundNodes.GetCount() - 1) );
	m_pclResultView->m_fn_vEnableFindPrevButton(TRUE);

	CString csStatusText;
	csStatusText.Format("Finder has selected occurence %ld on %ld",	m_pri_lCurrentIndexInFoundItemsList + 1,
																																	m_pri_clListOfFoundNodes.GetCount());
	m_pclResultView->m_pub_fn_vSetFindStatusText(csStatusText);

	return hNextItem;
}

//****************************************************************************
HTREEITEM IAD_MyDocument::m_pub_fn_hFindPreviousInTree()
{
 	m_pri_lCurrentIndexInFoundItemsList --;
	
	POSITION pos = m_pri_clListOfFoundNodes.FindIndex(m_pri_lCurrentIndexInFoundItemsList);
	
	HTREEITEM hNextItem = m_pri_clListOfFoundNodes.GetPrev(pos)->m_pub_fn_hGetTreeItem();

	m_pclResultView->m_fn_vEnableFindNextButton(TRUE);
	m_pclResultView->m_fn_vEnableFindPrevButton( m_pri_lCurrentIndexInFoundItemsList > 0 );

	CString csStatusText;
	csStatusText.Format("Finder has selected occurence %ld on %ld",	m_pri_lCurrentIndexInFoundItemsList + 1,
																																	m_pri_clListOfFoundNodes.GetCount());
	m_pclResultView->m_pub_fn_vSetFindStatusText(csStatusText);

	return hNextItem;
}

//****************************************************************************
TID_tdstFindInfo *IAD_MyDocument::m_pub_p_tdstGetFindInfo()
{
	return &m_pri_tdstFindInfo;
}

//****************************************************************************
struct IAD_stInternalActorDescription *IAD_MyDocument::m_pub_fn_p_stGetSelectedActor()
{
	return m_pri_p_stSelectedActor;
}

//****************************************************************************
void IAD_MyDocument::m_pub_fn_vSetSelectedActor(struct IAD_stInternalActorDescription *_p_stNewSelectedActor)
{
	m_pri_p_stSelectedActor = _p_stNewSelectedActor;
}

//****************************************************************************
void IAD_MyDocument::m_pub_fn_vFreeEditorTraceForActor(struct IAD_stInternalActorDescription *_p_stActorToFree)
{
	if ( _p_stActorToFree != NULL )
	{
		if ( _p_stActorToFree->pclRoot != NULL )
		{
			//Frees all nodes
			m_pri_fn_vFreeNodes(_p_stActorToFree->pclRoot);

			_p_stActorToFree->pclRoot = NULL;
		}
	}
}

//****************************************************************************
void IAD_MyDocument::m_pub_fn_vFreeEditorTraceForEditedActor()
{
	m_pub_fn_vFreeEditorTraceForActor(m_pri_p_stSelectedActor);
	
	m_pri_p_stSelectedActor = NULL;

	//Resets 'Find' list
	m_pri_clListOfFoundNodes.RemoveAll();
	m_pclResultView->m_pub_fn_vSetFindStatusText("Nothing has been searched");

	//Resets Tree Control
	m_pclResultView->m_pub_fn_vDeleteAllInTreeControl();
	m_pclResultView->m_pub_fn_vSetStatusText("Ready !");
	m_pclResultView->m_pub_fn_vSetEditedActorName("<< Nothing edited >>");

	m_pclControlView->m_pub_fn_vUnselectAllItems();
}

//****************************************************************************
void IAD_MyDocument::m_pub_fn_vReInitEditorTraceForActors()
{
	IAD_td_stInternalActorDescription *p_stCurrentActorDesc;
	POSITION pos = m_pclControlView->m_pri_clListOfActors.GetHeadPosition();
	while ( pos != NULL )
	{
		p_stCurrentActorDesc = m_pclControlView->m_pri_clListOfActors.GetNext(pos);

		if ( p_stCurrentActorDesc->bTraceIsEnabled )
		{
			m_pub_fn_vFreeEditorTraceForActor(p_stCurrentActorDesc);

			if ( M_GetMainApp()->m_bAutoReinitTheMap )
			{
				//Calls motor re-init
				AI_tdstMind *p_stMind = M_pstGetMindOfBrain( ((tdstEngineObject *)p_stCurrentActorDesc->pclActor->GetStruct())->h_Brain );
				ERROR_ASSERT( p_stMind != NULL );

				// CG PB DEBUGGER 23/06/98 {
				#ifdef ACTIVE_AIDEBUG
					AIDebug_fn_vReinitTrace(p_stMind);
				#endif // CG PB DEBUGGER }
			}
		}
	}

	//Resets display in Editor
	m_pri_p_stSelectedActor = NULL;

	//Resets 'Find' list
	m_pri_clListOfFoundNodes.RemoveAll();
	m_pclResultView->m_pub_fn_vSetFindStatusText("Nothing has been searched");

	//Resets Tree Control
	m_pclResultView->m_pub_fn_vDeleteAllInTreeControl();
	m_pclResultView->m_pub_fn_vSetStatusText("Ready !");
	m_pclResultView->m_pub_fn_vSetEditedActorName("<< Nothing edited >>");

	m_pclControlView->m_pub_fn_vUnselectAllItems();
}

//****************************************************************************
BOOL IAD_MyDocument::m_pub_fn_bIsDebuggerOn()
{
	return m_pri_bDebuggerIsOn;
}

//****************************************************************************
void IAD_MyDocument::m_pub_fn_vSetDebuggerOn(BOOL _bDebuggerOn /*= TRUE*/)
{
	m_pri_bDebuggerIsOn = _bDebuggerOn;
}

	///////////////////////
	// Private functions //
	///////////////////////

//****************************************************************************
void IAD_MyDocument::m_pri_fn_vFindInOneLevelOfTree(IAD_EditorTreeNode *_pclParentNode,
																										TID_tdstFindInfo *_p_tdstFindInfo)
{
	IAD_EditorTreeNode *pclCurrentChildNode;
	BOOL bMustGoOn = TRUE;
	BOOL bMustAdd = TRUE;
	POSITION pos = _pclParentNode->m_pub_fn_ptdGetListOfChildNodes()->GetHeadPosition();
	while ( (pos != NULL) && (bMustGoOn) )
	{
		pclCurrentChildNode	= _pclParentNode->m_pub_fn_ptdGetListOfChildNodes()->GetNext(pos);
 
		if ( !_p_tdstFindInfo->bAllLevels )
		{
			bMustGoOn = ( pclCurrentChildNode->m_pub_fn_wGetDepth() <= _p_tdstFindInfo->lEndLevel );
			bMustAdd = ( pclCurrentChildNode->m_pub_fn_wGetDepth() >= _p_tdstFindInfo->lBeginningLevel );
		}

		if ( bMustGoOn )
		{
			//Analyses current Node
			if ( bMustAdd )
			{
				if ( m_pri_fn_bNodeRespectsFindCriteria(pclCurrentChildNode, _p_tdstFindInfo) )
					m_pri_clListOfFoundNodes.AddTail(pclCurrentChildNode);
			}

			//Looks in childs
			m_pri_fn_vFindInOneLevelOfTree(pclCurrentChildNode, _p_tdstFindInfo);
		}
	}
}

//****************************************************************************
BOOL IAD_MyDocument::m_pri_fn_bNodeRespectsFindCriteria(IAD_EditorTreeNode *_pclNode,
																												TID_tdstFindInfo *_p_tdstFindInfo)
{
	BOOL bCriteriaRespected;

	if ( _p_tdstFindInfo->bSimpleSearch )
	{
		bCriteriaRespected = m_pri_fn_bCompareStringsWithCriteria(_pclNode->m_pub_fn_csGetCompleteString(),
																															_p_tdstFindInfo->csSimpleSearchString,
																															_p_tdstFindInfo->bSimpleSearch_CaseSensitive,
																															_p_tdstFindInfo->bSimpleSearch_Exact);
	}
	else
	{
		//Analyses type
		bCriteriaRespected = ( _pclNode->m_pub_fn_tdeGetNodeType() == _p_tdstFindInfo->eNodeType );

		if ( bCriteriaRespected )
		{
			if ( !_p_tdstFindInfo->csName.IsEmpty() )
				bCriteriaRespected = m_pri_fn_bCompareStringsWithCriteria(_pclNode->m_pub_fn_csGetNodeName(),
																																	_p_tdstFindInfo->csName,
																																	_p_tdstFindInfo->bName_CaseSensitive,
																																	_p_tdstFindInfo->bName_Exact);
		}

		if ( bCriteriaRespected )
		{
			if ( !_p_tdstFindInfo->csValue.IsEmpty() )
				bCriteriaRespected = m_pri_fn_bCompareStringsWithCriteria(_pclNode->m_pub_fn_csGetNodeValue(),
																																	_p_tdstFindInfo->csValue,	
																																	_p_tdstFindInfo->bValue_CaseSensitive,
																																	_p_tdstFindInfo->bValue_Exact);
		}

		if ( bCriteriaRespected )
		{
			if ( !_p_tdstFindInfo->csRetValue.IsEmpty() )
				bCriteriaRespected = m_pri_fn_bCompareStringsWithCriteria(_pclNode->m_pub_fn_csGetNodeReturnValue(),
																																	_p_tdstFindInfo->csRetValue,
																																	_p_tdstFindInfo->bRetValue_CaseSensitive,
																																	_p_tdstFindInfo->bRetValue_Exact);
		}
	}

	return bCriteriaRespected;
}

//****************************************************************************
BOOL IAD_MyDocument::m_pri_fn_bCompareStringsWithCriteria(CString _csString1,
																													CString _csString2,
																													BOOL _bMatchCase,
																													BOOL _bExact)
{
	BOOL bStringsRespectCriteria = FALSE;

	CString csTempStr1 = _csString1;
	CString csTempStr2 = _csString2;

	if ( !_bMatchCase )
	{
		csTempStr1.MakeLower();
		csTempStr2.MakeLower();
	}

	if ( _bExact )
		bStringsRespectCriteria = ( csTempStr1.Compare(csTempStr2) == 0 );
	else
		bStringsRespectCriteria = ( csTempStr1.Find(csTempStr2) != -1 );

	return bStringsRespectCriteria;
}

//****************************************************************************
void IAD_MyDocument::m_pub_fn_vDisplaySetup()
{
	TID_Dialog_Setup SetupDial(AfxGetMainWnd());

	SetupDial.DoModal();

	//Saves INI file
	m_fn_bWriteIniFile();

	//Refreshes Tree
	m_pclResultView->m_pub_fn_vRefreshTree();
}

//****************************************************************************
void IAD_MyDocument::m_pri_fn_vFreeNodes(IAD_EditorTreeNode *_pclParentNode)
{
	IAD_EditorTreeNode *pclCurrentChildNode;
	POSITION pos = _pclParentNode->m_pub_fn_ptdGetListOfChildNodes()->GetHeadPosition();
	while ( pos != NULL )
	{
		pclCurrentChildNode = _pclParentNode->m_pub_fn_ptdGetListOfChildNodes()->GetNext(pos);

		//Deletes childs of current child
		m_pri_fn_vFreeNodes(pclCurrentChildNode);

		//Deletes current childs
		delete pclCurrentChildNode;
	}
}

//****************************************************************************
struct AI_tdstMind_ *IAD_MyDocument::m_pub_fn_p_stGetMindOfActor(IAD_td_stInternalActorDescription *_p_stInternalActor)
{
	return M_pstGetMindOfBrain( ((tdstEngineObject *)_p_stInternalActor->pclActor->GetStruct())->h_Brain );
}
