// Global functions 
///////////////////////////
#include "stdafx.h"

#include "EDACGlob.hpp"

#include "ACP_Base.h"

#include "TAC.h"
#include "TFa.h"
#include "DPT.h"

#include "EdtList.hpp"
#include "CPA_dato.hpp"
#include "CPA_nll.hpp"

#include "_AInterf.hpp"

#include "x:\cpa\main\inc\_EditID.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BOOL FirstLoad = FALSE;

//**************************************************************************
void fn_vCheckAndChangeName(CString &r_csName)
{
	if ( !r_csName.IsEmpty() )
	{
		long c_lI;
		for (c_lI = 0; c_lI < r_csName.GetLength(); c_lI++ )
		{
			if	(		(r_csName.Mid(c_lI, 1) == " ")
					||	(r_csName.Mid(c_lI, 1) == "-")
					)
				r_csName.SetAt(c_lI, '_');
		}
	}
}

#define C_CHAR_UNPASSED	'.'
#define C_CHAR_PASSED		'|'
#define C_BAR_LENGTH		50

//**************************************************************************
// Only displays message in the status bar if 'cPercentage = -1'
void EDACTORS_fn_vGiveProgressInfo(CString csMessage, char cPercentage /*= -1*/)
{
	CString csTrueMessage = csMessage;

	if ( cPercentage > 0 )
	{
		CString csBar;
		short c_wI;
		for (c_wI = 0; c_wI < C_BAR_LENGTH; c_wI ++)
		{
			csBar += (c_wI < ((cPercentage * C_BAR_LENGTH)/100)) ? C_CHAR_PASSED : C_CHAR_UNPASSED;
		}

		csTrueMessage.Format("%s %s", csMessage, csBar);
	}
	if(FirstLoad == FALSE)
		M_GetMainWnd()->UpdateStatus((char *)LPCTSTR(csTrueMessage), C_STATUSPANE_INFOS, C_STATUS_NORMAL);
	else
		g_oBaseFrame.mfnv_UpdateTitle((char *)LPCTSTR(csTrueMessage));
}

//**************************************************************************
BOOL EDACTORS_fn_bIsFileInCommon(CString _csFileName)
{
	_csFileName.MakeLower();
	return ( _csFileName.Find("_common") != -1 );
}

//***********************************************************************
// m_wGetIndexOfName
// finds the the index of a name in a name list, and the model using the name list
// wParam can have the following values :
//      C_uiActor_GetANameList
//      C_uiActor_CreateANewObjectList
//      C_uiActor_DeleteAnObjectList
//      C_uiActor_GetAnObjectList
//      C_uiActor_SetAnObjectList
//      C_uiActor_SetAnObjectInObjectList
//***********************************************************************
void fn_vSplitName(CString csName,CString &csFamily,CString &csNameList,CString &csObject)
{
	CString csTemp;
	int		iPos;
	
	// extract object
	iPos = csName.ReverseFind('@');
	if (iPos != -1)
	{
		csObject = csName.Right(csName.GetLength()-iPos-1);
		csTemp = csName.Left(iPos);
	}
	else
	{
		csObject = csName;
		csTemp = "";
	}

	// extract name list
	iPos = csTemp.ReverseFind('@');
	if (iPos != -1)
	{
		csNameList = csTemp.Right(csTemp.GetLength()-iPos-1);
		csFamily = csName.Left(iPos);
	}
	else
	{
		csNameList = csTemp;
		csFamily = "";
	}
}

