// Classes for the Editor tree
//
// YB
//////////////////////////////////////////////

//BEGIN ROMTEAM Cristi Petrescu 98-05-
#include "StdAfx.h"
#define HieFriend

#include "IADEdttr.hpp"

#include "IADEdtTr.hpp"
#include "IADLnkMt.hpp"
#include "_IADItrf.hpp"
#include "IADVwRst.hpp"
#include "IAD_Res.h"
#include "x:/cpa/tempgrp/tia/inc/ai_intf.hpp"
#include "x:/cpa/main/inc/_EditId.h"

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

//END ROMTEAM Cristi Petrescu 98-05-

//*********************************************************************
//Constructor
// BEGIN ROMTEAM Cristi Petrescu 98-06-
IAD_EditorTreeNode::IAD_EditorTreeNode(enum AIDebug_tdeSectionType_ _tdeNodeType,
																			 tdstNodeInterpret *_p_stNode,
																			 CPA_Actor *_pclParentActor,
																			 IAD_EditorTreeNode *_pclParentNode,
																			 short _wDepth,
																			 CString _csNodeType,
																			 CString _csNodeName /*= ""*/,
																			 CString _csValue /*= ""*/,
																			 CString _csReturnValue /*= ""*/,
																			 CString _csNodeAdditionnalInfo /*= ""*/,
																			 BOOL bSubtreeBuilded /*= TRUE*/,
																			 unsigned short uwSubtreeTraceIndex /*= -1*/)
																			 // END ROMTEAM Cristi Petrescu 98-06-
{
	m_pro_tdeNodeType = _tdeNodeType;
	m_p_stMotorNode = _p_stNode;
	m_pri_pclParentActor = _pclParentActor;

	m_pro_pclParentNode = _pclParentNode;
	m_pro_wDepth = _wDepth;
	m_pro_csNodeType = _csNodeType;
	m_pro_csNodeName = _csNodeName;
	m_pro_csReturnValue = _csReturnValue;
	m_pro_csValue = _csValue;

	m_pro_csNodeAdditionnalInfo = _csNodeAdditionnalInfo;

	m_pro_csCompleteString = m_pri_fn_csComputeTotalName();

	m_pro_hTreeItem = NULL;

	// BEGIN ROMTEAM Cristi Petrescu 98-06-
	m_bSubtreeBuilded = bSubtreeBuilded;
	m_uwSubtreeTraceIndex = uwSubtreeTraceIndex;

	m_lNumberOfVariableValues = 0;
	m_a_tdstArrayOfVariableValues = NULL;
	// END ROMTEAM Cristi Petrescu 98-06-
}

//*********************************************************************
//Empty Constructor for Root
IAD_EditorTreeNode::IAD_EditorTreeNode()
{
	// CG PB DEBUGGER 23/06/98 {
	#ifdef ACTIVE_AIDEBUG
		m_pro_tdeNodeType = (enum AIDebug_tdeSectionType_)AI_DEBUGGER_CONSTANT_FOR_TREE_ROOT_ID;
	#endif // CG PB DEBUGGER }

	m_pro_pclParentNode = NULL;
	m_pro_wDepth = IAD_NODE_DEPTH_UNKNOWN;
	m_pro_csNodeType = "** Root **";
	m_pro_csNodeAdditionnalInfo = "";

	// BEGIN ROMTEAM Cristi Petrescu 98-06-
	m_bSubtreeBuilded = TRUE;
	m_uwSubtreeTraceIndex = -1;

	m_lNumberOfVariableValues = 0;
	m_a_tdstArrayOfVariableValues = NULL;
	// END ROMTEAM Cristi Petrescu 98-06-
}

//*********************************************************************
//Destructor
IAD_EditorTreeNode::~IAD_EditorTreeNode()
{
	// BEGIN ROMTEAM Cristi Petrescu 98-06-
	if (m_a_tdstArrayOfVariableValues != NULL)
		free (m_a_tdstArrayOfVariableValues);
	// END ROMTEAM Cristi Petrescu 98-06-
}

//////////////////////
// Public Functions	//
//////////////////////

//Get/Set functions
////////////////////

//*********************************************************************
IAD_tdListOfNodes *IAD_EditorTreeNode::m_pub_fn_ptdGetListOfChildNodes()
{
	return &m_pro_clListOfChildNodes;
}

