// NFOBotDlg.cpp : implementation file
//

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

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

/////////////////////////////////////////////////////////////////////////////
// CNFOBotDlg dialog


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


void CNFOBotDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNFOBotDlg)
	DDX_Control(pDX, IDC_BOT_RULE, m_RuleNum);
	DDX_Control(pDX, IDC_BOT_NUM, m_BotCount);
	DDX_Control(pDX, IDC_TEAM, m_Team);
	DDX_Control(pDX, IDC_MIN_TON, m_MinTon);
	DDX_Control(pDX, IDC_MIN_LEVEL, m_MinLevel);
	DDX_Control(pDX, IDC_MAX_TON, m_MaxTon);
	DDX_Control(pDX, IDC_MAX_LEVEL, m_MaxLevel);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CNFOBotDlg, CDialog)
	//{{AFX_MSG_MAP(CNFOBotDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNFOBotDlg message handlers

void CNFOBotDlg::OnOK() 
{
	CNFOPropertiesDlg* pParent = (CNFOPropertiesDlg*) GetOwner();

	// check the validity of the min and max tonnage
	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;
		}
	}
	
	// check the validity of the min and max level
	if ((m_MinLevel.GetCurSel() != m_MinLevel.GetCount() - 1) &&
		(m_MaxLevel.GetCurSel() != m_MaxLevel.GetCount() - 1))
	{
		if (m_MinLevel.GetCurSel() > m_MaxLevel.GetCurSel())
		{
			CNFOErrorDlg dlg;
			dlg.m_Error = "The minimum AI level cannot be larger than the maximum AI level.";
			dlg.DoModal();
			return;
		}
	}

	char bot_count[256];
	char team[256];
	char max_ton[256];
	char min_ton[256];
	char max_level[256];
	char min_level[256];

	// make sure that we get everything from the dialog	
	m_BotCount.GetLBText(m_BotCount.GetCurSel(), bot_count);
	if (m_Team.GetCurSel() != -1)
		m_Team.GetLBText(m_Team.GetCurSel(), team);
	else
		team[0] = '\0';
	if (m_MaxTon.GetCurSel() != m_MaxTon.GetCount() - 1)
		m_MaxTon.GetLBText(m_MaxTon.GetCurSel(), max_ton);
	else
		max_ton[0] = '\0';
	if (m_MinTon.GetCurSel() != m_MinTon.GetCount() - 1)
		m_MinTon.GetLBText(m_MinTon.GetCurSel(), min_ton);
	else
		min_ton[0] = '\0';	
	if (m_MaxLevel.GetCurSel() != m_MaxLevel.GetCount() - 1)
		m_MaxLevel.GetLBText(m_MaxLevel.GetCurSel(), max_level);
	else
		max_level[0] = '\0';
	if (m_MinLevel.GetCurSel() != m_MinLevel.GetCount() - 1)
		m_MinLevel.GetLBText(m_MinLevel.GetCurSel(), min_level);
	else
		min_level[0] = '\0';

	// check the validity of the team min and max level against the bots
	if (team[0] != '\0')
	{
		int team_num = atoi(team) - 1;
		
		TEAM_INFO* pInfo = pParent->FindTeamInfo(pParent->m_PropertyList.GetSelectionMark());
		ASSERT(pInfo);

		for (int i = 0; i < pInfo->team_count; i++)
		{
			if ((team_num + 1) == pInfo->team_num[team_num])
			{
				if ((min_ton[0] != '\0') && (pInfo->max_ton[team_num] != -1))
				{
					int min_ton_num = atoi(min_ton);
					if (min_ton_num > pInfo->max_ton[team_num])
					{
						char team_max_ton[256];
						_itoa(pInfo->max_ton[team_num], team_max_ton, 10);

						CNFOErrorDlg dlg;
						dlg.m_Error = "The minimum bot tonnage cannot be larger than the maximum team tonnage of ";
						dlg.m_Error += team_max_ton;
						dlg.m_Error += ".";
						dlg.DoModal();
						return;
					}
				}
				
				if ((max_ton[0] != '\0') && (pInfo->min_ton[team_num] != -1))
				{
					int max_ton_num = atoi(max_ton);
					if (max_ton_num < pInfo->min_ton[team_num])
					{
						char team_min_ton[256];
						_itoa(pInfo->min_ton[team_num], team_min_ton, 10);

						CNFOErrorDlg dlg;
						dlg.m_Error = "The maximum bot tonnage cannot be smaller than the minimum team tonnage of ";
						dlg.m_Error += team_min_ton;
						dlg.m_Error += ".";
						dlg.DoModal();
						return;
					}
				}

				break;
			}
		}
	}

	// store this in the team list
	int curSel;
	if (pParent->m_bEditRule)
	{
		curSel = pParent->m_BotList.GetSelectionMark();
	}
	else
	{
		char intbuf[256];
		curSel = pParent->m_BotList.GetItemCount();
		pParent->m_BotList.InsertItem(curSel, _itoa(curSel, intbuf, 10), 0);
	}

	pParent->m_BotList.SetItemText(curSel, 1, bot_count);
	pParent->m_BotList.SetItemText(curSel, 2, team);
	pParent->m_BotList.SetItemText(curSel, 3, max_ton);
	pParent->m_BotList.SetItemText(curSel, 4, min_ton);
	pParent->m_BotList.SetItemText(curSel, 5, max_level);
	pParent->m_BotList.SetItemText(curSel, 6, min_level);

	// Once we are done setting the text in the team list, we need to save the team data
	int curPSel = pParent->m_PropertyList.GetSelectionMark();
	BOT_INFO* pBotdata = pParent->FindBotInfo(curPSel);
	ASSERT(pBotdata != NULL);
	int count = pParent->m_BotList.GetItemCount();
	for (int i = 0; i < count; i++)
	{
		pParent->m_BotList.GetItemText(i, 1, bot_count, 256);
		pParent->m_BotList.GetItemText(i, 2, team, 256);
		pParent->m_BotList.GetItemText(i, 3, max_ton, 256);
		pParent->m_BotList.GetItemText(i, 4, min_ton, 256);
		pParent->m_BotList.GetItemText(i, 5, max_level, 256);
		pParent->m_BotList.GetItemText(i, 6, min_level, 256);

		char cur_count[256];
		pParent->m_BotList.GetItemText(i, 0, cur_count, 256);
		int cur_num = atoi(cur_count);

		if (bot_count[0] != '\0')
			pBotdata->bot_count[cur_num] = atoi(bot_count);
		else
			pBotdata->bot_count[cur_num] = -1;
		if (team[0] != '\0')
			pBotdata->team_num[cur_num] = atoi(team);
		else
			pBotdata->team_num[cur_num] = -1;
		if (max_ton[0] != '\0')
			pBotdata->max_ton[cur_num] = atoi(max_ton);
		else
			pBotdata->max_ton[cur_num] = -1;
		if (min_ton[0] != '\0')
			pBotdata->min_ton[cur_num] = atoi(min_ton);
		else
			pBotdata->min_ton[cur_num] = -1;
		if (max_level[0] != '\0')
			pBotdata->max_level[cur_num] = atoi(max_level);
		else
			pBotdata->max_level[cur_num] = -1;
		if (min_level[0] != '\0')
			pBotdata->min_level[cur_num] = atoi(min_level);
		else
			pBotdata->min_level[cur_num] = -1;
	}
	pBotdata->rule_count = count;
	
	CDialog::OnOK();
}

