// ProjectDoc.cpp : implementation of the CProjectDoc class
//

#include "stdafx.h"
#include "AsCfgBuilder.h"

#include "ProjectDoc.h"
#include "XmlClassInfo.h"
#include "XmlBaseElement.h"
#include "XmlElementsFactory.h"

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

/////////////////////////////////////////////////////////////////////////////
// CProjectDoc

IMPLEMENT_DYNCREATE(CProjectDoc, CDocument)

BEGIN_MESSAGE_MAP(CProjectDoc, CDocument)
	//{{AFX_MSG_MAP(CProjectDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProjectDoc construction/destruction

CProjectDoc::CProjectDoc():
	displayRootNode_(true)
{
	// TODO: add one-time construction code here

}

CProjectDoc::~CProjectDoc()
{
}

BOOL CProjectDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	CXmlElementsFactory::ResetIdCounter();
	project_.Clear();
	selectedItemId_ = project_.GetRootElement()->GetId();

	UpdateAllViews(NULL, enUpdateViewReason_DocLoaded);

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CProjectDoc serialization

void CProjectDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CProjectDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CProjectDoc commands

BOOL CProjectDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	bool ret;
	cmString tmp;

	tmp = lpszPathName;
	CoInitialize(NULL);
	project_.Clear();
	ret = project_.LoadFromXmlFile(tmp);
	CoUninitialize();
	if (ret) {
		selectedItemId_ = project_.GetRootElement()->GetId();
		UpdateAllViews(NULL, enUpdateViewReason_DocLoaded);
	} else {
		::MessageBox(NULL, project_.ParseErrorToString(), _T("Project Parse Error"), MB_OK);
	}
	
	return ret;
}

BOOL CProjectDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	UpdateAllViews(NULL, enUpdateViewReason_BeforeSaveDoc);

	CoInitialize(NULL);
	cmString tmp;

	tmp = lpszPathName;
	project_.SaveToXmlFile(tmp);
	CoUninitialize();

	return TRUE;
}

void CProjectDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CDocument::SetPathName(lpszPathName, bAddToMRU);
}

void CProjectDoc::SetTitle(LPCTSTR lpszTitle) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CDocument::SetTitle(lpszTitle);
}

void CProjectDoc::SetDisplayRootNode(bool displayRootNode)
{
	displayRootNode_ = displayRootNode;
}

bool CProjectDoc::GetDisplayRootNode() const
{
	return displayRootNode_;
}

void CProjectDoc::SetSelectedItemId(const cmString& id)
{
	selectedItemId_ = id;
}
	
cmString CProjectDoc::GetSelectedItemId() const
{
	return selectedItemId_;
}

CXmlBaseElement* CProjectDoc::GetElementById(const cmString& id)
{
	if (project_.GetRootElement()->GetId() == id)
		return project_.GetRootElement();

	return project_.GetRootElement()->GetMemberById(id);
}
