// EdIRWaW.cpp : implementation file
//

#include "stdafx.h"

#include "Defines.hpp"

#ifdef D_ED_IR_ACTIVE

#include "EdIRWaW.hpp"

#include "EdIRDat.hpp"
#include "EdIRStrg.hpp"

#include "EDACActr.hpp"
#include "_Actors.hpp"

#include "WAW.h"

// CPA2 ASilvescu 98-05-06
#include "dpt.h"
// CPA2 ASilvescu 98-05-06

/**********************************************************************************/
static CString fn_csGetValue(WAW_Data *pclWatchData)
{
	CString	csValue;

	CTL_Editor_Data *pclData=(CTL_Editor_Data *)pclWatchData->m_pub_fn_pvGetUserData();

	if(pclData!=NULL)
	{
		CString			csDsgVarName;
		CString			csDsgVarType;
		CStringList	cslDsgVarValues;
		fn_vGetInfoAboutDsgVar(pclData,csDsgVarName,csDsgVarType,&cslDsgVarValues);

		//Is it an array ?
		if(cslDsgVarValues.GetCount()>1)
		{
			CString	csWatchName=pclWatchData->m_pub_fn_csGetDataName();
			long		lIndex=csWatchName.Find('[');
			if(lIndex!=-1)
			{
				long	lEndIndex=csWatchName.Find(']');
				if(lEndIndex!=-1)
				{
					CString	csArrayIndex=csWatchName.Mid(lIndex+1,lEndIndex-(lIndex+1));
					long		lArrayIndex=atol(M_MAKECHAR(csArrayIndex));
					
					POSITION	pos=cslDsgVarValues.FindIndex(lArrayIndex);
					if(pos!=NULL)
						csValue=cslDsgVarValues.GetAt(pos);
					else
					{
						ASSERT(FALSE);
					}
				}
				else
				{
					ASSERT(FALSE);
				}
			}
			else
			{
				ASSERT(FALSE);
			}
		}
		else
			csValue=cslDsgVarValues.GetHead();
	}
	else
	{
		ASSERT(FALSE);
	}

	return csValue;
}

/**********************************************************************************/
void fn_vCallBackForWatchWindow(WAW_tdeMessage eMessage,void *pvData)
{
	switch(eMessage)
	{
		case WAW_eDeletedData:
			{
				WAW_Data *pclWatchData=(WAW_Data *)pvData;
				if(pclWatchData!=NULL)
				{
					CTL_Editor_Data *pclData=(CTL_Editor_Data *)pclWatchData->m_pub_fn_pvGetUserData();

					if(pclData!=NULL)
					{
						fn_vSetWatchData(pclData,NULL);
						fn_vSetInWatch(pclData,FALSE);

// CPA2 ASilvescu 98-05-06
						CString saveFileName = M_GetMainApp()->m_csEditorDataPath;
						saveFileName += "watch.dat";
						EdActors_EditorActor *pclActor=fn_pclGetParentActor(pclData);
						if(pclActor)
							WritePrivateProfileString(pclActor->m_pclActor->GetName(), pclData->m_pub_fn_csGetDataName(), NULL, saveFileName);
// CPA2 ASilvescu 98-05-06

						CString			csDsgVarName;
						CString			csDsgVarType;
						CStringList	cslDsgVarValues;
						fn_vGetInfoAboutDsgVar(pclData,csDsgVarName,csDsgVarType,&cslDsgVarValues);

						//Is it an single entry ?
						if(cslDsgVarValues.GetCount()==1)
						{
							//Update display
							CTL_Editor_Control	*pclControl=pclData->m_pub_fn_pclGetParentControl();
							CTL_Editor_Static		*pclStatic=pclControl->m_pub_fn_pclGetNameStatic();
// CPA2 ASilvescu 98-04-30
							if(pclStatic)
// End CPA2 ASilvescu 98-04-30
							pclStatic->InvalidateRect(NULL);
						}
					}
					else
					{
						ASSERT(FALSE);
					}
				}
				else
				{
					ASSERT(FALSE);
				}
			}
			break;

		case WAW_eDeletedNode:
			{
				WAW_Data *pclWatchData=(WAW_Data *)pvData;
				if(pclWatchData!=NULL)
				{
					CTL_Editor_Data *pclData=(CTL_Editor_Data *)pclWatchData->m_pub_fn_pvGetUserData();

					if(pclData!=NULL)
					{
						fn_vSetWatchData(pclData,NULL);

						CString			csDsgVarName;
						CString			csDsgVarType;
						CStringList	cslDsgVarValues;
						fn_vGetInfoAboutDsgVar(pclData,csDsgVarName,csDsgVarType,&cslDsgVarValues);

						//Is it an array ?
						if(cslDsgVarValues.GetCount()>1)
						{
							fn_vSetInWatch(pclData,FALSE);

							//Update display
							CTL_Editor_Control	*pclControl=pclData->m_pub_fn_pclGetParentControl();
							CTL_Editor_Static		*pclStatic=pclControl->m_pub_fn_pclGetNameStatic();
// CPA2 ASilvescu 98-04-30
							if(pclStatic)
// End CPA2 ASilvescu 98-04-30
							pclStatic->InvalidateRect(NULL);
						}
					}
				}
				else
				{
					ASSERT(FALSE);
				}
			}
			break;

		case WAW_eUpdateData:
			{
				WAW_DataList	clWAWDataList(C_MODULE_NAME);
				clWAWDataList.m_pub_fn_vGetVisible();
				clWAWDataList.m_pub_fn_vSelectData();
				POSITION	pos=clWAWDataList.GetHeadPosition();
				while(pos!=NULL)
				{
					WAW_Data *pclWatchData=clWAWDataList.GetNext(pos);

					CString	csValue=fn_csGetValue(pclWatchData);
					WAW_fn_bUpdateData(pclWatchData,csValue);
				}
			}
			break;

		case WAW_eHideWindow:
			{
			}
			break;

		case WAW_eShowWindow:
			{
			}
			break;

		default:
			{
				AfxMessageBox("fn_vCallBackForWatchWindow: unknown message !!");
			}
	}
}

