// To handle Watch Window messages
//
// Written by YB on 19 / 01 / 1998
///////////////////////////////////////
#include "StdAfx.h"

#include "EDACWatc.hpp"

#include "EDACCust.hpp"
#include "EDACMStA.hpp"
#include "EDACInst.hpp"
#include "_AInterf.hpp"
#include "EDACVwAc.hpp"

//External Modules
#include "CTL.h"
#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

//*******************************************************************
void OAC_CallBackForWatchWindow(WAW_tdeMessage _tdeMessage,
																void *_p_vData)
{
	switch ( _tdeMessage )
	{
	case WAW_eDeletedData:
		OAC_fn_vDeleteDataFromWatch((WAW_Data *)_p_vData);
		break;

	case WAW_eDeletedNode:
		OAC_fn_vDeleteNodeFromWatch((WAW_Data *)_p_vData);
		break;

	case WAW_eUpdateData:
		{
			WAW_DataList clListOfMyData("OAC_Module_for_WAW");
			clListOfMyData.m_pub_fn_vGetAll();
			clListOfMyData.m_pub_fn_vSelectData();
			clListOfMyData.m_pub_fn_vSelectVisible();
			
			WAW_Data *pclCurrentData;
			CTL_Editor_Data *pclData;
			POSITION pos = clListOfMyData.GetHeadPosition();
			while ( pos != NULL )
			{
				pclCurrentData = clListOfMyData.GetNext(pos);

				OAC_tdstWatchField *p_tdstWatchField = (OAC_tdstWatchField *)pclCurrentData->m_pub_fn_pvGetUserData();
				pclData = (CTL_Editor_Data *)p_tdstWatchField->p_vData;
				pclData->m_fn_vGetMotorData();

				WAW_fn_bUpdateData(pclCurrentData, OAC_fn_csComputeTotalValueString(pclData));
			}
		}
		break;

	case WAW_eHideWindow:
		((CButton *)g_pclInterface->m_clDocument.m_pclActorsView->GetDlgItem(IDC_CHECK_WATCH))->SetCheck(FALSE);
		g_pclInterface->m_clDocument.m_pub_bWatchWindowIsVisible = FALSE;
		break;

	case WAW_eShowWindow:
		((CButton *)g_pclInterface->m_clDocument.m_pclActorsView->GetDlgItem(IDC_CHECK_WATCH))->SetCheck(TRUE);
		g_pclInterface->m_clDocument.m_pub_bWatchWindowIsVisible = TRUE;
		break;
	}
}

