//ROMTEAM Networks (Gabriela Dumitrascu 15/03/98)

/*
///////////////////////////////////////////////////////////////////////////////////////////////////
// Description : NtwMod.hpp
//   
// Modification classes
//
///////////////////////////////////////////////////////////////////////////////////////////////////
// inherit from CPA_Modif
//					
///////////////////////////////////////////////////////////////////////////////////////////////////
// Creation date: 98-03-15                        Author: CPA2 Gabriela Dumitrascu
///////////////////////////////////////////////////////////////////////////////////////////////////
// Modification date:			                      Author: 
///////////////////////////////////////////////////////////////////////////////////////////////////
*/

#include "stdafx.h"
#include "acp_base.h"
#include "resource.h"
#include "incdpt.h"
#include "incitf.h"
#include "incgam.h"
#include "incai.h"
#include "WPObj.hpp"
//ANNECY Shaitan Nettoyage (12/05/98) {
/*
#include "WayObj.hpp"
#include "LinkObj.hpp"
*/
//ENDANNECY Shaitan Nettoyage }
#include "Inter.hpp"
#include "NtwMod.hpp"
#include "NtwDia.hpp"
#include "x:\cpa\main\inc\_editid.h"

ModifGraph::ModifGraph(Graph *poGraph, unsigned short type) : CPA_Modif (type,"",FALSE)
{
	m_poGraph = poGraph;
	m_hOldGraph = WPG_fn_hCreate("", "");
	WPG_fn_vCopy(m_hOldGraph, m_poGraph->GetEngineStruct());
	m_hNewGraph = NULL;
	switch (type)
	{
	case GRAPH_INSERT_GRAPH:
		SetName("Insert graph");
		break;
	case GRAPH_DELETE_GRAPH:
		SetName("Delete graph");
		break;
	case GRAPH_INSERT_NODE:
		SetName("Insert node");
		break;
	case GRAPH_DELETE_NODE:
		SetName("Delete node");
		break;
	case GRAPH_INSERT_ARC:
		SetName("Insert arc");
		break;
	case GRAPH_DELETE_ARC:
		SetName("Delete arc");
		break;
	}
}

void ModifGraph::ModifSave()
{
	m_hNewGraph = WPG_fn_hCreate("", "");
	WPG_fn_vCopy(m_hNewGraph, m_poGraph->GetEngineStruct());
}

BOOL ModifGraph::Do (void)
{
	WPG_fn_vCopy(m_poGraph->GetEngineStruct(), m_hNewGraph);
	m_poGraph->fn_vNotifySave();
	m_poGraph->fn_pGetDialog()->fn_vRefreshDialog();
	return TRUE;
}

BOOL ModifGraph::Undo (void)
{
	WPG_fn_vCopy(m_poGraph->GetEngineStruct(), m_hOldGraph);
	m_poGraph->fn_vNotifySave();
	m_poGraph->fn_pGetDialog()->fn_vRefreshDialog();
	return TRUE;
}

//ENDROMTEAM Networks (Gabriela Dumitrascu)



// Shaitan Correction {
/*===========================================================================
	Delete Graph
===========================================================================*/
DeleteGraph::DeleteGraph (Graph *pGraph)
	: CPA_Modif (GRAPH_DELETE_GRAPH, "Delete Graph", FALSE)
{
	// register parameters
	m_pGraph = pGraph;
	// init flag
	m_bDeleted = FALSE;
	m_bFirstTime = TRUE;
}

DeleteGraph::~DeleteGraph	(void)
{

}

BOOL DeleteGraph::Do	(void)
{
	// update editor
	POSITION DeletePos = Graph::ms_oListOfGraph.Find(m_pGraph);
	Graph::ms_oListOfGraph.RemoveAt(DeletePos);

	// update engine
	SCR_tdst_Link_Value *pValue = SCR_fnp_st_Link_SearchValue(WPG_fnp_Graph_GetLinkTable(), (unsigned long) m_pGraph->GetEngineStruct());
	if (pValue)
	{
		SCR_fn_v_Link_DeleteEntry(WPG_fnp_Graph_GetLinkTable(), pValue);
	}

	// update flag
	m_pGraph->fn_vNotifyUnSave();
	m_bDeleted = TRUE;

	// update interface
	Graph::fn_pGetDialog()->fn_vRefreshGraphList(NULL);
	Graph::GetInterface()->fn_vSelectGraph(NULL);

	m_bFirstTime = FALSE;
	return TRUE;
}

