//#################################################################################
//
// BASE CLASS OF YOUR DLL interface
//
//#################################################################################
#include "StdAfx.h"

#include "ACP_Base.h"
#include "ITF.h"
#include "IncMEC.h"
#define D_State_Define
#include "IncGAM.h"
#undef D_State_Define
#include "GLI.h"
#include "SCR.h"

#include "_IADItrf.hpp"

#include "IADVwMn.hpp"
#include "IADStrg.hpp"
#include "IAD_Res.h"
//BEGIN ROMTEAM Cristi Petrescu 98-05-
#include "IADvwrst.hpp"
#include "IADVrVw.hpp"
//END ROMTEAM Cristi Petrescu 98-05-

// BEGIN ROMTEAM Cristi Petrescu 98-06-
#include "IADglob.hpp"

#include "debugcom.h"
// END ROMTEAM Cristi Petrescu 98-06-

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

// infos
static CString g_csTIDName = "IA_Debugger";
static CString g_csTIDAuthor = "Y. Babitch";
static CString g_csTIDVersion = "V 2.0.1   18/03/98";
static CString g_csTIDFrenchHelpFile = "";
static CString g_csTIDEnglishHelpFile = "";

BOOL g_bTipFirstTime = TRUE;

IAD_Interface *g_pclInterface;

#define C_IAD_uiOpenID								51
#define C_IAD_uiCloseID								52

#define C_IAD_PopupID_Exit_Editor			61

#define C_IAD_PopupID_Setup						71

// the DLL global definition
tdstDLLIdentity g_stIADDLLIdentity; 

//--------------------------------------------------------------------------------
// CONSTRUCTOR.
// You must put here all your DLL interface definitions.
//--------------------------------------------------------------------------------
IAD_Interface::IAD_Interface(void)
{
	// ************************** private internal
	m_p_stDLLIdentity = &g_stIADDLLIdentity;

	// Does your DLL can output in main game view ?
	m_stBaseDLLDefinition.bCanOutputIn3DView = FALSE;

	// Does your DLL can be refresh by engine ?
	m_stBaseDLLDefinition.bCanBeRefreshedByEngine = FALSE;

	// Specific Init.
	m_pclIADMainFrame = NULL;

	m_csEditorVersion = "1.1.0 [" + CString(__DATE__) + "]";

    SetEditorInfo(g_csTIDName, g_csTIDAuthor, g_csTIDVersion, g_csTIDFrenchHelpFile, g_csTIDEnglishHelpFile);

	//For errors
	ERROR_g_fn_vAddAModuleDescriptor(	g_c_csIADebuggerModuleNameForErrors,
																		m_csEditorVersion,
																		"Yves BABITCH",
																		"01 48 18 53 74 (Montreuil)",
																		"ybabitch@ubisoft.fr",
																		"Daniel PALIX",
																		"04 50 51 26 63 (Annecy)",
																		"dpalix@ubisoft.fr",
																		"David REIZER",
																		"01 48 18 53 74 (Montreuil)",
																		"dreizer@ubisoft.fr"
																	);

	SetCurrent(FALSE);
}

//**************************************************************************************
void IAD_Interface::fn_vJustAfterRegistered(void)
{

}

//**************************************************************************************
void IAD_Interface::fn_vConstruct()
{
	//Windows constructions

	//Creates the main frame
	if ( m_pclIADMainFrame == NULL )
	{
		m_pclIADMainFrame = new IAD_MainFrame;

		m_pclIADMainFrame->CreateBase("",
																	1,
																	2,
																	GetInterface()->GetMainWorld() );

		m_pclIADMainFrame->mfn_vEnableCaption(FALSE);

		m_pclIADMainFrame->m_iInitWidth = 400;
		m_pclIADMainFrame->m_bKeepPos = TRUE;
		
		m_pclIADMainFrame->m_stWinRight.cLinkSame = FRM_C_MoveLeft;
	}
}

//**************************************************************************************
void IAD_Interface::fn_vLevelChanges()
{

}

