// NFORuleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NFO File Editor.h"
#include "NFO File EditorDlg.h"
#include "NFOPropertiesDlg.h"
#include "NFORuleDlg.h"
#include "NFOErrorDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CNFORuleDlg dialog


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


void CNFORuleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNFORuleDlg)
	DDX_Control(pDX, IDC_SCRIPT, m_Script);
	DDX_Control(pDX, IDC_MIN_TON, m_MinTon);
	DDX_Control(pDX, IDC_MAX_TON, m_MaxTon);
	DDX_Control(pDX, IDC_GAME_LENGTH, m_GameLength);
	DDX_Control(pDX, IDC_MAP_NAME, m_MapName);
	DDX_Control(pDX, IDC_WEATHER, m_Weather);
	DDX_Control(pDX, IDC_TEAM_COUNT, m_TeamCount);
	DDX_Control(pDX, IDC_STOCK_MECH, m_StockMech);
	DDX_Control(pDX, IDC_SCENARIO_TEXT, m_ScenarioText);
	DDX_Control(pDX, IDC_RULE_SET, m_RuleSet);
	DDX_Control(pDX, IDC_RULE_NAME, m_RuleName);
	DDX_Control(pDX, IDC_RESPAWN_LIMIT, m_RespawnLimit);
	DDX_Control(pDX, IDC_RADAR_MODE, m_RadarMode);
	DDX_Control(pDX, IDC_NIGHT, m_Night);
	DDX_Control(pDX, IDC_KILL_LIMIT, m_KillLimit);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CNFORuleDlg, CDialog)
	//{{AFX_MSG_MAP(CNFORuleDlg)
	ON_CBN_SELCHANGE(IDC_RULE_SET, OnSelChangeRuleSet)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNFORuleDlg message handlers