//BEGIN ROMTEAM Cristi Petrescu 98-05-
//*********************************************************************
void IAD_EditorTreeNode::m_fn_vSetListOfChildNodes (IAD_tdListOfNodes *pclListOfChildNodes)
{
	// cleaning
	IAD_EditorTreeNode *pclWasteNode;
	POSITION pos = m_pro_clListOfChildNodes . GetHeadPosition ();
	while (pos != NULL)
	{
		pclWasteNode = m_pro_clListOfChildNodes . GetNext (pos);
		// BEGIN ROMTEAM Cristi Petrescu 98-06-
		if (! pclListOfChildNodes -> Find (pclWasteNode))
			delete pclWasteNode;
		// END ROMTEAM Cristi Petrescu 98-06-
	}
	m_pro_clListOfChildNodes . RemoveAll ();

	// add the new ones
	pos = pclListOfChildNodes -> GetHeadPosition ();
	while (pos != NULL)
	{
		m_pro_clListOfChildNodes . AddTail (pclListOfChildNodes -> GetNext (pos));
	}
}
//END ROMTEAM Cristi Petrescu 98-05-

//*********************************************************************
IAD_EditorTreeNode *IAD_EditorTreeNode::m_pub_fn_pclGetParentNode()
{
	return m_pro_pclParentNode;
}

//BEGIN ROMTEAM Cristi Petrescu 98-05-
//*********************************************************************
IAD_EditorTreeNode *IAD_EditorTreeNode::m_fn_pclGetBehaviourParentNode()
{
// CG PB DEBUGGER 23/06/98 {
#ifdef ACTIVE_AIDEBUG

	IAD_EditorTreeNode *pclBehaviourNode = this;

	while (pclBehaviourNode && pclBehaviourNode -> m_pub_fn_tdeGetNodeType () != AIDebug_E_SecTyp_ReflexOrAI)
	{
		pclBehaviourNode = pclBehaviourNode -> m_pub_fn_pclGetParentNode ();
	}

	return pclBehaviourNode;
	
#else
	return NULL;
#endif 
// CG PB DEBUGGER
}

//*********************************************************************
void IAD_EditorTreeNode::m_fn_vAddExecutedNodes (void)
{
// CG PB DEBUGGER 23/06/98 {
#ifdef ACTIVE_AIDEBUG
	// for me
	if (m_pro_tdeNodeType != AIDebug_E_SecTyp_ReflexOrAI)
	{
		m_pri_pclParentActor -> m_fn_vSetOneExecutedNode (m_p_stMotorNode);
	}
#endif 
// CG PB DEBUGGER

	// and for all the childs
	IAD_EditorTreeNode *pclChildNode;

	POSITION pos = m_pro_clListOfChildNodes . GetHeadPosition ();
	while (pos != NULL)
	{
		pclChildNode = m_pro_clListOfChildNodes . GetNext (pos);
		pclChildNode -> m_fn_vAddExecutedNodes ();
	}
}
//END ROMTEAM Cristi Petrescu 98-05-

// BEGIN ROMTEAM Cristi Petrescu 98-06-
//*********************************************************************
IAD_EditorTreeNode *IAD_EditorTreeNode::m_fn_pclGetEngineLoopParentNode()
{
	IAD_EditorTreeNode *pclBehaviourNode = this;

	while (pclBehaviourNode && pclBehaviourNode -> m_pub_fn_tdeGetNodeType () != AIDebug_E_SecTyp_EngineLoop)
	{
		pclBehaviourNode = pclBehaviourNode -> m_pub_fn_pclGetParentNode ();
	}

	ASSERT (pclBehaviourNode);
	return pclBehaviourNode;
}
// END ROMTEAM Cristi Petrescu 98-06-

//*********************************************************************
tdstNodeInterpret *IAD_EditorTreeNode::m_pub_fn_p_stGetMotorNode()
{
	return m_p_stMotorNode;
}

//*********************************************************************
CPA_Actor *IAD_EditorTreeNode::m_pub_fn_pclGetParentActor()
{
	return m_pri_pclParentActor;
}

//*********************************************************************
short IAD_EditorTreeNode::m_pub_fn_wGetDepth()
{
	return m_pro_wDepth;
}

//*********************************************************************
HTREEITEM IAD_EditorTreeNode::m_pub_fn_hGetTreeItem()
{
	return m_pro_hTreeItem;
}

//*********************************************************************
void IAD_EditorTreeNode::m_pub_fn_vSetTreeItem(HTREEITEM _hTreeItem)
{
	m_pro_hTreeItem = _hTreeItem;
}

