/*=========================================================================
* 
* EDTdUpdt.cpp :  Hierarchy update - Implementation file
* 
*
* Version			1.0
* Creation date		
* Revision date
*
* Shaitan
*=======================================================================*/


#include "stdafx.h"
#ifdef ACTIVE_EDITOR
#include "acp_base.h"

#include "EDTdUpdt.hpp"
#include "EDTBase.Hpp"
#include "EDTSObj.hpp"
#include "EDTSpec.hpp"


//#################################################################################
// EDT_DialogUpdate dialog
//#################################################################################

/*----------------------------------------
----------------------------------------*/
EDT_DialogUpdate::EDT_DialogUpdate(CWnd* pParent /*=NULL*/)
	: CDialog(EDT_DialogUpdate::IDD, &g_oBaseFrame)
{
	//{{AFX_DATA_INIT(EDT_DialogUpdate)
	//}}AFX_DATA_INIT
	POS_fn_vSetIdentityMatrix(&m_stRefMatrix);
}

/*----------------------------------------
----------------------------------------*/
void EDT_DialogUpdate::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(EDT_DialogUpdate)
	DDX_Control(pDX, EDT_IDC_LISTREINSERT1, m_cListInsert1);
	DDX_Control(pDX, EDT_IDC_LISTDESTROY1, m_cListDestroy1);
	DDX_Control(pDX, EDT_IDC_LISTREINSERT, m_cListInsert);
	DDX_Control(pDX, EDT_IDC_LISTDESTROY, m_cListDestroy);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(EDT_DialogUpdate, CDialog)
	//{{AFX_MSG_MAP(EDT_DialogUpdate)
	ON_LBN_SELCHANGE(EDT_IDC_LISTDESTROY, OnSelchangeListDestroy)
	ON_LBN_SELCHANGE(EDT_IDC_LISTREINSERT, OnSelchangeListReinsert)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//#################################################################################
// EDT_DialogUpdate message handler
//#################################################################################
/*----------------------------------------
----------------------------------------*/
CPA_Interface * EDT_DialogUpdate::GetInterface (void)
{
	return m_pEditor->GetInterface();
}

/*----------------------------------------
----------------------------------------*/
void EDT_DialogUpdate::OnSelchangeListDestroy() 
{
	EDT_SuperObject *pObjectToInsert;
	POSITION		pos;
	char			szName[30];
	char			szType[30];
	int				iObjectToInsert = -1;

	// get object to insert
	iObjectToInsert = m_cListDestroy.GetCurSel();
	if (iObjectToInsert != -1)
		pObjectToInsert = m_stListDestroy.FindElementFromIndex(iObjectToInsert);
	// reinsert object
	if (pObjectToInsert)
	{
		// update the lists
		pos = m_stListDestroy.Find(pObjectToInsert, NULL);
		if (pos)
			m_stListDestroy.RemoveAt(pos);
		m_stListInsert.AddTail(pObjectToInsert);
		// update the dialog
		m_cListDestroy.GetText(iObjectToInsert, szName);
		m_cListDestroy1.GetText(iObjectToInsert, szType);
		m_cListDestroy.DeleteString(iObjectToInsert);
		m_cListDestroy1.DeleteString(iObjectToInsert);
		m_cListInsert.AddString(szName);
		m_cListInsert1.AddString(szType);
	}
}