BOOL DeleteGraph::Undo (void)
{
	// update editor
	Graph::ms_oListOfGraph.AddTail(m_pGraph);

	// update engine
	SCR_tdst_Link_Value *pValue = SCR_fnp_st_Link_SearchValue(WPG_fnp_Graph_GetLinkTable(), (unsigned long) m_pGraph->GetEngineStruct());
	if (!pValue)
	{
		WPG_fn_vAddGlobalGraph(m_pGraph->GetEngineStruct());
	}

	// update flag
	m_pGraph->fn_vNotifyRestore();
	m_bDeleted = FALSE;

	// update interface
	Graph::fn_pGetDialog()->fn_vRefreshGraphList(NULL);
	Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
	return TRUE;
}

/*===========================================================================
	Delete Graph
===========================================================================*/
RenameGraph::RenameGraph (Graph *pGraph, CString csNewName)
	: CPA_Modif (GRAPH_RENAME_GRAPH, "Rename Graph", FALSE)
{
	// register parameters
	m_pGraph = pGraph;
	m_csOldName = pGraph->GetName();
	m_csNewName = csNewName,
	// init flag
	m_bFirstTime = TRUE;
}

RenameGraph::~RenameGraph	(void)
{

}

BOOL RenameGraph::Do	(void)
{
	// update editor
	m_pGraph->fn_eRename(m_csNewName);
	m_csNewName = m_pGraph->GetName();

	// update engine structure
	WPG_fn_vSetNameOfGraph(m_pGraph->GetEngineStruct(), m_pGraph->GetName());

	// update link table
	SCR_tdst_Link_Value *pValue = SCR_fnp_st_Link_SearchValue(WPG_fnp_Graph_GetLinkTable(), (unsigned long) m_pGraph->GetEngineStruct());
	if (pValue)
	{
		SCR_fn_v_Link_DeleteEntry(WPG_fnp_Graph_GetLinkTable(), pValue);
		WPG_fn_vAddGlobalGraph(m_pGraph->GetEngineStruct());
	}

	// update interface
	if (!m_bFirstTime)
	{
		Graph::fn_pGetDialog()->fn_vRefreshGraphList(m_pGraph);
		Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
	}

	m_bFirstTime = FALSE;
	return TRUE;
}

BOOL RenameGraph::Undo (void)
{
	// update editor
	BOOL bIsLoadingWorld = Graph::GetInterface()->GetInterface()->fn_bIsLoadingWorld();
	Graph::GetInterface()->GetInterface()->SetLoadingWorld(TRUE);
	m_pGraph->fn_eRename(m_csOldName);
	Graph::GetInterface()->GetInterface()->SetLoadingWorld(bIsLoadingWorld);

	// update engine structure
	WPG_fn_vSetNameOfGraph(m_pGraph->GetEngineStruct(), m_pGraph->GetName());

	// update link table
	SCR_tdst_Link_Value *pValue = SCR_fnp_st_Link_SearchValue(WPG_fnp_Graph_GetLinkTable(), (unsigned long) m_pGraph->GetEngineStruct());
	if (pValue)
	{
		SCR_fn_v_Link_DeleteEntry(WPG_fnp_Graph_GetLinkTable(), pValue);
		WPG_fn_vAddGlobalGraph(m_pGraph->GetEngineStruct());
	}

	// update interface
	Graph::fn_pGetDialog()->fn_vRefreshGraphList(m_pGraph);
	Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
	return TRUE;
}

/*===========================================================================
	Insert Node
===========================================================================*/
InsertNode::InsertNode (Graph *pGraph, WayPoint *pNode, BOOL pBlock)
	: CPA_Modif (GRAPH_INSERT_NODE, "Insert Node", pBlock)
{
	// register parameters
	m_pGraph = pGraph;
	m_pNode = pNode;
	// init flag
	m_bInserted = FALSE;
	m_bFirstTime = TRUE;
}