BOOL CNFORuleDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CNFOPropertiesDlg* pParent = (CNFOPropertiesDlg*) GetOwner();
	CNFOFileEditorDlg* pPParent = (CNFOFileEditorDlg*) pParent->GetOwner();

	// set all the options
	char intbuf[256];
	for (int i = 0; i < GameTypeCount; i++)
	{
		if (pParent->m_bTypeAvailable[i] == TRUE)
			m_RuleSet.AddString(pPParent->m_szScenarioName[i]);
	}
	m_Night.AddString(pPParent->m_szTOD[0]);
	m_Night.AddString(pPParent->m_szTOD[1]);
	m_Night.AddString(pPParent->m_szDefault);
	m_Weather.AddString(pPParent->m_szYesNo[0]);
	m_Weather.AddString(pPParent->m_szYesNo[1]);
	m_Weather.AddString(pPParent->m_szDefault);
	m_RadarMode.AddString(pPParent->m_szRadar[0]);
	m_RadarMode.AddString(pPParent->m_szRadar[1]);
	m_RadarMode.AddString(pPParent->m_szRadar[2]);
	m_RadarMode.AddString(pPParent->m_szDefault);
	m_StockMech.AddString(pPParent->m_szYesNo[0]);
	m_StockMech.AddString(pPParent->m_szYesNo[1]);
	m_StockMech.AddString(pPParent->m_szDefault);
	for (i = 25; i <= 100; i += 5)
	{
		m_MaxTon.AddString(_itoa(i, intbuf, 10));
		m_MinTon.AddString(_itoa(i, intbuf, 10));
	}
	m_MaxTon.AddString(pPParent->m_szDefault);
	m_MinTon.AddString(pPParent->m_szDefault);
	m_GameLength.AddString("1");
	m_GameLength.AddString("2");
	m_GameLength.AddString("5");
	m_GameLength.AddString("10");
	m_GameLength.AddString("15");
	m_GameLength.AddString("30");
	m_GameLength.AddString("60");
	m_GameLength.AddString("90");
	m_GameLength.AddString("120");
	m_GameLength.AddString(pPParent->m_szDefault);

	// kill limit and respawn limit max is 99
	m_KillLimit.SetLimitText(2);
	m_RespawnLimit.SetLimitText(2);

	// Text limit is 256
	m_MapName.SetLimitText(256);
	m_RuleName.SetLimitText(256);

	// either grab the info or set to default
	if (pParent->m_bEditRule)
	{
		// grab the info from the property list
		int curSel = pParent->m_PropertyList.GetSelectionMark();
		char scenario_name[256];
		char rule_set_name[256];
		char scenario_read_name[256];
		char team_count[256];
		char scenario_text[256];
		char kill_count[256];
		char respawn_count[256];
		char night[256];
		char weather[256];
		char radar_mode[256];
		char stock_mech[256];
		char max_ton[256];
		char min_ton[256];
		char game_len[256];
		char script[256];

		int found;

		pParent->m_PropertyList.GetItemText(curSel, 0, scenario_name, 256);
		pParent->m_PropertyList.GetItemText(curSel, 1, rule_set_name, 256);
		pParent->m_PropertyList.GetItemText(curSel, 2, scenario_read_name, 256);
		pParent->m_PropertyList.GetItemText(curSel, 4, team_count, 256);
		pParent->m_PropertyList.GetItemText(curSel, 5, scenario_text, 256);
		pParent->m_PropertyList.GetItemText(curSel, 7, kill_count, 256);
		pParent->m_PropertyList.GetItemText(curSel, 9, respawn_count, 256);
		pParent->m_PropertyList.GetItemText(curSel, 10, night, 256);
		pParent->m_PropertyList.GetItemText(curSel, 11, weather, 256);
		pParent->m_PropertyList.GetItemText(curSel, 12, radar_mode, 256);
		pParent->m_PropertyList.GetItemText(curSel, 13, stock_mech, 256);
		pParent->m_PropertyList.GetItemText(curSel, 14, max_ton, 256);
		pParent->m_PropertyList.GetItemText(curSel, 15, min_ton, 256);
		pParent->m_PropertyList.GetItemText(curSel, 16, game_len, 256);
		pParent->m_PropertyList.GetItemText(curSel, 17, script, 256);
		
		m_RuleSet.SetCurSel(m_RuleSet.FindStringExact(0, rule_set_name));	
		for (int i = 0; i < GameTypeCount; i++)
		{
			if (_stricmp(pPParent->m_szScenarioName[i], rule_set_name) == 0)
			{
				// do team
				for (int j = 1; j <= pParent->m_iMaxTeam[i]; j++)
					m_TeamCount.AddString(_itoa(j, intbuf, 10));

				// do script
				SCRIPT_INFO *pInfo = pParent->m_pScriptInfo[i];
				while (pInfo != NULL)
				{
					m_Script.AddString(pInfo->script);
					pInfo = pInfo->pNext;
				}
				break;
			}
		}
		if (m_TeamCount.GetCount() == 0)
			m_TeamCount.AddString("0");
		if (m_Script.GetCount() == 0)
		{
			m_Script.AddString("\0");
			m_Script.EnableWindow(FALSE);
		}
		else
			m_Script.EnableWindow(TRUE);

		found = m_TeamCount.FindStringExact(0, team_count);
		if (found != CB_ERR)
			m_TeamCount.SetCurSel(found);
		else
			m_TeamCount.SetCurSel(m_TeamCount.GetCount() - 1);

		found = m_Night.FindStringExact(0, night);
		if (found != CB_ERR)
			m_Night.SetCurSel(found);
		else
			m_Night.SetCurSel(m_Night.GetCount() - 1);
		found = m_Weather.FindStringExact(0, weather);
		if (found != CB_ERR)
			m_Weather.SetCurSel(found);
		else
			m_Weather.SetCurSel(m_Weather.GetCount() - 1);
		found = m_RadarMode.FindStringExact(0, radar_mode);
		if (found != CB_ERR)
			m_RadarMode.SetCurSel(found);
		else
			m_RadarMode.SetCurSel(m_RadarMode.GetCount() - 1);
		found = m_StockMech.FindStringExact(0, stock_mech);
		if (found != CB_ERR)
			m_StockMech.SetCurSel(found);
		else
			m_StockMech.SetCurSel(m_StockMech.GetCount() - 1);
		found = m_MaxTon.FindStringExact(0, max_ton);
		if (found != CB_ERR)
			m_MaxTon.SetCurSel(found);
		else
			m_MaxTon.SetCurSel(m_MaxTon.GetCount() - 1);
		found = m_MinTon.FindStringExact(0, min_ton);
		if (found != CB_ERR)
			m_MinTon.SetCurSel(found);
		else
			m_MinTon.SetCurSel(m_MinTon.GetCount() - 1);
		found = m_GameLength.FindStringExact(0, game_len);
		if (found != CB_ERR)
			m_GameLength.SetCurSel(found);
		else
			m_GameLength.SetCurSel(m_GameLength.GetCount() - 1);
		found = m_Script.FindStringExact(0, script);
		if (found != CB_ERR)
			m_Script.SetCurSel(found);
		else
			m_Script.SetCurSel(m_Script.GetCount() - 1);
		
		m_RuleName.ReplaceSel(scenario_name);
		m_MapName.ReplaceSel(scenario_read_name);
		m_ScenarioText.ReplaceSel(scenario_text);
		m_KillLimit.ReplaceSel(kill_count);
		m_RespawnLimit.ReplaceSel(respawn_count);

		// Do not allow rule name edit
		m_RuleName.SetReadOnly();
		m_ScenarioText.SetReadOnly();
	}
	else
	{
		// since we are adding, just set everything to default
		m_RuleSet.SetCurSel(0);

		char rule_set_name[256];
		m_RuleSet.GetLBText(0, rule_set_name);
		for (int i = 0; i < GameTypeCount; i++)
		{
			if (_stricmp(pPParent->m_szScenarioName[i], rule_set_name) == 0)
			{
				// do team
				for (int j = 1; j <= pParent->m_iMaxTeam[i]; j++)
					m_TeamCount.AddString(_itoa(j, intbuf, 10));
				
				// do script
				SCRIPT_INFO *pInfo = pParent->m_pScriptInfo[i];
				while (pInfo != NULL)
				{
					m_Script.AddString(pInfo->script);
					pInfo = pInfo->pNext;
				}
				break;
			}
		}
		if (m_TeamCount.GetCount() == 0)
			m_TeamCount.AddString("0");
		if (m_Script.GetCount() == 0)
		{
			m_Script.AddString("\0");
			m_Script.EnableWindow(FALSE);
		}
		else
			m_Script.EnableWindow(TRUE);

		m_TeamCount.SetCurSel(m_TeamCount.GetCount() - 1);
		m_ScenarioText.ReplaceSel(pPParent->m_szScenarioText[0]);
		m_Night.SetCurSel(m_Night.GetCount() - 1);
		m_Weather.SetCurSel(m_Weather.GetCount() - 1);
		m_RadarMode.SetCurSel(m_RadarMode.GetCount() - 1);
		m_StockMech.SetCurSel(m_StockMech.GetCount() - 1);
		m_MaxTon.SetCurSel(m_MaxTon.GetCount() - 1);
		m_MinTon.SetCurSel(m_MinTon.GetCount() - 1);
		m_GameLength.SetCurSel(m_GameLength.GetCount() - 1);
		m_Script.SetCurSel(0);

		m_RuleName.Clear();
		m_MapName.Clear();
		m_ScenarioText.Clear();
		m_KillLimit.Clear();
		m_RespawnLimit.Clear();
		
		m_RuleSet.EnableWindow(TRUE);
		m_ScenarioText.EnableWindow(TRUE);
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CNFORuleDlg::OnOK() 
{	
	// first validate the min and max ton
	if ((m_MinTon.GetCurSel() != m_MinTon.GetCount() - 1) &&
		(m_MaxTon.GetCurSel() != m_MaxTon.GetCount() - 1))
	{
		if (m_MinTon.GetCurSel() > m_MaxTon.GetCurSel())
		{
			CNFOErrorDlg dlg;
			dlg.m_Error = "The minimum tonnage cannot be larger than the maximum tonnage.";
			dlg.DoModal();
			return;
		}
	}

	CNFOPropertiesDlg* pParent = (CNFOPropertiesDlg*) GetOwner();
	CNFOFileEditorDlg* pPParent = (CNFOFileEditorDlg*) pParent->GetOwner();

	char scenario_name[256];
	char rule_set_name[256];
	char scenario_read_name[256];
	char team_count[256];
	char scenario_text[256];
	char kill_count[256];
	char respawn_count[256];
	char night[256];
	char weather[256];
	char radar_mode[256];
	char stock_mech[256];
	char max_ton[256];
	char min_ton[256];
	char game_len[256];
	char script[256];

	int curSel;

	int rule_len = m_RuleName.GetLine(0, scenario_name, 256);
	int map_len = m_MapName.GetLine(0, scenario_read_name, 256);
	int text_len = m_ScenarioText.GetLine(0, scenario_text, 256);
	scenario_name[rule_len] = '\0';
	scenario_read_name[map_len] = '\0';
	scenario_text[text_len] = '\0';

	// then check for empty texts
	if (rule_len == 0)
	{
		CNFOErrorDlg dlg;
		dlg.m_Error = "Empty Rule Name is not allowed.";
		dlg.DoModal();
		return;
	}
	if (map_len == 0)
	{
		CNFOErrorDlg dlg;
		dlg.m_Error = "Empty Map Name is not allowed.";
		dlg.DoModal();
		return;
	}

	// finally check for duplicate names if we are adding a new rule
	if (pParent->m_bEditRule == FALSE)
	{
		for (int i = 0; i < pParent->m_PropertyList.GetItemCount(); i++)
		{
			char existing_name[256];
			pParent->m_PropertyList.GetItemText(i, 0, existing_name, 256);
			if (_stricmp(scenario_name, existing_name) == 0)
			{
				CNFOErrorDlg dlg;
				dlg.m_Error = "The current Rule Name exists.  Duplicated Rule Names are not allowed.";
				dlg.DoModal();
				return;
			}
		}
	
		// Check to see if the scenario text file exist
		if ((text_len != 0) && ((GetFileAttributes(scenario_text) == -1) || ((GetFileAttributes(scenario_text) & FILE_ATTRIBUTE_DIRECTORY) != 0)))
		{
			CNFOErrorDlg dlg;
			dlg.m_Error = "Can't find the scenario text file.  Please either specify a valid text file or leave the scenario text file field blank.";
			dlg.DoModal();
			return;
		}
	}

	// We are cool, so go ahead and grab all the info from the dialog
	int kill_len = m_KillLimit.GetLine(0, kill_count, 256);
	int respawn_len = m_RespawnLimit.GetLine(0, respawn_count, 256);
	kill_count[kill_len] = '\0';
	respawn_count[respawn_len] = '\0';
	curSel = m_RuleSet.GetCurSel();
	m_RuleSet.GetLBText(curSel, rule_set_name);
	curSel = m_TeamCount.GetCurSel();
	m_TeamCount.GetLBText(curSel, team_count);
	curSel = m_Night.GetCurSel();
	if (curSel != m_Night.GetCount() - 1)
		m_Night.GetLBText(curSel, night);
	else
		night[0] = '\0';
	curSel = m_Weather.GetCurSel();
	if (curSel != m_Weather.GetCount() - 1)
		m_Weather.GetLBText(curSel, weather);
	else
		weather[0] = '\0';
	curSel = m_RadarMode.GetCurSel();
	if (curSel != m_RadarMode.GetCount() - 1)
		m_RadarMode.GetLBText(curSel, radar_mode);
	else
		radar_mode[0] = '\0';
	curSel = m_StockMech.GetCurSel();
	if (curSel != m_StockMech.GetCount() - 1)
		m_StockMech.GetLBText(curSel, stock_mech);
	else
		stock_mech[0] = '\0';
	curSel = m_MaxTon.GetCurSel();
	if (curSel != m_MaxTon.GetCount() - 1)
		m_MaxTon.GetLBText(curSel, max_ton);
	else
		max_ton[0] = '\0';
	curSel = m_MinTon.GetCurSel();
	if (curSel != m_MinTon.GetCount() - 1)
		m_MinTon.GetLBText(curSel, min_ton);
	else
		min_ton[0] = '\0';
	curSel = m_GameLength.GetCurSel();
	if (curSel != m_GameLength.GetCount() - 1)
		m_GameLength.GetLBText(curSel, game_len);
	else
		game_len[0] = '\0';
	curSel = m_Script.GetCurSel();
	m_Script.GetLBText(curSel, script);
	
	// finally if it is a team scenario, make sure that team count is not zero
	if (m_TeamCount.IsWindowEnabled() == TRUE)
	{
		if (m_TeamCount.GetCurSel() == 0)
		{
			CNFOErrorDlg dlg;
			dlg.m_Error = "Zero Team Count is not allowed.";
			dlg.DoModal();
			return;
		}
	}

	// once we are done grabbing, we either edit the existing one or add a new rule
	if (pParent->m_bEditRule)
	{
		curSel = pParent->m_PropertyList.GetSelectionMark();
		pParent->m_PropertyList.SetItemText(curSel, 0, scenario_name);
	}
	else
	{
		curSel = pParent->m_PropertyList.GetItemCount();
		pParent->m_PropertyList.InsertItem(curSel, scenario_name, 0);
	}

	// let's go ahead and set the info for the current rule
	pParent->m_PropertyList.SetItemText(curSel, 1, rule_set_name);
	pParent->m_PropertyList.SetItemText(curSel, 2, scenario_read_name);
	if (atoi(team_count) == 0)
		pParent->m_PropertyList.SetItemText(curSel, 3, pPParent->m_szYesNo[0]);
	else
		pParent->m_PropertyList.SetItemText(curSel, 3, pPParent->m_szYesNo[1]);
	pParent->m_PropertyList.SetItemText(curSel, 4, team_count);
	pParent->m_PropertyList.SetItemText(curSel, 5, scenario_text);
	if (kill_len)
	{
		if (atoi(kill_count) == 0)
			pParent->m_PropertyList.SetItemText(curSel, 6, pPParent->m_szYesNo[0]);
		else
			pParent->m_PropertyList.SetItemText(curSel, 6, pPParent->m_szYesNo[1]);
		pParent->m_PropertyList.SetItemText(curSel, 7, kill_count);
	}
	else
	{
		pParent->m_PropertyList.SetItemText(curSel, 6, "");
		pParent->m_PropertyList.SetItemText(curSel, 7, "");
	}
	if (respawn_len)
	{
		if (atoi(respawn_count) == 0)
			pParent->m_PropertyList.SetItemText(curSel, 8, pPParent->m_szYesNo[0]);
		else
			pParent->m_PropertyList.SetItemText(curSel, 8, pPParent->m_szYesNo[1]);
		pParent->m_PropertyList.SetItemText(curSel, 9, respawn_count);
	}
	else
	{
		pParent->m_PropertyList.SetItemText(curSel, 8, "");
		pParent->m_PropertyList.SetItemText(curSel, 9, "");
	}
	pParent->m_PropertyList.SetItemText(curSel, 10, night);
	pParent->m_PropertyList.SetItemText(curSel, 11, weather);
	pParent->m_PropertyList.SetItemText(curSel, 12, radar_mode);
	pParent->m_PropertyList.SetItemText(curSel, 13, stock_mech);
	pParent->m_PropertyList.SetItemText(curSel, 14, max_ton);
	pParent->m_PropertyList.SetItemText(curSel, 15, min_ton);
	pParent->m_PropertyList.SetItemText(curSel, 16, game_len);
	pParent->m_PropertyList.SetItemText(curSel, 17, script);

	// just set the team info for the current rule to have no team count
	if (pParent->m_bEditRule == FALSE)
	{
		TEAM_INFO* pTeamInfo = new TEAM_INFO;
		pTeamInfo->team_count = 0;
		pParent->AppendTeamInfo(pTeamInfo);
		
		BOT_INFO* pBotInfo = new BOT_INFO;
		pBotInfo->rule_count = 0;
		pParent->AppendBotInfo(pBotInfo);
	}

	// Done!
	CDialog::OnOK();
}

void CNFORuleDlg::OnSelChangeRuleSet() 
{
	int curSel = m_RuleSet.GetCurSel();
	ASSERT(curSel != -1);
	
	char rule_set_name[256];
	m_RuleSet.GetLBText(curSel, rule_set_name);

	CNFOPropertiesDlg* pParent = (CNFOPropertiesDlg*) GetOwner();
	CNFOFileEditorDlg* pPParent = (CNFOFileEditorDlg*) pParent->GetOwner();

	// find the rule set
	int rule_set = -1;
	for (int i = 0; i < GameTypeCount; i++)
	{
		if (_stricmp(rule_set_name, pPParent->m_szScenarioName[i]) == 0)
		{
			rule_set = i;
			char intbuf[256];

			// do team
			m_TeamCount.ResetContent();
			for (int j = 1; j <= pParent->m_iMaxTeam[i]; j++)
				m_TeamCount.AddString(_itoa(j, intbuf, 10));

			// do script
			m_Script.ResetContent();
			SCRIPT_INFO *pInfo = pParent->m_pScriptInfo[i];
			while (pInfo != NULL)
			{
				m_Script.AddString(pInfo->script);
				pInfo = pInfo->pNext;
			}
			break;
		}
	}
	ASSERT(rule_set != -1);

	if (m_TeamCount.GetCount() == 0)
		m_TeamCount.AddString("0");
	if (m_Script.GetCount() == 0)
	{
		m_Script.AddString("\0");
		m_Script.EnableWindow(FALSE);
	}
	else
	{
		m_Script.SetCurSel(0);
		m_Script.EnableWindow(TRUE);
	}

	m_TeamCount.SetCurSel(m_TeamCount.GetCount() - 1);

	if (pPParent->m_iTeam[rule_set])
	{
		// if it is a team scenario, enable team dropdown
		m_TeamCount.EnableWindow(TRUE);
	}
	else
	{
		m_TeamCount.EnableWindow(FALSE);
	}

	m_ScenarioText.SetSel(0, m_ScenarioText.LineLength());
	m_ScenarioText.ReplaceSel(pPParent->m_szScenarioText[rule_set]);
}