/*----------------------------------------
----------------------------------------*/
void EDT_DialogUpdate::OnSelchangeListReinsert() 
{
	EDT_SuperObject *pObjectToDestroy;
	POSITION		pos;
	char			szName[30];
	char			szType[30];
	int				iObjectToDestroy = -1;
	
	// get object to insert
	iObjectToDestroy = m_cListInsert.GetCurSel();
	if (iObjectToDestroy != -1)
		pObjectToDestroy = m_stListInsert.FindElementFromIndex(iObjectToDestroy);
	// destroy object
	if (pObjectToDestroy)
	{
		// update the lists
		pos = m_stListInsert.Find(pObjectToDestroy, NULL);
		if (pos)
			m_stListInsert.RemoveAt(pos);
		m_stListDestroy.AddTail(pObjectToDestroy);
		// update the dialog
		m_cListInsert.GetText(iObjectToDestroy, szName);
		m_cListInsert1.GetText(iObjectToDestroy, szType);
		m_cListInsert.DeleteString(iObjectToDestroy);
		m_cListInsert1.DeleteString(iObjectToDestroy);
		m_cListDestroy.AddString(szName);
		m_cListDestroy1.AddString(szType);
	}
}

/*----------------------------------------
----------------------------------------*/
void EDT_DialogUpdate::OnOK() 
{
	EDT_SuperObject *pParent;
	EDT_SuperObject *pElem;
	POSITION	    pos;
	
	// re-insert all the selected objects
	for (pElem = m_stListInsert.GetHeadElement(pos); pElem; 
		 pElem = m_stListInsert.GetNextElement(pos))
	{
		pParent = (EDT_SuperObject *) pElem->GetSuperObjectFather();
		if (!fn_bIsInHierarchy(pParent))
			pParent = m_pEditor->GetSpecificEditor()->GetDefaultParentForPaste(pElem);
		if (pParent)
		{
			pElem->SetList(NULL, FALSE);
			pElem->fn_vInsertUnderParent(pParent, NULL, TRUE);
		}
		else
			m_pEditor->fn_vDestroySuperObject(pElem, FALSE);
	}

	// remove all the elements in the lists
	m_stListInsert.RemoveAll();
	
	// destroy all removed elements
	while (m_stListDestroy.GetCount() > 0)
	{
		pElem = m_stListDestroy.RemoveTail();
		m_pEditor->fn_vDestroySuperObject(pElem, FALSE);
	}
 	
	// destroy the dialog
	EndDialog(0);
}

/*----------------------------------------
----------------------------------------*/
void EDT_DialogUpdate::fn_vInitDialog (EDT_HierarchyEditor *pEditor)
{
	// init parent editor
	m_pEditor = pEditor;
	// this dialog is modal
	DoModal();
}

/*----------------------------------------
----------------------------------------*/
BOOL EDT_DialogUpdate::OnInitDialog () 
{
	CPA_SuperObject *pElem;
	POSITION		pos;
	char			szName[30];
	char			szType[30];

	// Create the dialog
	CDialog::OnInitDialog();

	// init list destroy
	for (pElem = m_stListDestroy.GetHeadElement(pos); pElem; pElem = m_stListDestroy.GetNextElement(pos))
	{
		sprintf(szName, "%s", pElem->GetNameToDraw());
		sprintf(szType, " %1s-%s", pElem->GetRealTypeName(), pElem->GetModelName());
		m_cListDestroy.AddString(szName);
		m_cListDestroy1.AddString(szType);
	}
	return TRUE;
}


/*===========================================================================
*		Description:	Check if an object is still in the hierarchy  
*		Creation date:	
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		
*---------------------------------------------------------------------------
*		Revision date:         Author:         
*=========================================================================*/
BOOL EDT_DialogUpdate::fn_bIsInHierarchy (EDT_SuperObject *pParent)
{
	EDT_SuperObject *pElem;
	POSITION		pos, pos1;

	if (!pParent)
		return FALSE;
	
	// check if pParent was destroyed
	pos1 = M_GetListAllObjects()->Find(pParent);
	if (pos1)
	{
		// check if pParent is reinserted
		for (pElem = m_stListInsert.GetHeadElement(pos); pElem;
			 pElem = m_stListInsert.GetNextElement(pos))
		{
			if (pParent->fn_bIsUnderObject(pElem))
				return TRUE;
		}
		// pParent is destroyed
		return FALSE;
	}
	// pParent was not destroyed
	else
		return TRUE;
}

#endif // ACTIVE_EDITOR