//*********************************************************************
enum AIDebug_tdeSectionType_ IAD_EditorTreeNode::m_pub_fn_tdeGetNodeType()
{
	return m_pro_tdeNodeType;
}

//*********************************************************************
CString IAD_EditorTreeNode::m_pub_fn_csGetNodeType()
{
	return m_pro_csNodeType;
}

//*********************************************************************
CString IAD_EditorTreeNode::m_pub_fn_csGetNodeName()
{
	return m_pro_csNodeName;
}

//*********************************************************************
CString IAD_EditorTreeNode::m_pub_fn_csGetNodeValue()
{
	return m_pro_csValue;
}

//*********************************************************************
CString IAD_EditorTreeNode::m_pub_fn_csGetNodeReturnValue()
{
	return m_pro_csReturnValue;
}

//*********************************************************************
CString IAD_EditorTreeNode::m_pub_fn_csGetNodeAdditionnalInfo()
{
	return m_pro_csNodeAdditionnalInfo;
}

//*********************************************************************
void IAD_EditorTreeNode::m_pub_fn_vSetNodeAdditionnalInfo(CString _csNewNodeInfo)
{
	m_pro_csNodeAdditionnalInfo = _csNewNodeInfo;
}

//*********************************************************************
CString IAD_EditorTreeNode::m_pub_fn_csGetCompleteString()
{
	return m_pro_csCompleteString;
}


//Tree construction
////////////////////

//*********************************************************************
IAD_EditorTreeNode *IAD_EditorTreeNode::m_pub_fn_pclAddChildNode(enum AIDebug_tdeSectionType_ _tdeNodeType,
																																 tdstNodeInterpret *_p_stNode,
																																 CPA_Actor *_pclParentActor,
																																 CString _csNewElementType,
																																 CString _csNewElementName,
																																 CString _csNewElementValue,
																																 CString _csNewElemenReturnValue,
																																 CString _csNodeAdditionnalInfo)
{
	IAD_EditorTreeNode *pclNewNode = new IAD_EditorTreeNode(_tdeNodeType,
																													_p_stNode,
																													_pclParentActor,
																													this, 
																													m_pub_fn_wGetDepth() + 1,
																													_csNewElementType,
																													_csNewElementName,
																													_csNewElementValue,
																													_csNewElemenReturnValue,
																													_csNodeAdditionnalInfo);
	//Adds to internal list
	m_pro_clListOfChildNodes.AddTail(pclNewNode);

	return pclNewNode;
}

// BEGIN ROMTEAM Cristi Petrescu 98-06-
// Description: this function add a new variable value to a given node
// Note: it uses dinamic reallocation of the array of values
//*********************************************************************
#define BLOCK_INCREMENT 20
void IAD_EditorTreeNode :: m_fn_vAddVariableValue (tdstGetSetParam *p_tdstValue)
{
	// realloc
	if (m_lNumberOfVariableValues % BLOCK_INCREMENT == 0)
		m_a_tdstArrayOfVariableValues = (tdstGetSetParam *) realloc (m_a_tdstArrayOfVariableValues, (m_lNumberOfVariableValues + BLOCK_INCREMENT) * sizeof (tdstGetSetParam));

	if (m_a_tdstArrayOfVariableValues)
		m_a_tdstArrayOfVariableValues [m_lNumberOfVariableValues ++] = * p_tdstValue;
}
#undef BLOCK_INCREMENT

//*********************************************************************
tdstGetSetParam * IAD_EditorTreeNode :: m_fn_p_tdstGetVariableValue (long lIndex)
{
	if (lIndex >= 0 && lIndex < m_lNumberOfVariableValues)
		return & m_a_tdstArrayOfVariableValues [lIndex];
	else
		return NULL;
}
// END ROMTEAM Cristi Petrescu 98-06-

////////////////////////
// Private Functions	//
////////////////////////

//*********************************************************************
CString IAD_EditorTreeNode::m_pri_fn_csComputeTotalName()
{
	CString csTotalName = m_pro_csNodeType;

	if ( !m_pro_csNodeName.IsEmpty() )
		csTotalName += CString(" ") + m_pro_csNodeName;
	if ( !m_pro_csValue.IsEmpty() )
		csTotalName += CString(" = ") + m_pro_csValue;
	if ( !m_pro_csReturnValue.IsEmpty() )
		csTotalName += CString("   [ returns ") + m_pro_csReturnValue + " ]";

	return csTotalName;
}

