// EdIRBeEn.cpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRBeEn.hpp"

#include "EdIRBvw.hpp"
#include "EdIRBeha.hpp"
#include "EdIRFrm.hpp"
#include "EdIRIRD.hpp"
#include "EdIRStrg.hpp"

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

#include "ai_intf.hpp"	//for g_pclAIInterface

#define C_BEHAVIOUR_TITLE_LINE			"/************************************************************/\n"
#define C_BEHAVIOUR_TITLE_TEMPLATE	"/*                                                          */\n"

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

//////////////////////////////////////////////////////////////////
// class CPA_EdIR_MainBehaviour
//////////////////////////////////////////////////////////////////

/****************************************************************************/
CPA_EdIR_MainBehaviour::CPA_EdIR_MainBehaviour(CPA_Actor *pclActor)
:CPA_BaseObject(g_pclAIInterface,C_szIABehaviourTypeName,(CPA_BaseObject *)pclActor)
{
	//m_csName						="";
	m_crPosition				=CRect(0,0,0,0);
	m_csText						="";
	m_pclBehaviour			=NULL;
	m_bHasBeenModified	=FALSE;

	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	m_clDebugInfoList . RemoveAll ();
	//END ROMTEAM Cristi Petrescu 98-05-
}

/****************************************************************************/
CPA_EdIR_MainBehaviour::CPA_EdIR_MainBehaviour(CPA_Actor *pclActor,CString csName,CRect crPosition,CString csText,CPA_EdIR_Behaviour *pclBehaviour)
:CPA_BaseObject(g_pclAIInterface,C_szIABehaviourTypeName,(CPA_BaseObject *)pclActor)
{
	//m_csName=csName;
	if(fn_eRename(csName)!=E_mc_None)
		SetDefaultUniqueName();

	m_crPosition=crPosition;
	m_csText=csText;
	m_pclBehaviour=pclBehaviour;

	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	m_clDebugInfoList .RemoveAll ();
	//END ROMTEAM Cristi Petrescu 98-05-
}

/****************************************************************************/
CPA_EdIR_MainBehaviour::CPA_EdIR_MainBehaviour(CPA_Actor *pclActor,CPA_EdIR_MainBehaviour &clMainBehaviour)
:CPA_BaseObject(g_pclAIInterface,C_szIABehaviourTypeName,(CPA_BaseObject *)pclActor)
{
	if(clMainBehaviour.m_pclBehaviour!=NULL)
	{
		//In order to skip prefix checking
		BOOL	bIsLoadingWorld=g_pclAIInterface->GetInterface()->fn_bIsLoadingWorld();
		g_pclAIInterface->GetInterface()->SetLoadingWorld(TRUE);

		//m_csName=clMainBehaviour.m_pclBehaviour->m_fn_csGetName();
		if(fn_eRename(clMainBehaviour.m_pclBehaviour->m_fn_csGetName())!=E_mc_None)
			SetDefaultUniqueName();

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

		m_crPosition=clMainBehaviour.m_pclBehaviour->m_fn_crGetBoundingBox();
		m_csText=clMainBehaviour.m_pclBehaviour->m_fn_csGetRules();
		m_pclBehaviour=NULL;
	}
	else
	{
		//In order to skip prefix checking
		BOOL	bIsLoadingWorld=g_pclAIInterface->GetInterface()->fn_bIsLoadingWorld();
		g_pclAIInterface->GetInterface()->SetLoadingWorld(TRUE);

		if(fn_eRename(clMainBehaviour.GetName())!=E_mc_None)
			SetDefaultUniqueName();

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

		m_crPosition=clMainBehaviour.m_crPosition;
		m_csText=clMainBehaviour.m_csText;
		m_pclBehaviour=NULL;
	}

	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	m_clDebugInfoList . RemoveAll ();
	//END ROMTEAM Cristi Petrescu 98-05-
}

/****************************************************************************/
CPA_EdIR_MainBehaviour::~CPA_EdIR_MainBehaviour()
{
	delete m_pclBehaviour;

	//BEGIN ROMTEAM Cristi Petrescu 98-05-
	m_clDebugInfoList . RemoveAll ();
	//END ROMTEAM Cristi Petrescu 98-05-
}

/****************************************************************************/
long CPA_EdIR_MainBehaviour::GetDataType(void)
{
	return 0;
}

/****************************************************************************/
void *CPA_EdIR_MainBehaviour::GetData(void)
{
	return NULL;
}

/****************************************************************************/
tdeMissingCriteria CPA_EdIR_MainBehaviour::fn_eCheckUnicity(const CString csName)
{
	if(csName==g_c_csDsgVarName ||
		 csName==g_c_csMacroName)
	{
		return E_mc_None;
	}
	else
	{
		return fn_eCheckUnicityWithOwner(csName);
	}
}