InsertNode::~InsertNode	(void)
{

}

BOOL InsertNode::Do	(void)
{
	// update engine
	WPG_fn_lAddWayPointIfNotExists(m_pGraph->GetEngineStruct(), m_pNode->GetStruct(), 0L); //BART

	// update flag
	m_pGraph->fn_vNotifySave();
	m_bInserted = TRUE;

	// update interface
	if (!m_bFirstTime)
		Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
	Graph::GetInterface()->fn_vSetDisplayedWaypoint(m_pNode);
	Graph::GetInterface()->fn_vUpdateConnections();
	Graph::fn_pGetDialog()->fn_vRefreshNodeList(m_pGraph, m_pNode);

	m_bFirstTime = FALSE;
	return TRUE;
}

BOOL InsertNode::Undo (void)
{
	// update engine
	WPG_fn_lRemoveWayPoint(m_pGraph->GetEngineStruct(), m_pNode->GetStruct());

	// update flag
	m_pGraph->fn_vNotifySave();
	m_bInserted = FALSE;

	// update interface
	Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
	Graph::GetInterface()->fn_vSetDisplayedWaypoint(NULL);
	Graph::GetInterface()->fn_vUpdateConnections();
	Graph::fn_pGetDialog()->fn_vRefreshNodeList(m_pGraph, NULL);

	return TRUE;
}

/*===========================================================================
	Delete Node
===========================================================================*/
DeleteNode::DeleteNode (Graph *pGraph, WayPoint *pNode, BOOL pBlock)
	: CPA_Modif (GRAPH_DELETE_NODE, "Delete Node", pBlock)
{
	// register parameters
	m_pGraph = pGraph;
	m_pNode = pNode;
	// register connections
	m_pGraph->GetConnectionsToWaypoint(pNode, 0, &m_lstConnections);
	// init flag
	m_bDeleted = FALSE;
	m_bFirstTime = TRUE;
}

DeleteNode::~DeleteNode	(void)
{
	if (m_bDeleted)
	{
		while (m_lstConnections.GetCount() > 0)
			delete(m_lstConnections.RemoveTail());
	}
}

BOOL DeleteNode::Do	(void)
{
	CPA_SuperObject *pConnection;
	WP_tdhGraphNode hNode, hNextNode;
	WP_tdhWayPoint	hWayPoint;
	POSITION		pos;
	int				iNode;
	
	// update engine
	hWayPoint = m_pNode->GetStruct();
	LST2_M_DynamicForEachMovingElementOf(&m_pGraph->GetEngineStruct()->m_hListOfNode, hNode, hNextNode, iNode)
	{
		WPG_fn_lRemoveArcFromWayPoint(m_pGraph->GetEngineStruct(), hNode->m_hWayPoint, hWayPoint);
	}
	WPG_fn_lRemoveWayPoint(m_pGraph->GetEngineStruct(), hWayPoint);

	// update editor connections
	for (pConnection = m_lstConnections.GetHeadElement(pos); pConnection;
		 pConnection = m_lstConnections.GetNextElement(pos))
		m_pGraph->fn_vRemoveGraphicConnection(pConnection);

	// update flag
	m_pGraph->fn_vNotifySave();
	m_bDeleted = TRUE;

	// update interface
	m_pNode->GetRealGraphic()->SetLocalColor(E_lc_NoColor);
	m_pNode->GetSymbolicalGraphic()->SetLocalColor(E_lc_NoColor);
	if (!m_bFirstTime)
		Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
	Graph::GetInterface()->fn_vSetDisplayedWaypoint(NULL);
	Graph::GetInterface()->fn_vUpdateConnections();
	Graph::fn_pGetDialog()->fn_vRefreshNodeList(m_pGraph, NULL);

	m_bFirstTime = FALSE;
	return TRUE;
}