//**************************************************************************
short m_wGetIndexOfName(CString _csNameOfObject, CPA_Actor *&r_pModel,CString &rcsObjectType)
{
	//CPA_BaseObjectList *p_oObjectList = GetMainWorld()->fn_p_oGetOriginalObjectList(C_szObjectNameType);
	CString csFamilyName, csNameListName, csObjectName;
	//split the name to get separately the reachable names of the name, list, and family
	//p_oObjectList->fn_vSplitName(_csNameOfObject, &csNameListName, &csObjectType, &csObjectName);
	//p_oObjectList->fn_vSplitName(csNameListName, &csFamilyName, &csObjectType, &csNameListName);
	fn_vSplitName(_csNameOfObject,csFamilyName,csNameListName,csObjectName);
	
	rcsObjectType="";
	r_pModel = NULL;
	long lNbFamilies, lNbNameLists, lNbNames;
	class CPA_List<class CPA_BaseObject> oObList;
	CPA_BaseObject *p_oFamily, *p_oNameList, *p_oName;

	//if the family is specified, find it
	p_oFamily = NO_CRITERIA;
	if ( !csFamilyName.IsEmpty() )
	{
		lNbFamilies = g_pclInterface->GetMainWorld()->fn_lFindObjects(&oObList, csFamilyName, C_szFamilyTypeName, NO_CRITERIA);
		if ( lNbFamilies <= 0 )
		{
			oObList.RemoveAll();
			return C_wNoFamilyFound;
		}
		if ( lNbFamilies > 1 )
		{
			oObList.RemoveAll();
			return C_wMultipleFamiliesFound;
		}
		p_oFamily = oObList.GetHead()/*->GetObject()*/;
	}

	//if the name list is specified, find it
	oObList.RemoveAll();
	p_oNameList = NO_CRITERIA;
	if ( !csNameListName.IsEmpty() )
	{
		char *a_szTypes[] =
		{
			C_szZDeNameListType,
			C_szZDmNameListType,
			C_szZDdNameListType,
			C_szZDrNameListType,
			C_szWayNameListType,
		};
		short wNbTypes = sizeof(a_szTypes) / sizeof(char *);
		lNbNameLists = 0;
		for ( short wIndex = 0; wIndex < wNbTypes; wIndex ++ )
		{
			//find a name list owned by the specified family, if relevant
			long lNb = g_pclInterface->GetMainWorld()->fn_lFindObjects(&oObList, csNameListName, a_szTypes[wIndex], p_oFamily);
			lNbNameLists =+ lNb;
			if ( lNb == 1 )
				p_oNameList = oObList.GetHead()/*->GetObject()*/;
		}
		if ( lNbNameLists <= 0 )
		{
			oObList.RemoveAll();
			return C_wNoNameListFound;
		}
		if ( lNbNameLists > 1 )
		{
			oObList.RemoveAll();
			return C_wMultipleNameListsFound;
		}
	}

	//try to find the object name
	oObList.RemoveAll();
	if ( !csObjectName.IsEmpty() )
	{
		//find a name that is used by the specified name list, or without criteria
		lNbNames = g_pclInterface->GetMainWorld()->fn_lFindObjects(&oObList, csObjectName, C_szObjectNameType, p_oNameList);
		if ( lNbNames <= 0 )
		{
			oObList.RemoveAll();
			return C_wNoNameFound;
		}
		if ( lNbNames > 1 )
		{
			oObList.RemoveAll();
			return C_wMultipleNamesFound;
		}
		p_oName = oObList.GetHead()/*->GetObject()*/;
	}
	else
		return C_wNoNameFound;

	oObList.RemoveAll();
	//here only one name was found with the specified information
	//lets find out the model using the name list referencing the name

	//Gets type
	rcsObjectType=p_oName->GetOwner()->GetType();

	//Gets model
	//CString	csNameList=p_oName->GetOwner()->GetName();
	int iNbReferencingFathers;
	CList< CPA_BaseObject *, CPA_BaseObject *> oFatherList;
	p_oNameList=p_oName->GetOwner();
	iNbReferencingFathers = g_oCoherenceManager.m_fn_iGetFatherList(p_oNameList, &oFatherList);
	if ( iNbReferencingFathers == 1 )
		r_pModel = ((CPA_Actor *) oFatherList.GetHead());
	oFatherList.RemoveAll();
	return ((CPA_tdoNameList *) p_oNameList)->m_wGetIndexOfName(csObjectName);

/*	int iNbReferencingFathers;
	CList< CPA_SaveObject *, CPA_SaveObject *> oFatherList;

	if ( p_oNameList == NO_CRITERIA ) //if no name list was specified
	{
		//find the name lists that use it
		int iNbReferencingFathers = g_oCoherenceManager.m_fn_iGetFatherList(p_oName, &oFatherList);
		if ( iNbReferencingFathers <= 0 )
		{
			oFatherList.RemoveAll();
			return C_wNoNameListFound;
		}
		if ( iNbReferencingFathers > 1 )
		{
			oFatherList.RemoveAll();
			return C_wMultipleNameListsFound;
		}
		//the only valid result is 1!
		p_oNameList = oFatherList.GetHead();
	}

	//find which models use this name list
	oFatherList.RemoveAll();
	iNbReferencingFathers = g_oCoherenceManager.m_fn_iGetFatherList(p_oNameList, &oFatherList);
	if ( iNbReferencingFathers == 1 )
		r_pModel = (EdActors_EditorActorModel *) ((CPA_Actor *) oFatherList.GetHead())->m_fn_pclGetEditorActor();
	oFatherList.RemoveAll();
	return ((CPA_tdoNameList *) p_oNameList)->m_wGetIndexOfName(csObjectName);*/
}
