// NFOAddCycleRuleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NFO File Editor.h"
#include "NFO File EditorDlg.h"
#include "NFOAddCycleRuleDlg.h"
#include "NFOErrorDlg.h"

#if !defined(STUFF_STUFF_HPP)
	#include "Stuff\Stuff.hpp"
#endif


#include <GameOS\GameOS.hpp>
#include <MFCPlatform\MFCPlatform.hpp>
#include <Stuff\NotationFile.hpp>

#if 0 // def _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNFOAddCycleRuleDlg dialog


CNFOAddCycleRuleDlg::CNFOAddCycleRuleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNFOAddCycleRuleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNFOAddCycleRuleDlg)
	//}}AFX_DATA_INIT
}


void CNFOAddCycleRuleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNFOAddCycleRuleDlg)
	DDX_Control(pDX, IDOK, m_OK);
	DDX_Control(pDX, IDC_NFO_RULE_NAME, m_MapList);
	DDX_Control(pDX, IDC_NFO_FILE_NAME, m_FileList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CNFOAddCycleRuleDlg, CDialog)
	//{{AFX_MSG_MAP(CNFOAddCycleRuleDlg)
	ON_CBN_SELCHANGE(IDC_NFO_FILE_NAME, OnSelchangeNFOFileName)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNFOAddCycleRuleDlg message handlers

BOOL CNFOAddCycleRuleDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	int value = LoadNFOFileName();
	ASSERT(value == TRUE);
	value = LoadNFOMapName();
	ASSERT(value == TRUE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


BOOL CNFOAddCycleRuleDlg::LoadNFOFileName()
{	
	CNFOFileEditorDlg* pParent = (CNFOFileEditorDlg*) GetOwner();

	// Get the current selected NFO
	int count = pParent->m_NFOList.GetCount();
	ASSERT(count != LB_ERR);
	if (count == LB_ERR)
		return FALSE;
	
	// Get the name of the NFO
	char filename[256];
	for (int i = 0; i < count; i++)
	{
		int text_value = pParent->m_NFOList.GetText(i, filename);
		ASSERT(text_value != LB_ERR);
		if (text_value == LB_ERR)
			return FALSE;

		m_FileList.AddString(filename);
	}

	// Just set the default as the first one in the list
	if (pParent->m_NFOList.GetCurSel() != -1)
		m_FileList.SetCurSel(pParent->m_NFOList.GetCurSel());
	else
		m_FileList.SetCurSel(0);

	return TRUE;
}

BOOL CNFOAddCycleRuleDlg::LoadNFOMapName()
{
	CNFOFileEditorDlg* pParent = (CNFOFileEditorDlg*) GetOwner();

	// Delete every item from the combo box.
	while (m_MapList.GetCount())
	{
	   m_MapList.DeleteString(0);
	}

	// Grab the NFO file that is selected
	int curSel = m_FileList.GetCurSel();
	ASSERT(curSel != LB_ERR);
	if (curSel == LB_ERR)
		return FALSE;

	char filename[256];
	int value = m_FileList.GetLBText(curSel, filename);
	ASSERT(value != CB_ERR);
	if (value == CB_ERR)
		return FALSE;
	
	// choose the directory we need
	Stuff::MString nfo_name;
	int mission = pParent->m_MissionList.GetCurSel();
	switch (mission)
	{
	case 0:
		nfo_name = "resource\\Missions\\";
		break;
	case 1:
		nfo_name = "resource\\UserMissions\\";
		break;
	default:
		ASSERT(!"Mission selection is invalid!");
		return FALSE;
	}
	nfo_name += filename;
	Stuff::NotationFile notefile(nfo_name, Stuff::NotationFile::Standard, true);
	
	// Grab the scenarios from the selected NFO file
	Stuff::Page* page = notefile.FindPage("scenarios");
	if (page != NULL)
	{
		Stuff::ChainIteratorOf<Stuff::Note*> *note_iterator = page->MakeNoteIterator();

		Stuff::Note *note;
		while ((note = note_iterator->ReadAndNext()) != NULL)
		{
			Stuff::NotationFile scenario_file;
			note->GetEntry(&scenario_file);
				
			Stuff::Page *server_page = scenario_file.FindPage("server");
			if (server_page)
			{
				const char* scenario_name = note->GetName();
				m_MapList.AddString(scenario_name);
			}
		}
	}
	else
	{
		m_MapList.EnableWindow(FALSE);
		m_OK.EnableWindow(FALSE);
	}

	// Just set the default as the first one in the list
	m_MapList.SetCurSel(0);

	return TRUE;
}

void CNFOAddCycleRuleDlg::OnSelchangeNFOFileName() 
{
	int value = LoadNFOMapName();
	ASSERT(value == TRUE);
}

void CNFOAddCycleRuleDlg::OnOK() 
{
	CNFOFileEditorDlg* pParent = (CNFOFileEditorDlg*) GetOwner();

	// save the NFO file name
	int curSel = m_FileList.GetCurSel();
	ASSERT(curSel != LB_ERR);
	if (curSel == LB_ERR)
		return;

	char filename[256];
	int value = m_FileList.GetLBText(curSel, filename);
	ASSERT(value != CB_ERR);
	if (value == CB_ERR)
		return;

	// And save the rule name from that NFO file
	curSel = m_MapList.GetCurSel();
	ASSERT(curSel != LB_ERR);
	if (curSel == LB_ERR)
		return;
	
	char mapname[256];
	value = m_MapList.GetLBText(curSel, mapname);
	ASSERT(value != CB_ERR);
	if (value == CB_ERR)
		return;
	
	// choose the directory we need
	Stuff::MString nfo_name;
	int mission = pParent->m_MissionList.GetCurSel();
	switch (mission)
	{
	case 0:
		nfo_name = "resource\\Missions\\";
		break;
	case 1:
		nfo_name = "resource\\UserMissions\\";
		break;
	default:
		ASSERT(!"Mission selection is invalid!");
		return;
	}
	nfo_name += filename;

	if (pParent->LoadScenario(nfo_name, filename, mapname, pParent->m_CycleList.GetItemCount()) == TRUE)
	{
		CDialog::OnOK();
		return;
	}

	// We encounter some errors, so print out an error message and return
	CNFOErrorDlg dlg;
	dlg.m_Error = "The scenario selected is invalid.  It will not be added to the Server Cycle List.";
	dlg.DoModal();

	CDialog::OnOK();
}
