// MultitronDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Multitron.h"
#include "FileOpenDlg.h"
#include "MainFrm.h"

#include <GameOs\TooLOS.hpp>
#include <imagelib\image.h>
#include <direct.h>

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

/////////////////////////////////////////////////////////////////////////////
// CFileOpenDlg dialog


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

	m_pFileNode = NULL;
}

CFileOpenDlg::~CFileOpenDlg()
{
	if (m_pFileNode) {
		delete m_pFileNode;
		m_pFileNode = NULL;
	}
}

void CFileOpenDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileOpenDlg)
	DDX_Control(pDX, IDC_TREE_FILES, m_wndTreeFiles);
	DDX_Control(pDX, IDC_HINTFILENAME, m_wndHintFileName);
	DDX_Control(pDX, IDC_BUILDFILENAME, m_wndBuildFileName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFileOpenDlg, CDialog)
	//{{AFX_MSG_MAP(CFileOpenDlg)
	ON_WM_DRAWITEM()
	ON_WM_CONTEXTMENU()
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_FILES, OnSelchangedTreeFiles)
	ON_COMMAND(IDC_FILEBROWSE, OnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileOpenDlg message handlers

/*******************************************************************************
/* function name: OnInitDialog
/* description: Open content\Mechwarrior4.build if none is opened
/*******************************************************************************/
BOOL CFileOpenDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	if (!m_pFileNode) {
		CFileNode *pNode = new CFileNode("content\\Mechwarrior4.build");
		m_pFileNode = pNode;
	}
	
	AddTreeFileItem(m_pFileNode);

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

/*******************************************************************************
/* function name: OnSelchangedTreeFiles
/* description: User selects a node in the build file tree, look in the selected 
/*	build file for the entry "texturepool" to see if there is a hint file referenced
/*******************************************************************************/
void CFileOpenDlg::OnSelchangedTreeFiles(NMHDR* pNMHDR, LRESULT* pResult) 
{
	m_wndHintFileName.SetWindowText("");

	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	CFileNode *pNode = (CFileNode *)pNMTreeView->itemNew.lParam;
	if (pNode) {

		m_wndBuildFileName.SetWindowText(pNode->strName);
		Stuff::NotationFile::PageIterator *pages = pNode->pFile->MakePageIterator();
		Stuff::Page *page;
		while (NULL != (page = pages->ReadAndNext()))
		{
			const char *entry;
			if (page->GetEntry("texturepool",&entry))
			{
				CString filename("content\\");
				filename+=entry;
				m_wndHintFileName.SetWindowText(filename);
				break;
			}
		}
		delete pages;
	}

	m_wndHintFileName.GetWindowText(m_HintFileName);
	GetDlgItem(IDOK)->EnableWindow(m_HintFileName.GetLength());
}

/*******************************************************************************
/* function name: AddTreeFileItem
/* description: Recursively add a file node to the the tree view
/*******************************************************************************/
int CFileOpenDlg::AddTreeFileItem(CFileNode *pNode,HTREEITEM hParent)
{
	int count = 0;
	if (pNode)
	{
		count++;
		CString name = pNode->strName;
		if (TVI_ROOT != hParent)
		{
			char fname[MAX_PATH];
			_splitpath(name,NULL,NULL,fname,NULL);
			name = fname;
		}
		HTREEITEM hItem = m_wndTreeFiles.InsertItem(name,hParent);
		m_wndTreeFiles.SetItemData(hItem,(DWORD)pNode);
		POSITION pos = pNode->listChildren.GetHeadPosition();
		while (pos)
		{
			CFileNode *pChild = (CFileNode *)pNode->listChildren.GetNext(pos);
			count += AddTreeFileItem(pChild,hItem);
		}
	}
	return count;
}

/*******************************************************************************
/* function name: OnBrowse
/* description: User browse for a build file with a file open dialog
/*******************************************************************************/
void CFileOpenDlg::OnBrowse()
{
	CFileDialog dlg(true, NULL, "mechwarrior4.build", 0, "build Files (*.build)|*.build");
	if (IDCANCEL != dlg.DoModal()) {
		CString FileName = dlg.GetPathName();
		m_wndBuildFileName.SetWindowText(FileName);

		char path[MAX_PATH];
		_splitpath(FileName, NULL, path, NULL, NULL);
		CString strPath(path);
		strPath.MakeLower();
		chdir(strPath.Left(strPath.Find("content")));

		CFileNode *pNode = new CFileNode(FileName);
		if (m_pFileNode)
			delete m_pFileNode;
		
		m_pFileNode = pNode;
		m_wndTreeFiles.DeleteAllItems();
		AddTreeFileItem(m_pFileNode);
	}
}
