// MegatronDoc.cpp : implementation of the CMegatronDoc class
//

#include "stdafx.h"
#include "Megatron.h"

#include "MegatronDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMegatronDoc

IMPLEMENT_DYNCREATE(CMegatronDoc, CDocument)

BEGIN_MESSAGE_MAP(CMegatronDoc, CDocument)
	//{{AFX_MSG_MAP(CMegatronDoc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMegatronDoc construction/destruction

CMegatronDoc::CMegatronDoc()
{
	m_pFileNode = NULL;
}

CMegatronDoc::~CMegatronDoc()
{
}
/*
int ReadBuildFile(CString filename,CMapStringToPtr *pMap);

int ReadBuildFile(Stuff::NotationFile *notefile,CMapStringToPtr *pMap)
{
	int count = 0;
	Stuff::NotationFile::PageIterator *pages = notefile->MakePageIterator();
	Register_Object(pages);
	Stuff::Page *page;
	while (page = pages->ReadAndNext())
	{
		CString pagename = page->GetName();
		if (pagename.GetLength() ==0)
		{
			Stuff::Page::NoteIterator *notes = page->MakeNoteIterator();
			Stuff::Note *note;
			while (note = notes->ReadAndNext())
			{
				count++;
				CString notename = note->GetName();
				{
					Stuff::NotationFile entryfile;
					note->GetEntry(&entryfile);
					{
						CString entryfilename = entryfile.GetFileName();
						count += ReadBuildFile(&entryfile,pMap);
					}
				}

				notename = "content\\"+notename;
				count += ReadBuildFile(notename,pMap);
			}
			delete notes;
		}
	}
	Unregister_Object(pages);
	delete pages;
	return count;
}

int ReadBuildFile(CString filename,CMapStringToPtr *pMap)
{
	Stuff::NotationFile *pFile = new Stuff::NotationFile(filename);
	(*pMap)[filename] = pFile;
	int count = 1 + ReadBuildFile(pFile,pMap);
	return count;
}
*/
BOOL CMegatronDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	CString filename = "content\\Mechwarrior4.build";
	//int count = ReadBuildFile(filename,&m_mapBuildFiles);
	m_pFileNode = new CFileNode(filename);

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CMegatronDoc serialization

void CMegatronDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
	}
	else
	{
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMegatronDoc diagnostics

#ifdef _DEBUG
void CMegatronDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMegatronDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMegatronDoc commands

void CMegatronDoc::OnCloseDocument() 
{
/*	POSITION pos = m_mapBuildFiles.GetStartPosition();
	while (pos)
	{
		CString strKey;
		LPVOID lpValue;
		m_mapBuildFiles.GetNextAssoc(pos,strKey,lpValue);
		if (lpValue)
		{
			Stuff::NotationFile *pFile = (Stuff::NotationFile*)lpValue;
			Check_Object(pFile);
			delete pFile;
		}
	}
*/
	if (m_pFileNode)
	{
		delete m_pFileNode;
		m_pFileNode = NULL;
	}
	
	POSITION pos = m_mapHintFiles.GetStartPosition();
	while (pos)
	{
		CString strKey;
		LPVOID lpValue;
		m_mapHintFiles.GetNextAssoc(pos,strKey,lpValue);
		if (lpValue)
		{
			Stuff::NotationFile *pFile = (Stuff::NotationFile*)lpValue;
			Check_Object(pFile);

			if (pFile->IsChanged())
			{
				CString str = pFile->GetFileName();
				if (IDYES == MessageBox(NULL,str+" has changed.  Save changes?","Texture Hint File",MB_YESNO))
				{
					pFile->Save();
				}
				else
				{
					pFile->IgnoreChanges();
				}
			}
			delete pFile;
		}
	}

	CDocument::OnCloseDocument();
}

Stuff::NotationFile *CMegatronDoc::GetHintFile(CString name)
{
	LPVOID value;
	name.MakeLower();
	if (m_mapHintFiles.Lookup(name,value))
	{
		Stuff::NotationFile *pFile = (Stuff::NotationFile *)value;
		Check_Object(pFile);
		return pFile;
	}
	Stuff::NotationFile *pFile = new Stuff::NotationFile(name);
	Check_Object(pFile);
	m_mapHintFiles[name] = pFile;
	return pFile;
}