BOOL DeleteNode::Undo (void)
{
	CPA_SuperObject *pConnection;
	WP_tdhGraphNode hSrcNode, hDstNode;
	Connection		*pLinkObject;
	POSITION		pos;
	int				iSrcNode, iDstNode;

	// update engine
	WPG_fn_lAddWayPointIfNotExists(m_pGraph->GetEngineStruct(), m_pNode->GetStruct(), 0L); //BART
	for (pConnection = m_lstConnections.GetHeadElement(pos); pConnection;
		 pConnection = m_lstConnections.GetNextElement(pos))
	{
		pLinkObject = (Connection *) pConnection->GetObject();
		iSrcNode = m_pGraph->fn_iGetNodeOfWayPoint(pLinkObject->GetSrcWayPoint()->GetStruct());
		hSrcNode = m_pGraph->fn_hGetNode(iSrcNode);
		iDstNode = m_pGraph->fn_iGetNodeOfWayPoint(pLinkObject->GetDstWayPoint()->GetStruct());
		hDstNode = m_pGraph->fn_hGetNode(iDstNode);
		WPG_fn_vAddArc(hSrcNode, hDstNode, pLinkObject->GetWeight(), pLinkObject->GetCapacity());
	}

	// update editor
	for (pConnection = m_lstConnections.GetHeadElement(pos); pConnection;
		 pConnection = m_lstConnections.GetNextElement(pos))
		m_pGraph->fn_vAddGraphicConnection(pConnection);

	// update flag
	m_pGraph->fn_vNotifySave();
	m_bDeleted = FALSE;

	// update interface
	Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
	Graph::GetInterface()->fn_vSetDisplayedWaypoint(m_pNode);
	Graph::GetInterface()->fn_vUpdateConnections();
	Graph::fn_pGetDialog()->fn_vRefreshNodeList(m_pGraph, m_pNode);

	return TRUE;
}
/*===========================================================================
	Insert Connection
===========================================================================*/
InsertConnection::InsertConnection (Graph *pGraph, WayPoint *pSrcWaypoint, WayPoint *pDstWaypoint, BOOL bUpdate, BOOL pBlock)
	: CPA_Modif (GRAPH_INSERT_ARC, "Insert Connection", pBlock)
{
	int iSrcNode, iDstNode;

	// register parameters
	m_pGraph = pGraph;
	m_pSrcWaypoint = pSrcWaypoint;
	m_pDstWaypoint = pDstWaypoint;
	m_pArrow = NULL;
	// register engine parameters
	iSrcNode = m_pGraph->fn_iGetNodeOfWayPoint(m_pSrcWaypoint->GetStruct());
	m_hSrcNode = m_pGraph->fn_hGetNode(iSrcNode);
	iDstNode = m_pGraph->fn_iGetNodeOfWayPoint(m_pDstWaypoint->GetStruct());
	m_hDstNode = m_pGraph->fn_hGetNode(iDstNode);
	// init flag
	m_bUpdate = bUpdate;
	m_bInserted = FALSE;
	m_bFirstTime = TRUE;
}

InsertConnection::~InsertConnection	(void)
{
	// if necessary, delete graphic connection
	if (!m_bInserted)
		delete m_pArrow;
}

BOOL InsertConnection::Do (void)
{
	// update engine data
	WPG_fn_vAddArc(m_hSrcNode, m_hDstNode, 0, 0);

	// update graphic connection 
	if (!m_pArrow)
		m_pArrow = m_pGraph->GetInterface()->fn_pCreateConnectionGraphicObject(m_pGraph, m_pSrcWaypoint, m_pDstWaypoint, 0, 0);
	m_pGraph->fn_vAddGraphicConnection(m_pArrow);

	// update flag
	m_pGraph->fn_vNotifySave();
	m_bInserted = TRUE;

	// update interface
	if (!m_bFirstTime)
	{
		Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
		Graph::GetInterface()->fn_vSetDisplayedWaypoint(m_pSrcWaypoint);
		Graph::fn_pGetDialog()->fn_vRefreshConnectList(m_pGraph, m_pSrcWaypoint, m_pDstWaypoint);
	}
	else if (m_bUpdate)
		Graph::fn_pGetDialog()->fn_vRefreshConnectList(m_pGraph, m_pSrcWaypoint, m_pDstWaypoint);
	
	Graph::GetInterface()->fn_vUpdateConnections();

	m_bFirstTime = FALSE;
	return TRUE;
}