//*******************************************************************************
void fn_vAddValueInWatch(CTL_Editor_Data *pclData)
{
	EdActors_EditorActor *pclActor=fn_pclGetParentActor(pclData);
	if(pclActor!=NULL)
	{
		//Adds Actor Name node
		CString		csActorName=pclActor->m_pclActor->GetName();
		WAW_Data	*pclWatchData=WAW_fn_pclAddNode(C_MODULE_NAME,NULL,csActorName);

		//Adds "Designer Variables" node
		pclWatchData=WAW_fn_pclAddNode(C_MODULE_NAME,pclWatchData,"Designer Variables");

		//Adds Data (Name+Value)
		CString			csDsgVarName;
		CString			csDsgVarType;
		CStringList	cslDsgVarValues;
		fn_vGetInfoAboutDsgVar(pclData,csDsgVarName,csDsgVarType,&cslDsgVarValues);

		ASSERT(cslDsgVarValues.IsEmpty()==FALSE);

// CPA2 ASilvescu 98-05-06
		CString saveFileName = M_GetMainApp()->m_csEditorDataPath;
		saveFileName += "watch.dat";
		WritePrivateProfileString(csActorName, pclData->m_pub_fn_csGetDataName(), "Designer Variables", saveFileName);
// CPA2 ASilvescu 98-05-06

		if(cslDsgVarValues.GetCount()==1)
		{
			pclWatchData=WAW_fn_pclAddData(C_MODULE_NAME,pclWatchData,csDsgVarName,cslDsgVarValues.GetHead(),pclData);
			fn_vSetWatchData(pclData,pclWatchData);
		}
		else
		{
			//It is an array
			//Adds a node for the array
			pclWatchData=WAW_fn_pclAddNode(C_MODULE_NAME,pclWatchData,csDsgVarName,pclData);
			fn_vSetWatchData(pclData,pclWatchData);

			//Adds array entries
			long			lIndex=0;
			CString		csIndex;
			CString		csDsgVarNameEntry;
			POSITION	pos=cslDsgVarValues.GetHeadPosition();
			while(pos!=NULL)
			{
				csIndex.Format("%ld",lIndex);
				csDsgVarNameEntry=csDsgVarName+"["+csIndex+"]";
				WAW_fn_pclAddData(C_MODULE_NAME,pclWatchData,csDsgVarNameEntry,cslDsgVarValues.GetNext(pos),pclData);
				lIndex++;
			}
		}
	}
	else
	{
		AfxMessageBox("EdIRWaW::fn_vAddValueInWatch : Parent actor is NULL !!");

		ASSERT(FALSE);
	}
}

//*******************************************************************************
void fn_vRemoveValueFromWatch(CTL_Editor_Data *pclData)
{
	WAW_Data *pclWatchData=fn_pclGetWatchData(pclData);
	if(pclWatchData!=NULL)
	{
		WAW_fn_bDeleteData(pclWatchData);
		fn_vSetWatchData(pclData,NULL);
	}
	else
	{
		AfxMessageBox("EdIRWaW::fn_vRemoveValueFromWatch : Watch data is NULL !!");

		ASSERT(FALSE);
	}
// CPA2 ASilvescu 98-05-06
	CString saveFileName = M_GetMainApp()->m_csEditorDataPath;
	saveFileName += "watch.dat";
	EdActors_EditorActor *pclActor=fn_pclGetParentActor(pclData);
	if(pclActor)
		WritePrivateProfileString(pclActor->m_pclActor->GetName(), pclData->m_pub_fn_csGetDataName(), NULL, saveFileName);
// CPA2 ASilvescu 98-05-06
}

#endif //D_ED_IR_ACTIVE
