// IslandDoc.cpp : implementation of the CIslandDoc class
//

#include "stdafx.h"
#include "Island.h"

#include "IslandDoc.h"
#include "Helpers/Texture.h"

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

/////////////////////////////////////////////////////////////////////////////
// CIslandDoc

IMPLEMENT_DYNCREATE(CIslandDoc, CDocument)

BEGIN_MESSAGE_MAP(CIslandDoc, CDocument)
	//{{AFX_MSG_MAP(CIslandDoc)
		// 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()

/////////////////////////////////////////////////////////////////////////////
// CIslandDoc construction/destruction

CIslandDoc::CIslandDoc()
{
	// TODO: add one-time construction code here

}

CIslandDoc::~CIslandDoc()
{
}

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

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	SetModifiedFlag(TRUE); 
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CIslandDoc serialization

void CIslandDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		m_currentIsland.Serialize(ar);
	}
	else
	{
		// TODO: add loading code here
		m_currentIsland.Serialize(ar);
		UpdateAllViews(NULL);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CIslandDoc diagnostics

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

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

/////////////////////////////////////////////////////////////////////////////
// CIslandDoc commands

//tell the island to create itself
void CIslandDoc::CreateIsland(void)
{
	m_currentIsland.CreateIsland();
	//default it to sunny
	m_currentIsland.SetWeather(SUNNY);
	m_currentIsland.ToggleCurrentDisplayMethod();
}

//tell the island to draw itself
void CIslandDoc::DrawScene(void)
{
	m_currentIsland.DrawIslandScene();

}

//called by CIslandView when the user presses down a key.
void CIslandDoc::KeyPressedInDirection(int nChar)
{
	m_currentIsland.KeyPressedInDirection(nChar);
	UpdateAllViews(NULL);
}

void CIslandDoc::KeyUpInDirection(int nChar)
{
	m_currentIsland.KeyUpInDirection(nChar);
	UpdateAllViews(NULL);
}

int CIslandDoc::GetCurrentIslandWeather(void)
{
	return m_currentIsland.GetWeather();
}

void CIslandDoc::SetCurrentIslandWeather(int nWeather)
{
	m_currentIsland.SetWeather(nWeather);
	UpdateAllViews(NULL);
}

int CIslandDoc::GetCurrentIslandDisplayMethod(void)
{
	return m_currentIsland.GetDisplayMethod();
}

void CIslandDoc::ToggleCurrentIslandDisplayMethod(void)
{
	m_currentIsland.ToggleCurrentDisplayMethod();
	UpdateAllViews(NULL);
}

void CIslandDoc::ChangeCharacter(int code)
{
	m_currentIsland.ChangeCharacter(code);
	UpdateAllViews(NULL);
}

int CIslandDoc::GetCurrentIslandCharacter(void)
{
	return m_currentIsland.GetCurrentCharacter();
}

BOOL CIslandDoc::OnSaveDocument(LPCTSTR lpszPathName) 
{
	// TODO: Add your specialized code here and/or call the base class
	//always allow saving
	SetModifiedFlag(TRUE); 
	if (! CDocument::OnSaveDocument(lpszPathName))
	{
		::MessageBox (NULL, "The selected island could not be saved", "Error Loading", MB_OK);
		return false;
	}
	SetModifiedFlag(TRUE); 
	return true;
}

//just don't try t open the current one, MFC screws up
BOOL CIslandDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{

	//load the world
	if (!CDocument::OnOpenDocument(lpszPathName))
	{
		::MessageBox (NULL, "The selected world could not be loaded", "Error Loading", MB_OK);
		return false;
	}
	SetModifiedFlag(TRUE); 
	return true;
	
}