//*******************************************************************
void OAC_AddFieldInWatchWindow(CTL_Editor_Data *_pclData)
{
	//Gets Parent Actor
	WAW_Data *pclParentActorWatchData;
	EdActors_EditorActor *pclParentActor = OAC_fn_pclGetParentActorOfData(_pclData);
	ERROR_ASSERT( pclParentActor != NULL );
	ERROR_ASSERT( pclParentActor->m_fn_bIsAnInstance() );
	EdActors_EditorActorInstance *pclParentInstance = (EdActors_EditorActorInstance *)pclParentActor;
	
	if ( pclParentInstance->m_pub_fn_p_tdstGetWatchField()->eType == OAC_WATCH_FIELD_TYPE__NONE )
	{
		//Adds in Watch
		pclParentActorWatchData = WAW_fn_pclAddNode("OAC_Module_for_WAW",
																								NULL,
																								pclParentInstance->m_pclActor->GetName());
 		WAW_fn_bSetDataIcon(pclParentActorWatchData, g_pclInterface->m_clDocument.m_pub_hWatchIcon_Actor);

		pclParentInstance->m_pub_fn_p_tdstGetWatchField()->eType == OAC_WATCH_FIELD_TYPE__ACTOR;	
		pclParentInstance->m_pub_fn_p_tdstGetWatchField()->pclWatchData == pclParentActorWatchData;	
		
		pclParentActorWatchData->m_pub_fn_vSetUserData( (void *)pclParentInstance->m_pub_fn_p_tdstGetWatchField() );
	}
	else
		pclParentActorWatchData = pclParentInstance->m_pub_fn_p_tdstGetWatchField()->pclWatchData;

	//Gets Parent AMS
	WAW_Data *pclParentMSWatchData;
	EdActors_ActorMiniStructure *pclParentAMS = OAC_fn_pclGetParentAMSOfData(_pclData);
	ERROR_ASSERT( pclParentAMS != NULL );
	if ( pclParentAMS->m_pub_fn_p_tdstGetWatchField()->eType == OAC_WATCH_FIELD_TYPE__NONE )
	{
		//Adds in Watch
		pclParentMSWatchData = WAW_fn_pclAddNode("OAC_Module_for_WAW",
																							pclParentActorWatchData,
																							pclParentAMS->m_fn_csGetScriptName());
 		WAW_fn_bSetDataIcon(pclParentMSWatchData, g_pclInterface->m_clDocument.m_pub_hWatchIcon_MS);

		pclParentAMS->m_pub_fn_p_tdstGetWatchField()->eType == OAC_WATCH_FIELD_TYPE__MS;	
		pclParentAMS->m_pub_fn_p_tdstGetWatchField()->pclWatchData == pclParentMSWatchData;	
		
		pclParentMSWatchData->m_pub_fn_vSetUserData( (void *)pclParentAMS->m_pub_fn_p_tdstGetWatchField() );
	}
	else
		pclParentMSWatchData = pclParentAMS->m_pub_fn_p_tdstGetWatchField()->pclWatchData;

	//Adds data
	WAW_Data *pclAddedWatchData = WAW_fn_pclAddData("OAC_Module_for_WAW",
																									pclParentMSWatchData,
																									_pclData->m_pub_fn_csGetDataName(),
																									OAC_fn_csComputeTotalValueString(_pclData),
																									NULL);
	WAW_fn_bSetDataIcon(pclAddedWatchData, g_pclInterface->m_clDocument.m_pub_hWatchIcon_Field);

	OAC_tdstWatchField *p_tdstWatchField = new OAC_tdstWatchField;
	p_tdstWatchField->eType = OAC_WATCH_FIELD_TYPE__DATA;
	p_tdstWatchField->p_vData = (void *)_pclData;
	p_tdstWatchField->pclWatchData = pclAddedWatchData;

	pclAddedWatchData->m_pub_fn_vSetUserData((void *)p_tdstWatchField);
	
	OAC_fn_vSetDataInWatch(_pclData, pclAddedWatchData);

// CPA2 ASilvescu 98-05-07
	CTL_Editor_Control *parent = _pclData->m_pub_fn_pclGetParentControl();
	if(parent) parent->m_pub_fn_pclGetNameStatic()->InvalidateRect(NULL);
// Save watch data
		CString saveFileName = M_GetMainApp()->m_csEditorDataPath;
		saveFileName += "watch.dat";
		WritePrivateProfileString(pclParentInstance->m_pclActor->GetName(), _pclData->m_pub_fn_csGetDataName(), pclParentAMS->m_fn_csGetScriptName(), saveFileName);
// End CPA2 ASilvescu 98-05-07
}

//*******************************************************************
void OAC_RemoveFieldFromWatchWindow(CTL_Editor_Data *_pclData)
{
	ERROR_ASSERT( _pclData != NULL );

	WAW_Data *pclWAWData = OAC_fn_pclGetAssociatedWatchData(_pclData);
//	OAC_tdstWatchField *p_tdstWatchField = (OAC_tdstWatchField *)pclWAWData->m_pub_fn_pvGetUserData();
//	delete p_tdstWatchField;

	WAW_fn_bDeleteData(pclWAWData);
//	OAC_fn_vSetDataInWatch(_pclData, NULL);

//	_pclData->m_pub_fn_pclGetParentControl()->m_pub_fn_pclGetNameStatic()->InvalidateRect(NULL);

// CPA2 ASilvescu 98-05-07
// Delete watch data
	CString saveFileName = M_GetMainApp()->m_csEditorDataPath;
	saveFileName += "watch.dat";
	EdActors_EditorActor *pclActor = OAC_fn_pclGetParentActorOfData(_pclData);
	if(pclActor)
		WritePrivateProfileString(pclActor->m_pclActor->GetName(), _pclData->m_pub_fn_csGetDataName(), NULL, saveFileName);
// End CPA2 ASilvescu 98-05-07
}