BOOL CNFOBotDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CNFOPropertiesDlg* pParent = (CNFOPropertiesDlg*) GetOwner();
	CNFOFileEditorDlg* pPParent = (CNFOFileEditorDlg*) pParent->GetOwner();
	
	char intbuf[256];
	int i;
	
	// Handle team option first
	char team_allowed[256];
	char team_count[256];
	pParent->m_PropertyList.GetItemText(pParent->m_PropertyList.GetSelectionMark(), 3, team_allowed, 256);
	pParent->m_PropertyList.GetItemText(pParent->m_PropertyList.GetSelectionMark(), 4, team_count, 256);
	if (_stricmp(team_allowed, pPParent->m_szYesNo[0]) == 0)
	{
		m_Team.SetCurSel(-1);
		m_Team.EnableWindow(FALSE);
	}
	else
	{
		for (i = 1; i < atoi(team_count) + 1; i++)
			m_Team.AddString(_itoa(i, intbuf, 10));
		m_Team.SetCurSel(0);
	}

	// inits all the options
	for (i = 1; i < 15; i++)
		m_BotCount.AddString(_itoa(i, intbuf, 10));
	for (i = 25; i <= 100; i += 5)
	{
		m_MaxTon.AddString(_itoa(i, intbuf, 10));
		m_MinTon.AddString(_itoa(i, intbuf, 10));
	}
	for (i = 0; i < 10; i++)
	{
		m_MaxLevel.AddString(_itoa(i, intbuf, 10));
		m_MinLevel.AddString(_itoa(i, intbuf, 10));
	}
	m_MaxTon.AddString(pPParent->m_szDefault);
	m_MinTon.AddString(pPParent->m_szDefault);
	m_MaxLevel.AddString(pPParent->m_szDefault);
	m_MinLevel.AddString(pPParent->m_szDefault);

	// now either grab the info or set to default
	int curSel;
	if (pParent->m_bEditRule)
	{
		// we are editing, so grab the info
		curSel = pParent->m_BotList.GetSelectionMark();

		char rule_num[256];
		char bot_count[256];
		char team[256];
		char max_ton[256];
		char min_ton[256];
		char max_level[256];
		char min_level[256];

		pParent->m_BotList.GetItemText(curSel, 0, rule_num, 256);
		pParent->m_BotList.GetItemText(curSel, 1, bot_count, 256);
		pParent->m_BotList.GetItemText(curSel, 2, team, 256);
		pParent->m_BotList.GetItemText(curSel, 3, max_ton, 256);
		pParent->m_BotList.GetItemText(curSel, 4, min_ton, 256);
		pParent->m_BotList.GetItemText(curSel, 5, max_level, 256);
		pParent->m_BotList.GetItemText(curSel, 6, min_level, 256);

		int found = m_BotCount.FindStringExact(0, bot_count);
		if (found != CB_ERR)
			m_BotCount.SetCurSel(found);
		else
			m_BotCount.SetCurSel(0);
		found = m_Team.FindStringExact(0, team);
		if (found != CB_ERR)
			m_Team.SetCurSel(found);
		else
			m_Team.SetCurSel(0);
		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_MaxLevel.FindStringExact(0, max_level);
		if (found != CB_ERR)
			m_MaxLevel.SetCurSel(found);
		else
			m_MaxLevel.SetCurSel(m_MaxLevel.GetCount() - 1);
		found = m_MinLevel.FindStringExact(0, min_level);
		if (found != CB_ERR)
			m_MinLevel.SetCurSel(found);
		else
			m_MinLevel.SetCurSel(m_MinLevel.GetCount() - 1);
		
		// set the current team rule number
		m_RuleNum.ReplaceSel(rule_num);
	}
	else
	{
		// we are adding a new one, so set them to default
		curSel = pParent->m_BotList.GetItemCount();
		m_BotCount.SetCurSel(0);
		m_MaxTon.SetCurSel(m_MaxTon.GetCount() - 1);
		m_MinTon.SetCurSel(m_MinTon.GetCount() - 1);
		m_MaxLevel.SetCurSel(m_MaxLevel.GetCount() - 1);
		m_MinLevel.SetCurSel(m_MinLevel.GetCount() - 1);
		
		// set the current team rule number
		m_RuleNum.ReplaceSel(_itoa(curSel, intbuf, 10));
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