//**************************************************************************************
tdePermission IAD_Interface::fn_eAcceptNewEditor(CPA_EditorBase *pNewEditor)
{
	return C_Allow;
}

//**************************************************************************************
BOOL IAD_Interface::fn_bCanActivateEditor(CPA_List<CPA_BaseObject> *pParams)
{
	return TRUE;
}

//**************************************************************************************
void IAD_Interface::fn_vOnActivateEditor(CPA_List<CPA_BaseObject> *pParams,
																					BOOL bBackActivated)
{
	m_fn_vShowEditor();
}

//**************************************************************************************
BOOL IAD_Interface::fn_bCanCloseEditor(void)
{
	return TRUE;
}

//**************************************************************************************
void IAD_Interface::fn_vOnCloseEditor(void)
{

}

//**************************************************************************************
void IAD_Interface::fn_vBeforeEngine(void)
{
	//Re-inits the Trace of Actors
	m_clDocument.m_pub_fn_vReInitEditorTraceForActors();

	m_fn_vHideEditor();
}

//**************************************************************************************
void IAD_Interface::fn_vBeforeEditor(void)
{
	if ( fn_bIsCurrentEditor() ) 
	{
		m_fn_vShowEditor();

		// BEGIN ROMTEAM Cristi Petrescu 98-08-
		// hack for bug-fixing
		m_clDocument.m_pclControlView -> m_fn_vDisplayActorsList();
		// END ROMTEAM Cristi Petrescu 98-08-
		// BEGIN ROMTEAM Cristi Petrescu 98-08-
		// do we really need this here?
		// do it before the engine if you like...
		// as you do.
		//m_clDocument.m_pub_fn_vFreeEditorTraceForEditedActor();
		// END ROMTEAM Cristi Petrescu 98-08-
	}
}

//**************************************************************************************
void IAD_Interface::fn_vHasLostFocus(void)
{

}

//**************************************************************************************
BOOL IAD_Interface::fn_bHasGainedFocus(void)
{
	m_clDocument.m_pclControlView->m_pub_fn_vBuildActorsList();
	m_clDocument.m_pub_fn_vFreeEditorTraceForEditedActor();

	// display Tip of the Day
	GetInterface()->fn_vDisplayTipOfDay("AI Debugger", "TID", g_bTipFirstTime);
	g_bTipFirstTime = FALSE;

	return TRUE;
}

//**************************************************************************************
BOOL IAD_Interface::fn_bDefineSubMenu(EDT_SubMenu *_p_oEDTSubMenu)
{
	if ( _p_oEDTSubMenu->GetSubMenuType() == C_SubMenuTool )
	{
		if ( fn_bIsCurrentEditor() )
			_p_oEDTSubMenu->AddAnEntry(this, "IA Debugger", C_IAD_uiCloseID, TRUE);
		else
			_p_oEDTSubMenu->AddAnEntry(this, "IA Debugger", C_IAD_uiOpenID, FALSE);
	}
	else if ( _p_oEDTSubMenu->GetSubMenuType() == C_SubMenuPreference )
	{
		if ( fn_bIsCurrentEditor() )
			_p_oEDTSubMenu->AddAnEntry(this, "Internal Setup (IA Debugger)", C_IAD_PopupID_Setup, FALSE);
	}

	return TRUE;
}


//**************************************************************************************
void IAD_Interface::_OnSubMenuCommand(EDT_SubMenu *_p_oEDTSubMenu,
																			UINT uiMsgID)
{
	switch (uiMsgID)
	{
		case C_IAD_uiOpenID:
			if ( !fn_bIsCurrentEditor() )
				GetMainWorld()->fn_bActivateEditor(this, NULL);
			break;

		case C_IAD_uiCloseID:
			if ( fn_bIsCurrentEditor() )
				GetMainWorld()->fn_bCloseEditor(this);
			break;

		case C_IAD_PopupID_Setup:
			m_clDocument.m_pub_fn_vDisplaySetup();
			break;
	}
}

