// MultitronDoc.cpp : implementation of the CMultitronDoc class
//

#include "stdafx.h"
#include <direct.h>

#include "Multitron.h"
#include "MultitronDoc.h"
#include "MainFrm.h"
#include <GameOS\toolOS.hpp>

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

/////////////////////////////////////////////////////////////////////////////
// CMultitronDoc

IMPLEMENT_DYNCREATE(CMultitronDoc, CDocument)

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

/////////////////////////////////////////////////////////////////////////////
// CMultitronDoc construction/destruction

CMultitronDoc::CMultitronDoc()
{
	m_pNoteFile = NULL;
	m_pTexturePageDoc = NULL;
}

CMultitronDoc::~CMultitronDoc()
{
	if (m_pNoteFile->IsChanged())
	{
		m_pNoteFile->IgnoreChanges();
	}
	delete m_pNoteFile;
}

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

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CMultitronDoc serialization

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

/////////////////////////////////////////////////////////////////////////////
// CMultitronDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CMultitronDoc commands

/*******************************************************************************
/* function name: OnCloseDocument
/* description: Close the hint file
/*******************************************************************************/
void CMultitronDoc::OnCloseDocument() 
{
	if (m_pTexturePageDoc)
		m_pTexturePageDoc->OnCloseDocument();
	CDocument::OnCloseDocument();
}

/*******************************************************************************
/* function name: OnSaveDocument
/* description: Save the hint file
/*******************************************************************************/
BOOL CMultitronDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
	if (::gos_FileReadOnly(lpszPathName))
		::gos_FileSetReadWrite(lpszPathName);
	m_pNoteFile->SaveAs(lpszPathName);
	return TRUE;
}

/*******************************************************************************
/* function name: OnOpenDocument
/* description: Open a hint file
/*******************************************************************************/
BOOL CMultitronDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	char path[MAX_PATH];
	_splitpath(lpszPathName, NULL, path, NULL, NULL);
	CString strPath(path);
	strPath.MakeLower();
	chdir(strPath.Left(strPath.Find("content")));
	
	m_pNoteFile = new Stuff::NotationFile(lpszPathName);
	return TRUE;
}

/*******************************************************************************
/* function name: SetPathName
/* description: Called when a file is opened
/*******************************************************************************/
void CMultitronDoc::SetPathName(LPCTSTR lpszPathName, BOOL bAddToMRU)
{
	CDocument::SetPathName(lpszPathName, bAddToMRU);
	CString path(lpszPathName);
	path.MakeLower();
	SetTitle(path.Right(path.GetLength() - path.Find("content")));
}

/*******************************************************************************
/* function name: SaveModified
/* description: Called when a modified hint file is being closed
/*******************************************************************************/
BOOL CMultitronDoc::SaveModified() 
{	
	
	if (m_pNoteFile->IsChanged())
	{
		CString str = m_pNoteFile->GetFileName();
		if (IDYES == MessageBox(NULL,str+" has changed.  Save changes?","Texture Hint File",MB_YESNO))
		{
			if (::gos_FileReadOnly(m_pNoteFile->GetFileName()))
				::gos_FileSetReadWrite(m_pNoteFile->GetFileName());
			m_pNoteFile->Save();
		}
	}
	return CDocument::SaveModified();
}

/*******************************************************************************
/* function name: CanCloseFrame
/* description: 
/*******************************************************************************/
BOOL CMultitronDoc::CanCloseFrame(CFrameWnd* pFrame) 
{
	//return false;	
	return CDocument::CanCloseFrame(pFrame);
}

/*******************************************************************************
/* function name: RemoveTexture
/* description: Delete a hint page from the file
/*******************************************************************************/
CString CMultitronDoc::RemoveTexture(Stuff::Page *page)
{
	CString name = page->GetName();
	const char *entry;
	if (page->GetEntry("alias",&entry))
	{
		name = entry;
	}
	
	if (m_pNoteFile) {
		m_pNoteFile->DeletePage(page->GetName());
	}

	return name;
}

/*******************************************************************************
/* function name: OpenTexture
/* description: Open a texture hint page in the texture view panel
/*******************************************************************************/
CDocument *CMultitronDoc::OpenTexture(Stuff::Page *page)
{
	if (m_pTexturePageDoc)
	{
		m_pTexturePageDoc->SetPathName((LPCTSTR) page);
		return m_pTexturePageDoc;
	}

	CMultitronApp *pApp = (CMultitronApp *)AfxGetApp();
	CDocTemplate *pTemplate = (CDocTemplate *)pApp->m_mapTemplate[ID_FILE_NEW];
	m_pTexturePageDoc = pTemplate->OpenDocumentFile((LPCTSTR)page);
	return m_pTexturePageDoc;
}

/*******************************************************************************
/* function name: CloseTexture
/* description: Close a texture hint page in the texture view panel
/*******************************************************************************/
void CMultitronDoc::CloseTexture(CDocument *pDoc)
{
	CDocument *pValueDoc = (CDocument *)m_pTexturePageDoc;
	if (pValueDoc == pDoc)
		m_pTexturePageDoc = NULL;
}