/****************************************************************************/
void CPA_EdIR_MainBehaviour::m_fn_vUpdateBehaviour(CPA_EdIR_DiagView *pclDiagView,BOOL bDeclarationMode)
{
	if(m_pclBehaviour==NULL)
	{
		m_pclBehaviour=pclDiagView->m_fn_pclAddOneBehaviour(GetName(),m_crPosition,m_csText,bDeclarationMode,0);
		m_pclBehaviour->m_fn_vSetMainBehaviour(this);
	}
}

/****************************************************************************/
CString	CPA_EdIR_MainBehaviour::m_fn_csGetBehaviourTitle()
{
	CString	csTitle;
	CString	csName;

	csTitle+=C_BEHAVIOUR_TITLE_LINE;
	csTitle+=C_BEHAVIOUR_TITLE_TEMPLATE;
	csTitle+=C_BEHAVIOUR_TITLE_LINE;

	csName=GetName();

	int	iStart=strlen(C_BEHAVIOUR_TITLE_LINE)+
						 (strlen(C_BEHAVIOUR_TITLE_TEMPLATE)-csName.GetLength())/2;
	for(int iCpt=0;iCpt<csName.GetLength();iCpt++)
	{
		csTitle.SetAt(iStart+iCpt,csName[iCpt]);
	}

	return csTitle;
}

/****************************************************************************/
long CPA_EdIR_MainBehaviour::m_fn_lGetNbLines()
{
	CString	csText;
	csText+=m_fn_csGetBehaviourTitle();
	csText+=m_csText;

	long		lNbLines=0;
	long		lIndex;
	while((lIndex=csText.Find('\n'))!=-1)
	{
		csText=csText.Right(csText.GetLength()-lIndex-1);
		lNbLines++;
	}

	return lNbLines+1;
}

/****************************************************************************/
long CPA_EdIR_MainBehaviour::m_fn_lGetNbPages(long lLineHeight,
																							long lPageHeight)
{
	long	lNbLines=m_fn_lGetNbLines();
	long	lNbPages=0;
	long	lNbLinesInAPage=lPageHeight/lLineHeight;

	while(lNbLines>=0)
	{
		lNbLines-=lNbLinesInAPage;
		lNbPages++;
	}

	return lNbPages;
}

/****************************************************************************/
CString CPA_EdIR_MainBehaviour::m_fn_csGetTextToPrint(long lLineHeight,
																											long lPageHeight,
																											long lCurrentPage)
{
	long	lNbLines=m_fn_lGetNbLines();
	long	lNbLinesInAPage=lPageHeight/lLineHeight;
	long	lCurPage=1;
	long	lCurLine=0;

	while(lCurPage!=lCurrentPage)
	{
		lCurLine+=lNbLinesInAPage;
		lCurPage++;
	}

	return m_fn_csGetTextBetweenLines(lCurLine,lCurLine+lNbLinesInAPage-1);
}

/****************************************************************************/
CString CPA_EdIR_MainBehaviour::m_fn_csGetTextBetweenLines(long lLine1,long lLine2)
{
	CString	csText;
	csText+=m_fn_csGetBehaviourTitle();
	csText+=m_csText;

	CString	csTemp=csText;
	long		lCurLine=0;
	long		lIndex;
	long		lOldIndex=0;
	long		lStartIndex=-1;
	long		lEndIndex=-1;
	while((lIndex=csTemp.Find('\n'))!=-1)
	{
		csTemp=csTemp.Right(csTemp.GetLength()-lIndex-1);

		if(lCurLine==lLine1)
			lStartIndex=lOldIndex;

		if(lCurLine==lLine2)
			lEndIndex=lOldIndex+lIndex+1;

		lCurLine++;

		//Adds lIndex+1 because '\n' is two character
		lOldIndex+=lIndex+1;
	}

	if(lStartIndex==-1)
		return "";

	if(lEndIndex==-1)
		lEndIndex=csText.GetLength();
	else
		lEndIndex--;

	return csText.Mid(lStartIndex,lEndIndex-lStartIndex+1);
}

/****************************************************************************/
BOOL CPA_EdIR_MainBehaviour::m_fn_bHasBeenModified()
{
	if(m_pclBehaviour)
		return (m_bHasBeenModified||m_pclBehaviour->m_fn_bHasBeenModified());
	else
		return m_bHasBeenModified;
}

/****************************************************************************/
void CPA_EdIR_MainBehaviour::m_fn_vSetModified(BOOL bIsModified)
{
	if(m_pclBehaviour)
		m_pclBehaviour->m_fn_vSetModified(bIsModified);

	m_bHasBeenModified=bIsModified;
}

//BEGIN ROMTEAM Cristi Petrescu 98-05-
//****************************************************************************
void CPA_EdIR_MainBehaviour::m_fn_vClearExecutedNodes (void)
{
	POSITION pos = m_clDebugInfoList . GetHeadPosition ();
	CPA_EdIR_DebugInfo *pclDebugInfo;
	while (pos)
	{
		pclDebugInfo = m_clDebugInfoList . GetNext (pos);

		pclDebugInfo -> m_fn_vSetExecuted (FALSE);
	}
}
//END ROMTEAM Cristi Petrescu 98-05-

#endif //D_ED_IR_ACTIVE
