// EDACDgML.cpp : implementation file
/////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "EDACDgML.hpp"

#include "_AInterf.hpp"
#include "EDACStrg.hpp"
#include "EDACGlob.hpp"

//External modules
#include "TFa.h"
#include "Dpt.h"
#include "ErO.h"
//End of External modules

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
    #include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT


#define EDAC_C_TreeIconSize		16


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

/////////////////////////////////////////////////////////////////////////////
// EdActors_Dialog_LoadModels dialog

BEGIN_MESSAGE_MAP(EdActors_Dialog_LoadModels, CDialog)
	//{{AFX_MSG_MAP(EdActors_Dialog_LoadModels)
	ON_BN_CLICKED(IDC_RADIO_LOAD_ALL_MODELS, OnRadioLoadAllModels)
	ON_BN_CLICKED(IDC_RADIO_LOAD_SELECT_MODELS, OnRadioLoadSelectModels)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_MODELS, OnSelchangedTreeModels)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//***************************************************************************
EdActors_Dialog_LoadModels::EdActors_Dialog_LoadModels(CWnd* pParent /*=NULL*/)
													: CDialog(EdActors_Dialog_LoadModels::IDD, &g_oBaseFrame)
{
	//{{AFX_DATA_INIT(EdActors_Dialog_LoadModels)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

//***************************************************************************
void EdActors_Dialog_LoadModels::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(EdActors_Dialog_LoadModels)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// EdActors_Dialog_LoadModels message handlers

//***************************************************************************
BOOL EdActors_Dialog_LoadModels::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	AfxGetApp()->DoWaitCursor(1);
	
	//Prepares Icons for tree
	m_pri_clImageListForTree.Create(EDAC_C_TreeIconSize, EDAC_C_TreeIconSize, ILC_MASK, 0, 3);

	m_pri_iIndexInImageListForFamilies = m_pri_clImageListForTree.Add( LoadIcon(g_pclInterface->GetDLLIdentity()->hModule, MAKEINTRESOURCE(IDI_CAR_TREE_FAMILIES)) );
	m_pri_iIndexInImageListForModels = m_pri_clImageListForTree.Add( LoadIcon(g_pclInterface->GetDLLIdentity()->hModule, MAKEINTRESOURCE(IDI_CAR_TREE_MODELS)) );
	m_pri_iIndexInImageListForUnloadedFamilies = m_pri_clImageListForTree.Add( LoadIcon(g_pclInterface->GetDLLIdentity()->hModule, MAKEINTRESOURCE(IDI_CAR_TREE_FAMILIES_UNSELECTED)) );
	m_pri_iIndexInImageListForUnloadedModels = m_pri_clImageListForTree.Add( LoadIcon(g_pclInterface->GetDLLIdentity()->hModule, MAKEINTRESOURCE(IDI_CAR_TREE_MODELS_UNSELECTED)) );
	
	CTreeCtrl *pclTreeCtrl = (CTreeCtrl *)GetDlgItem(IDC_TREE_MODELS);
	pclTreeCtrl->SetImageList(&m_pri_clImageListForTree, TVSIL_NORMAL);

	//Fills Tree
	m_pri_fn_vScanDirectoryForUnloadedModels(fn_szGetFamiliesDataPath());
	
	((CButton *)GetDlgItem(IDC_RADIO_LOAD_SELECT_MODELS))->SetCheck(TRUE);
	((CButton *)GetDlgItem(IDC_RADIO_LOAD_ALL_MODELS))->SetCheck(TRUE);
	OnRadioLoadAllModels();

	AfxGetApp()->DoWaitCursor(-1);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//***************************************************************************
void EdActors_Dialog_LoadModels::OnRadioLoadAllModels() 
{
	GetDlgItem(IDC_TREE_MODELS)->EnableWindow(FALSE);
}

//***************************************************************************
void EdActors_Dialog_LoadModels::OnRadioLoadSelectModels() 
{
	GetDlgItem(IDC_TREE_MODELS)->EnableWindow(TRUE);
}

//***************************************************************************
void EdActors_Dialog_LoadModels::OnOK() 
{
	
	CDialog::OnOK();
}

//***************************************************************************
void EdActors_Dialog_LoadModels::OnSelchangedTreeModels(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	
	CTreeCtrl *pclTreeCtrl = (CTreeCtrl *)GetDlgItem(IDC_TREE_MODELS);
	if ( pclTreeCtrl->GetParentItem(pNMTreeView->itemNew.hItem) == NULL )
	{
		//It's a Family
		pclTreeCtrl->SetItemImage(pNMTreeView->itemNew.hItem, m_pri_iIndexInImageListForFamilies, m_pri_iIndexInImageListForFamilies);
	}
	else
	{
		//It's a Model
		pclTreeCtrl->SetItemImage(pNMTreeView->itemNew.hItem, m_pri_iIndexInImageListForModels, m_pri_iIndexInImageListForModels);
	}

	*pResult = 0;
}

//***************************************************************************
// Copyright Xavier Billault !!
void EdActors_Dialog_LoadModels::m_pri_fn_vScanDirectoryForUnloadedModels(CString csDirectory)
{
	HANDLE h;
	WIN32_FIND_DATA	hData;
	
	CString csName;
	CString csModelName;
	CString csFamilyName;
	short wIndex;
	
	CTreeCtrl *pclTreeCtrl = (CTreeCtrl *)GetDlgItem(IDC_TREE_MODELS);
	ERROR_ASSERT( pclTreeCtrl != NULL );

	h = FindFirstFile(csDirectory + "\\*", &hData);
	if( h != INVALID_HANDLE_VALUE )
	{
		do
		{
			if(		( CString(hData.cFileName) != "." )
				&&	( CString(hData.cFileName) != ".." )
				)
			{
				if( hData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
					m_pri_fn_vScanDirectoryForUnloadedModels(csDirectory + "\\" + hData.cFileName);
				else
				{
					CFile		cfSrcFile;
					CString	csSrcFile = csDirectory + "\\" + hData.cFileName;
					
					//Looks if extension is a Model's one
					if ( g_csModelExtension.CompareNoCase(csSrcFile.Right(g_csModelExtension.GetLength())) == 0 )
					{

						//Extracts Model' Name
						wIndex = csSrcFile.ReverseFind('\\');
						if ( wIndex != -1 )
						{
							csName = csSrcFile.Left(wIndex);
							wIndex = csName.ReverseFind('\\');
							csModelName = csName.Right(csName.GetLength() - wIndex - 1);
						}
						else
							csModelName = "< Unknown >";

						//Extracts Model' Family Name
						wIndex = csSrcFile.ReverseFind('\\');
						if ( wIndex != -1 )
						{
							csName = csSrcFile.Left(wIndex);
							wIndex = csName.ReverseFind('\\');
							if ( wIndex != -1 )
							{
								csName = csName.Left(wIndex);
								wIndex = csName.ReverseFind('\\');

								csFamilyName = csName.Right(csName.GetLength() - wIndex - 1);
							}
							else
								csFamilyName = "< Unknown >";
						}
						else
							csFamilyName = "< Unknown >";

						//Looks if this Model is loaded
						BOOL bModelFound = FALSE;
						CPA_Actor *pclActor;
						Position ModelPos = g_pclInterface->m_clDocument.m_clModelsList.GetHeadPosition();
						while ( (ModelPos != 0) && (!bModelFound) )
						{
							pclActor = (CPA_Actor *)g_pclInterface->m_clDocument.m_clModelsList.GetNext(ModelPos);

							//Compares Models names AND Families names
							if ( pclActor->m_pub_fn_pclGetFamily() != NULL )
								bModelFound =	(		( pclActor->GetName().CompareNoCase(csModelName) == 0 )
															&&	( pclActor->m_pub_fn_pclGetFamily()->GetName().CompareNoCase(csFamilyName) == 0 )
															);
						}

						if ( !bModelFound )
						{
							//Looks in the Tree if Family node exists
							BOOL bFamilyFoundInTree = FALSE;
							HTREEITEM hCurrentItemForFamily = pclTreeCtrl->GetNextItem(NULL, TVGN_ROOT);

							while ( (hCurrentItemForFamily != NULL) && (!bFamilyFoundInTree) )
							{
								bFamilyFoundInTree = ( csFamilyName.Compare(pclTreeCtrl->GetItemText(hCurrentItemForFamily)) == 0 );

								if ( !bFamilyFoundInTree )
									hCurrentItemForFamily = pclTreeCtrl->GetNextItem(hCurrentItemForFamily, TVGN_NEXT);
							}

							if ( !bFamilyFoundInTree )
							{
								hCurrentItemForFamily = pclTreeCtrl->InsertItem(csFamilyName, TVI_ROOT, TVI_SORT);
								pclTreeCtrl->SetItemImage(hCurrentItemForFamily, m_pri_iIndexInImageListForUnloadedFamilies, m_pri_iIndexInImageListForUnloadedFamilies);
							}

							//Adds in Tree
							HTREEITEM hAddedItemForModel = pclTreeCtrl->InsertItem(csModelName, hCurrentItemForFamily, TVI_SORT);
							pclTreeCtrl->SetItemImage(hAddedItemForModel, m_pri_iIndexInImageListForUnloadedModels, m_pri_iIndexInImageListForUnloadedModels);

/*							//Displays a message (in status bar)
							CString csMessage = "Actors Editor prepares the Model '" + csModelName + "' for load";
							EDACTORS_fn_vGiveProgressInfo(csMessage, -1);

							BOOL bIsLoadingWorld = g_pclInterface->GetInterface()->fn_bIsLoadingWorld();
							g_pclInterface->GetInterface()->SetLoadingWorld(TRUE);

							//Gets the Editor Family
							CPA_Family *pclBaseFamily = (CPA_Family *)g_pclInterface->GetMainWorld()->fn_p_oFindObject(csFamilyName, C_szFamilyTypeName);

							//Constructs the new CPA_Actor
							CPA_Actor *pclNewModel = new CPA_Actor(	NULL,
																											NULL,
																											g_c_csActorModelType,
																											NULL,
																											pclBaseFamily,		//Family
																											NULL,
																											csModelName,
																											FALSE
																										);
							pclNewModel->SetAvailable(FALSE);
							pclNewModel->m_fn_vSetFamilyName(csFamilyName);

							g_pclInterface->m_clDocument.m_clModelsList.fn_bAddObject(pclNewModel);

							g_pclInterface->GetInterface()->SetLoadingWorld(bIsLoadingWorld);
*/						}
					}
				}
			}
		} while ( FindNextFile(h,&hData) );
		
		FindClose(h);
	}
}