//*******************************************************************
CString OAC_fn_csComputeTotalValueString(CTL_Editor_Data *_pclData)
{
	CString csValue;
	CStringList *pcslList = _pclData->m_fn_pcslFormatDataValueString();

	POSITION pos = pcslList->GetHeadPosition();
	while ( pos != NULL )
	{
		csValue += pcslList->GetNext(pos);
		
		if ( pos != NULL )
			csValue += " ; ";
	}

	return csValue;
}

//*******************************************************************
void OAC_fn_vDeleteDataFromWatch(WAW_Data *_pclWatchData)
{
	ERROR_ASSERT( _pclWatchData != NULL );

	OAC_tdstWatchField *p_tdstWatchField = (OAC_tdstWatchField *)_pclWatchData->m_pub_fn_pvGetUserData();
	CTL_Editor_Data *pclData = (CTL_Editor_Data *)p_tdstWatchField->p_vData;
	delete p_tdstWatchField;

	OAC_fn_vSetDataInWatch(pclData, NULL);

//	pclData->m_pub_fn_pclGetParentControl()->m_pub_fn_pclGetNameStatic()->InvalidateRect(NULL);

// CPA2 ASilvescu 98-05-07
// Delete watch data
		CTL_Editor_Control *parent = pclData->m_pub_fn_pclGetParentControl();
		if(parent) parent->m_pub_fn_pclGetNameStatic()->InvalidateRect(NULL);
		CString saveFileName = M_GetMainApp()->m_csEditorDataPath;
		saveFileName += "watch.dat";
		EdActors_EditorActor *pclActor = OAC_fn_pclGetParentActorOfData(pclData);
		if(pclActor)
			WritePrivateProfileString(pclActor->m_pclActor->GetName(), pclData->m_pub_fn_csGetDataName(), NULL, saveFileName);
// End CPA2 ASilvescu 98-05-07
}

//*******************************************************************
void OAC_fn_vDeleteNodeFromWatch(WAW_Data *_pclWatchData)
{
	ERROR_ASSERT( _pclWatchData != NULL );

	OAC_tdstWatchField *p_tdstWatchField = (OAC_tdstWatchField *)_pclWatchData->m_pub_fn_pvGetUserData();
	
	switch ( p_tdstWatchField->eType )
	{
		case OAC_WATCH_FIELD_TYPE__ACTOR:
		{
			EdActors_EditorActorInstance *pclParentInstance = (EdActors_EditorActorInstance *)p_tdstWatchField->p_vData;
			pclParentInstance->m_pub_fn_p_tdstGetWatchField()->eType = OAC_WATCH_FIELD_TYPE__NONE;
			pclParentInstance->m_pub_fn_p_tdstGetWatchField()->pclWatchData = NULL;
		}
			break;

		case OAC_WATCH_FIELD_TYPE__MS:
		{
			EdActors_ActorMiniStructure *pclParentAMS = (EdActors_ActorMiniStructure *)p_tdstWatchField->p_vData;
			pclParentAMS->m_pub_fn_p_tdstGetWatchField()->eType = OAC_WATCH_FIELD_TYPE__NONE;
			pclParentAMS->m_pub_fn_p_tdstGetWatchField()->pclWatchData = NULL;
		}
			break;
	}
}

//*******************************************************************
void OAC_fn_vUpdateDataInWatchWindow(CTL_Editor_Data *_pclData)
{
	WAW_fn_bUpdateData(OAC_fn_pclGetAssociatedWatchData(_pclData), OAC_fn_csComputeTotalValueString(_pclData));
}

//*******************************************************************
void OAC_fn_vUpdateAllDataInWatchWindow()
{
	WAW_DataList clListOfMyData("OAC_Module_for_WAW");
	clListOfMyData.m_pub_fn_vSelectData();
	clListOfMyData.m_pub_fn_vSelectVisible();
	
	WAW_Data *pclCurrentData;
	CTL_Editor_Data *pclData;
	POSITION pos = clListOfMyData.GetHeadPosition();
	while ( pos != NULL )
	{
		pclCurrentData = clListOfMyData.GetNext(pos);

		OAC_tdstWatchField *p_tdstWatchField = (OAC_tdstWatchField *)pclCurrentData->m_pub_fn_pvGetUserData();
		pclData = (CTL_Editor_Data *)p_tdstWatchField->p_vData;
		pclData->m_fn_vGetMotorData();

		WAW_fn_bUpdateData(pclCurrentData, OAC_fn_csComputeTotalValueString(pclData));
	}
}