BOOL InsertConnection::Undo	(void)
{
	// update graphic connection 
	m_pGraph->fn_vRemoveGraphicConnection(m_pArrow);

	// update engine data
	WPG_fn_vRemoveArc(m_hSrcNode, m_hDstNode);

	// update flag
	m_pGraph->fn_vNotifySave();
	m_bInserted = FALSE;

	// update dialog
	Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
	Graph::GetInterface()->fn_vSetDisplayedWaypoint(m_pSrcWaypoint);
	Graph::GetInterface()->fn_vUpdateConnections();
	Graph::fn_pGetDialog()->fn_vRefreshConnectList(m_pGraph, m_pSrcWaypoint, NULL);

	return TRUE;
}

/*===========================================================================
	Delete Connection
===========================================================================*/
DeleteConnection::DeleteConnection (Graph *pGraph, WayPoint *pSrcWaypoint, WayPoint *pDstWaypoint, BOOL pBlock)
	: CPA_Modif (GRAPH_DELETE_ARC, "Delete Connection", pBlock)
{
	int iSrcNode, iDstNode;

	// register parameters
	m_pGraph = pGraph;
	m_pSrcWaypoint = pSrcWaypoint;
	m_pDstWaypoint = pDstWaypoint;
	m_pArrow = m_pGraph->GetGraphicConnection(m_pSrcWaypoint, m_pDstWaypoint);
	// register engine parameters
	iSrcNode = m_pGraph->fn_iGetNodeOfWayPoint(m_pSrcWaypoint->GetStruct());
	m_hSrcNode = m_pGraph->fn_hGetNode(iSrcNode);
	iDstNode = m_pGraph->fn_iGetNodeOfWayPoint(m_pDstWaypoint->GetStruct());
	m_hDstNode = m_pGraph->fn_hGetNode(iDstNode);
	// init flag
	m_bDeleted = FALSE;
	m_bFirstTime = TRUE;
}

DeleteConnection::~DeleteConnection	(void)
{
	// if necessary, delete graphic connection
	if (m_bDeleted)
		delete m_pArrow;
}

BOOL DeleteConnection::Do (void)
{
	// update graphic connection 
	m_pGraph->fn_vRemoveGraphicConnection(m_pArrow);

	// update engine data
	WPG_fn_vRemoveArc(m_hSrcNode, m_hDstNode);

	// update flag
	m_pGraph->fn_vNotifySave();
	m_bDeleted = TRUE;

	// update dialog
	if (!m_bFirstTime)
	{
		Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
		Graph::GetInterface()->fn_vSetDisplayedWaypoint(m_pSrcWaypoint);
	}
	Graph::GetInterface()->fn_vUpdateConnections();
	Graph::fn_pGetDialog()->fn_vRefreshConnectList(m_pGraph, m_pSrcWaypoint, NULL);

	m_bFirstTime = FALSE;
	return TRUE;
}

BOOL DeleteConnection::Undo	(void)
{
	Connection *pLinkObject = (Connection *) m_pArrow->GetObject();
	
	// update engine data
	WPG_fn_vAddArc(m_hSrcNode, m_hDstNode, pLinkObject->GetWeight(), pLinkObject->GetCapacity());

	// update graphic connection 
	m_pGraph->fn_vAddGraphicConnection(m_pArrow);

	// update flag
	m_pGraph->fn_vNotifySave();
	m_bDeleted = FALSE;

	// update dialog
	Graph::GetInterface()->fn_vSelectGraph(m_pGraph);
	Graph::GetInterface()->fn_vSetDisplayedWaypoint(m_pSrcWaypoint);
	Graph::GetInterface()->fn_vUpdateConnections();
	Graph::fn_pGetDialog()->fn_vRefreshConnectList(m_pGraph, m_pSrcWaypoint, m_pDstWaypoint);

	return TRUE;
}




//End Shaitan Correction }