//**************************************************************************************
//**************************************************************************************
// Popup
//**************************************************************************************
BOOL IAD_Interface::fn_bDefinePopupMenu(EDT_PopUpMenu *pPopup,
																				CPA_List<CPA_SuperObject> *pSelection,
																				BOOL bIsAlone)
{
	BOOL bReturn = FALSE;
	
	if ( !bIsAlone )
	{
		if ( fn_bIsCurrentEditor() )
		{
			pPopup->AddAnEntry(this, "Exit IA Debugger", C_IAD_PopupID_Exit_Editor);

			bReturn = TRUE;
		}
	}

	return bReturn;
}

//**************************************************************************************
void IAD_Interface::_OnPopUpMenuCommand(UINT m_IDCmdMsg)
{
	switch (m_IDCmdMsg)
	{
		case C_IAD_PopupID_Exit_Editor:
			if ( fn_bIsCurrentEditor() )
				GetMainWorld()->fn_bCloseEditor(this);
		break;
	}
}

//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
// Specific members

//***********************************************************************
void IAD_Interface::m_fn_vShowEditor()
{
	g_oFrameGest.mfn_vSetRefresh(FALSE);

	if ( m_pclIADMainFrame != NULL )
		g_oFrameGest.mfn_vActivateWindow(m_pclIADMainFrame);

	g_oFrameGest.mfn_vSetRefresh(TRUE);
}

//***********************************************************************
void IAD_Interface::m_fn_vHideEditor()
{						  
	g_oFrameGest.mfn_vSetRefresh(FALSE);

	if ( m_pclIADMainFrame != NULL )
		g_oFrameGest.mfn_vDisactivateWindow(m_pclIADMainFrame);

	g_oFrameGest.mfn_vSetRefresh(TRUE);
}

//**************************************************************************************
IAD_MyDocument *IAD_Interface::m_fn_pclGetDocument()
{
	ASSERT ( AfxIsValidAddress(&m_clDocument, sizeof(IAD_MyDocument)) );

	return &m_clDocument;
}

//BEGIN ROMTEAM Cristi Petrescu 98-05-
//**************************************************************************************
//**************************************************************************************
//**************************************************************************************
// Comunication

//**************************************************************************************
long IAD_Interface::OnQueryAction(CPA_EditorBase *p_oSender,WPARAM wParam,LPARAM lParam)
{
	long	lResult=0;
	// BEGIN ROMTEAM Cristi Petrescu 98-06-
	static	CString csResult;
	// END ROMTEAM Cristi Petrescu 98-06-

	switch(wParam)
	{
		case C_uiAI_ViewVariable:
			{
				// BEGIN ROMTEAM Cristi Petrescu 98-06-
				tdstDebugAICom *p_tdstDebugAICom = (tdstDebugAICom *) lParam;

				m_clDocument . m_pclVariableView -> m_fn_vAddVariable (p_tdstDebugAICom -> csName, p_tdstDebugAICom -> csValue);

				lResult = 1;
				// END ROMTEAM Cristi Petrescu 98-06-
			}
			break;

		// BEGIN ROMTEAM Cristi Petrescu 98-06-
		case C_uiAI_GetVariableValue:
			{
				// get the current node
				IAD_MyDocument *pclDocument = m_fn_pclGetDocument ();
				ASSERT (pclDocument);
				IAD_ResultView *pclResultView = pclDocument -> m_pclResultView;
				ASSERT (pclResultView);
				IAD_EditorTreeNode *pclCurrentNode = pclResultView -> m_fn_pclGetSelectedEditorNode ();
				if (pclCurrentNode)
				{
					IAD_EditorTreeNode *pclEngineLoopNode =  pclCurrentNode -> m_fn_pclGetEngineLoopParentNode ();
					tdstGetSetParam *p_stVariableValue = pclEngineLoopNode -> m_fn_p_tdstGetVariableValue (lParam);

					if (p_stVariableValue)
					{
						csResult = IAD_fn_csComputeValueFromGetSetParam (p_stVariableValue);
						lResult = (long) & csResult;
					}
				}

				break;
			}
		// END ROMTEAM Cristi Petrescu 98-06-
	}
		
	return lResult;
}
//END ROMTEAM Cristi Petrescu 98-05-
