/*=========================================================================
*
* NameMngr.cpp :  Name Manager : Implementation file.
*
*
* Version			1.0
* Creation date
* Revision date
*
* Shaitan
*=======================================================================*/
#include "stdafx.h"

#include "itf/customid.h"
#ifdef ACTIVE_EDITOR
#include "acp_base.h"

//#include "itf/EDTBase.hpp"

#include "itf/CPAInter.hpp"
#include "itf/NameMngr.hpp"

#include "DPT.h"

CPA_NameManager g_oNameManager;

//#################################################################################
// NAME CONFLICT
//#################################################################################
/*===========================================================================
	Constructor (initial object)
=========================================================================*/
CPA_NameConflict::CPA_NameConflict (CPA_BaseObject *pInitial, CPA_BaseObject *pRenamed, 
									const CString csInitialName, tdeMissingCriteria eConflict)
{
	// init parameters
	m_pInitialObject = pInitial;
	m_pRenamedObject = pRenamed;
	m_csInitialName = csInitialName;
	m_eConflict = eConflict;
}

/*===========================================================================
	Destructor 
=========================================================================*/
CPA_NameConflict::~CPA_NameConflict (void)
{

}

//#################################################################################
// CONSTRUCTOR
//#################################################################################
/*===========================================================================
	Constructor - init the list
=========================================================================*/
CPA_NameManager::CPA_NameManager ( )
{

}

/*===========================================================================
	Destructor - 
=========================================================================*/
CPA_NameManager::~CPA_NameManager (void)
{

}

//#################################################################################
// CONFLICTS
//#################################################################################
/*===========================================================================
	Register a name conflict
=========================================================================*/
void CPA_NameManager::fn_vRegisterConflict (CPA_BaseObject *pObject, const CString csInitialName, tdeMissingCriteria eConflict)
{
	CPA_BaseObjectList	*pListObject;
	CPA_BaseObjectList	*pListInvalid;
	CPA_BaseObject		*pFirstObject;
	CPA_BaseObject		*pOwner;
	CString				csTypeName;
	
	// no object => no conflict
	if (!pObject)
		return;

	// init parameters
	pListObject = pObject->GetBaseObjectList();
	pListInvalid = pObject->GetInvalidObjectList();
	csTypeName = pObject->GetType();
	pOwner = pObject->GetOwner();

	// switch conflict type to find object
	switch (eConflict)
	{
		case E_mc_TypeUnicity:
			pFirstObject = pListObject->fn_p_oFindObject(csInitialName, csTypeName, NO_CRITERIA);
			if (!pFirstObject)
				pFirstObject = pListInvalid->fn_p_oFindObject(csInitialName, csTypeName, NO_CRITERIA);
			break;

		case E_mc_OwnerUnicity:
			pFirstObject = pListObject->fn_p_oFindObject(csInitialName, csTypeName, pOwner);
			if (!pFirstObject)
				pFirstObject = pListInvalid->fn_p_oFindObject(csInitialName, csTypeName, pOwner);
			break;

		case E_mc_CustomUnicity:
			pFirstObject = NULL;
			break;
		default:
			pFirstObject = NULL;
	}

	m_oListOfNameConflict.AddTail (new CPA_NameConflict(pFirstObject, pObject, csInitialName, eConflict));
}


/*===========================================================================
	Register name conflict file
=========================================================================*/
void CPA_NameManager::fn_vWriteNameConflicts (void)
{
	CPA_NameConflict	*pElem;
	CPA_BaseObject		*pObject;
	POSITION			pos;
	CString				csInitialName;
	CString				csTime;
	CTime				oTime = CTime::GetCurrentTime();
	FILE				*pFile;
	char				szFileName[MAX_PATH];
	char				szText[MAX_PATH];

	// Compute modification file name (last and new)
	strcpy(szFileName, fn_szGetGameDataPath());
	strcat(szFileName, "\\NameMngr.txt");

	// Append modification list
	pFile = fopen(szFileName, "at");
	if (!pFile)
		return;

	// Date
	csTime.Format("%d/%d/%d %d:%d:%d", oTime.GetDay(), oTime.GetMonth(), oTime.GetYear(),
		oTime.GetHour(), oTime.GetMinute(), oTime.GetSecond());
	sprintf(szText, "\nCONFLICTS : %s\n", csTime);
	fputs(szText, pFile);

	for (pElem = m_oListOfNameConflict.GetHeadElement(pos); pElem;
		 pElem = m_oListOfNameConflict.GetNextElement(pos))
	{
		fputs("\n*********** Conflict *************\n\n", pFile);
		csInitialName = pElem->m_csInitialName;
		// conflict type
		switch (pElem->m_eConflict)
		{
			case E_mc_TypeUnicity:
				sprintf(szText, "CONFLICT ON : (Name + Type) unicity criteria \n");
				break;
			case E_mc_OwnerUnicity:
				sprintf(szText, "CONFLICT ON : (Name + Type + Owner) unicity criteria \n");
				break;
			case E_mc_CustomUnicity:
				sprintf(szText, "CONFLICT ON : custom unicity criteria\n");
				break;
		}
		fputs(szText, pFile);

		// renamed object
		fputs("RENAMED OBJECT :\n", pFile);
		pObject = pElem->m_pRenamedObject;
		// type
		sprintf(szText, "Type	: %s \n", pObject->GetType());
		fputs(szText, pFile);
		if (pObject->GetOwner())
		{
			sprintf(szText, "Owner   : %s \n", pObject->GetOwner()->GetName());
			fputs(szText, pFile);
		}
		// section
		if (pObject->fn_bCanBeSaved())
		{
			sprintf(szText, "Section : %s \n", ((CPA_SaveObject *) pObject)->fn_csComputeCompleteSectionName(csInitialName));
			fputs(szText, pFile);
		}
		else
		{
			sprintf(szText, "OldName : %s \n", csInitialName);
			fputs(szText, pFile);
		}

		// initial object
		if (pElem->m_pInitialObject)
		{
			fputs("CONFLICT WITH :\n", pFile);
			pObject = pElem->m_pInitialObject;
			// type
			sprintf(szText, "Type	: %s \n", pObject->GetType());
			fputs(szText, pFile);
			// owner
			if (pObject->GetOwner())
			{
				sprintf(szText, "Owner   : %s \n", pObject->GetOwner()->GetName());
				fputs(szText, pFile);
			}
			// section
			if (pObject->fn_bCanBeSaved())
			{
				sprintf(szText, "Section : %s \n", ((CPA_SaveObject *) pObject)->GetCompleteSectionName());
				fputs(szText, pFile);
			}
			else
			{
				sprintf(szText, "Name    : %s \n", csInitialName);
				fputs(szText, pFile);
			}
		}
	}
	fputs("\n##########################################################\n", pFile);
	fclose(pFile);
}

#endif // ACTIVE_EDITOR
