// WizPageMission.cpp : implementation file
//

#include "stdafx.h"
#include "contenttrack.h"
#include "WizPageMission.h"
#include "WizCopyMission.h"
#include "mainfrm.h"
#include <GameOS\ToolOS.hpp>

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

/////////////////////////////////////////////////////////////////////////////
// CWizPageMission property page

IMPLEMENT_DYNCREATE(CWizPageMission, CPropertyPage)

CWizPageMission::CWizPageMission() : CPropertyPage(CWizPageMission::IDD)
{
	//{{AFX_DATA_INIT(CWizPageMission)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CWizPageMission::~CWizPageMission()
{
}

void CWizPageMission::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWizPageMission)
	DDX_Control(pDX, IDC_EDIT_NAME, m_ctrlName);
	DDX_Control(pDX, IDC_CHECK_RENAME, m_checkRename);
	DDX_Control(pDX, IDC_LISTMISSION, m_ctrlList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWizPageMission, CPropertyPage)
	//{{AFX_MSG_MAP(CWizPageMission)
	ON_WM_DESTROY()
	ON_LBN_SELCHANGE(IDC_LISTMISSION, OnSelchangeListmission)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWizPageMission message handlers

BOOL CWizPageMission::OnSetActive() 
{
	CWizCopyMission *pParent = (CWizCopyMission *)GetParent();
    pParent->SetWizardButtons(NULL);
	return CPropertyPage::OnSetActive();
}

BOOL CWizPageMission::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// Open the Resources.build to determine which missions are available
	CFrameWnd *pFrame = GetParentFrame();
	CMainFrame *pMainFrame = (CMainFrame *)pFrame;
	CDocument *pDoc = pMainFrame->MDIGetActive()->GetActiveDocument();
	CWizCopyMission *pParent = (CWizCopyMission *)GetParent();
	pParent->m_pSrcDoc = (CNodeDataValueFileDoc *)pDoc;

	// Open Resources.build
	static int check1 = 1;

	CNodeDisk *pDisk = pParent->m_pSrcDoc->FindDirectoryNode("content\\resources.build");
	if (pDisk&& pDisk->IsLoaded())
	{
		POSITION pos = pDisk->GetFirstChild();
		CNodeObject *pNodeMain = NULL;
		while (pos &&(NULL == pNodeMain))
		{
			CNodeObject *pNode = pDisk->GetNextChild(pos);
			CString name;
			pNode->GetName(name);
//			name.MakeLower();
//			if (name == CString("resources\\main"))
			if (0 == stricmp(name,"resources\\main"))
			{
				pNodeMain = pNode;
			}
		}

		if (NULL != pNodeMain)
		{
			// Look for each instance node, and find each one that points to the mission directory
			pos = pNodeMain->GetFirstChild();
			while (pos)
			{
				CNodeObject *pNode = pNodeMain->GetNextChild(pos);
				CString name;
				pNode->GetName(name);
//				name.MakeLower();
//				if (name == CString("instance"))
				if (0 == stricmp(name,"instance"))
				{
					POSITION pos1 = pNode->GetFirstChild();
					while (pos)
					{
						CNodeObject *pNode1 = pNode->GetNextChild(pos1);
						CString name1;
						pNode1->GetName(name1);
						if (stricmp(name1,"missions")==0)
						{
							// We have a mission here
							char drive[MAX_PATH],dir[MAX_PATH],fname[MAX_PATH],ext[MAX_PATH];
							_splitpath(name1,drive,dir,fname,ext);
							int index = m_ctrlList.AddString(fname);

							// VERY IMPORTANT: be sure to delete these string during destruction
							CString *pString = new CString(name1);
							Register_Pointer(pString);
							m_ctrlList.SetItemData(index,(DWORD)pString);
						}
					}
				}
			}
		}
		else
		{
			// Invalid resources.build file
			int x=1;
		}
	}
	else
	{
		// resources.build is not already loaded so we have to scan it manually
		SetCurrentDirectory(pParent->m_pSrcDoc->GetRootDir());
		if (gos_DoesFileExist("content\\resources.build"))
		{
			NotationFile notefile("content\\resources.build");
			Page *page = notefile.FindPage("resources\\main");
			if (page)
			{
				Check_Object(page);
				Page::NoteIterator *entries = page->MakeNoteIterator();
				Check_Object(entries);

				
				Note *entry;
				// Enumerate the entries in the section
				while ((entry = entries->ReadAndNext()) != NULL)
				{
					Check_Object(entry);

					CString key(entry->GetName());
					//key.MakeLower();
					//if (key == "instance")
					if (0 == stricmp(key,"instance"))
					{
						const char *value;
						entry->GetEntry(&value);
						if (strnicmp(value,"missions",8)==0)
						{
							// We have a mission here
							char drive[MAX_PATH],dir[MAX_PATH],fname[MAX_PATH],ext[MAX_PATH];
							_splitpath(value,drive,dir,fname,ext);
							int index = m_ctrlList.AddString(fname);

							// VERY IMPORTANT: be sure to delete these string during destruction
							CString *pString = new CString(value);
							Register_Pointer(pString);
							m_ctrlList.SetItemData(index,(DWORD)pString);
						}
					}
				}
				Check_Object(entries);
				delete entries;
			}
			else
			{
				// Invalid resoures.build file
				int x=1;
			}

		}
		else
		{
			// We have no resources.build file
			int x=1;
		}
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CWizPageMission::OnDestroy() 
{
	CPropertyPage::OnDestroy();
	while (m_ctrlList.GetCount())
	{
		CString *pString = (CString *)m_ctrlList.GetItemData(0);
		Unregister_Pointer(pString);
		delete pString;
		m_ctrlList.DeleteString(0);
	}
}

void CWizPageMission::OnSelchangeListmission() 
{
	if (LB_ERR != m_ctrlList.GetCurSel())
	{
		CWizCopyMission *pParent = (CWizCopyMission *)GetParent();
		pParent->SetWizardButtons(PSWIZB_NEXT);
	}
}

BOOL CWizPageMission::OnWizardFinish() 
{
	// This shouldn't be called
	int index = m_ctrlList.GetCurSel();
	CString *pString = (CString *)m_ctrlList.GetItemData(index);
	CWizCopyMission *pParent = (CWizCopyMission *)GetParent();
	pParent->m_strMission= *pString;
	
	if (this->m_checkRename.GetCheck())
	{
		pParent->m_bRename = true;
		this->m_ctrlName.GetWindowText(pParent->m_strRename);
	}
	return CPropertyPage::OnWizardFinish();
}

LRESULT CWizPageMission::OnWizardNext() 
{
	int index = m_ctrlList.GetCurSel();
	CString strName;
	m_ctrlList.GetText(index,strName);
	CString *pString = (CString *)m_ctrlList.GetItemData(index);
	CWizCopyMission *pParent = (CWizCopyMission *)GetParent();
	pParent->m_strName = strName;
	pParent->m_strMission= *pString;
	
	if (this->m_checkRename.GetCheck())
	{
		pParent->m_bRename = true;
		this->m_ctrlName.GetWindowText(pParent->m_strRename);
	}
	return CPropertyPage::OnWizardNext();
}
