// NFOTeamDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NFO File Editor.h"
#include "NFO File EditorDlg.h"
#include "NFOTeamDlg.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

/////////////////////////////////////////////////////////////////////////////
// CNFOTeamDlg dialog


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


void CNFOTeamDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNFOTeamDlg)
	DDX_Control(pDX, IDC_TEAM_MAX_TON, m_MaxTotalTon);
	DDX_Control(pDX, IDC_TEAM_NUM, m_TeamNum);
	DDX_Control(pDX, IDC_SKIN, m_Skin);
	DDX_Control(pDX, IDC_MIN_TON, m_MinTon);
	DDX_Control(pDX, IDC_MAX_TON, m_MaxTon);
	DDX_Control(pDX, IDC_MAX_COUNT, m_MaxCount);
	DDX_Control(pDX, IDC_ALIGNMENT, m_Alignment);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CNFOTeamDlg message handlers

void CNFOTeamDlg::OnOK() 
{
	// 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;
		}
	}

	CNFOPropertiesDlg* pParent = (CNFOPropertiesDlg*) GetOwner();

	int curSel = -1;
	if (pParent->m_bEditRule)
	{
		curSel = pParent->m_TeamList.GetSelectionMark();
	}
	else
	{
		char team_num[256];
		m_TeamNum.GetLBText(m_TeamNum.GetCurSel(), team_num);
		int cur_num = atoi(team_num);

		// if we are adding, add the new item		
		for (int i = 0; i < pParent->m_TeamList.GetItemCount(); i++)
		{
			char cur_count[256];
			pParent->m_TeamList.GetItemText(i, 0, cur_count, 256);

			if (atoi(cur_count) > cur_num)
			{
				pParent->m_TeamList.InsertItem(i, team_num, 0);
				curSel = i;
				break;
			}
		}

		// append the item at the end of the list
		if (i == pParent->m_TeamList.GetItemCount())
		{
			pParent->m_TeamList.InsertItem(i, team_num, 0);
			curSel = i;
		}
	}
	ASSERT(curSel != -1);

	char max_count[256];
	char max_ton[256];
	char min_ton[256];
	char max_total_ton[256];
	char alignment[256];
	char skin[256];

	// make sure that we get everything from the dialog
	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_MaxTotalTon.GetCurSel() != m_MaxTotalTon.GetCount() - 1)
		m_MaxTotalTon.GetLBText(m_MaxTotalTon.GetCurSel(), max_total_ton);
	else
		max_total_ton[0] = '\0';
	if (m_MaxCount.GetCurSel() != m_MaxCount.GetCount() - 1)
		m_MaxCount.GetLBText(m_MaxCount.GetCurSel(), max_count);
	else
		max_count[0] = '\0';
	if (m_Skin.GetCurSel() != m_Skin.GetCount() - 1)
		m_Skin.GetLBText(m_Skin.GetCurSel(), skin);
	else
		skin[0] = '\0';
	if (m_Alignment.GetCurSel() != m_Alignment.GetCount() - 1)
		m_Alignment.GetLBText(m_Alignment.GetCurSel(), alignment);
	else
		alignment[0] = '\0';

	// store this in the team list
	pParent->m_TeamList.SetItemText(curSel, 1, max_ton);
	pParent->m_TeamList.SetItemText(curSel, 2, min_ton);
	pParent->m_TeamList.SetItemText(curSel, 3, max_total_ton);
	pParent->m_TeamList.SetItemText(curSel, 4, max_count);
	pParent->m_TeamList.SetItemText(curSel, 5, skin);
	pParent->m_TeamList.SetItemText(curSel, 6, alignment);

	// Once we are done setting the text in the team list, we need to save the team data
	int curPSel = pParent->m_PropertyList.GetSelectionMark();
	TEAM_INFO* pTeamdata = pParent->FindTeamInfo(curPSel);
	ASSERT(pTeamdata != NULL);
	int team_count = pParent->m_TeamList.GetItemCount();
	int cur_num = 0;
	for (int i = 0; i < team_count; i++)
	{
		pParent->m_TeamList.GetItemText(i, 1, max_ton, 256);
		pParent->m_TeamList.GetItemText(i, 2, min_ton, 256);
		pParent->m_TeamList.GetItemText(i, 3, max_total_ton, 256);
		pParent->m_TeamList.GetItemText(i, 4, max_count, 256);
		pParent->m_TeamList.GetItemText(i, 5, skin, 256);
		pParent->m_TeamList.GetItemText(i, 6, alignment, 256);

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

		pTeamdata->team_num[cur_num] = cur_num + 1;
		if (max_ton[0] != '\0')
			pTeamdata->max_ton[cur_num] = atoi(max_ton);
		else
			pTeamdata->max_ton[cur_num] = -1;
		if (min_ton[0] != '\0')
			pTeamdata->min_ton[cur_num] = atoi(min_ton);
		else
			pTeamdata->min_ton[cur_num] = -1;
		if (max_total_ton[0] != '\0')
			pTeamdata->max_total_ton[cur_num] = atoi(max_total_ton);
		else
			pTeamdata->max_total_ton[cur_num] = -1;
		if (max_count[0] != '\0')
			pTeamdata->max_team_count[cur_num] = atoi(max_count);
		else
			pTeamdata->max_team_count[cur_num] = -1;
		if (skin[0] != '\0')
			pTeamdata->skin[cur_num] = skin[0];
		else
			pTeamdata->skin[cur_num] = -1;
		if (alignment[0] != '\0')
			pTeamdata->alignment[cur_num] = atoi(alignment);
		else
			pTeamdata->alignment[cur_num] = -1;
	}
	pTeamdata->team_count = cur_num + 1;

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

BOOL CNFOTeamDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	CNFOPropertiesDlg* pParent = (CNFOPropertiesDlg*) GetOwner();
	CNFOFileEditorDlg* pPParent = (CNFOFileEditorDlg*) pParent->GetOwner();
	
	int i;
	char rule_set_name[256];
	pParent->m_PropertyList.GetItemText(pParent->m_PropertyList.GetSelectionMark(), 1, rule_set_name, 256);
	int rule_set = -1;
	for (i = 0; i < GameTypeCount; i++)
	{
		if (_stricmp(rule_set_name, pPParent->m_szScenarioName[i]) == 0)
		{
			rule_set = i;
			break;
		}
	}
	ASSERT(rule_set != -1);

	// inits all the options
	char intbuf[256];
	for (i = 1; i < 17; i++)
		m_MaxCount.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 = 100; i < 1700; i = i + 100)
		m_MaxTotalTon.AddString(_itoa(i, intbuf, 10));
	for (i = 0; i < pParent->m_iMaxAlignment[rule_set]; i++)
		m_Alignment.AddString(_itoa(i, intbuf, 10));	
	// 0 to 9 and a to z
	for (i = 48; i < 58; i++)
	{
		char skin[2];
		skin[0] = (char) i;
		skin[1] = '\0';
		m_Skin.AddString(skin);
	}
	for (i = 97; i < 123; i++)
	{
		char skin[2];
		skin[0] = (char) i;
		skin[1] = '\0';
		m_Skin.AddString(skin);	
	}
	m_MaxCount.AddString(pPParent->m_szDefault);
	m_MaxTon.AddString(pPParent->m_szDefault);
	m_MinTon.AddString(pPParent->m_szDefault);
	m_MaxTotalTon.AddString(pPParent->m_szDefault);
	m_Alignment.AddString(pPParent->m_szDefault);
	m_Skin.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_TeamList.GetSelectionMark();

		char team_num[256];
		char max_count[256];
		char max_ton[256];
		char min_ton[256];
		char max_total_ton[256];
		char alignment[256];
		char skin[256];

		pParent->m_TeamList.GetItemText(curSel, 0, team_num, 256);
		pParent->m_TeamList.GetItemText(curSel, 1, max_ton, 256);
		pParent->m_TeamList.GetItemText(curSel, 2, min_ton, 256);
		pParent->m_TeamList.GetItemText(curSel, 3, max_total_ton, 256);
		pParent->m_TeamList.GetItemText(curSel, 4, max_count, 256);
		pParent->m_TeamList.GetItemText(curSel, 5, skin, 256);
		pParent->m_TeamList.GetItemText(curSel, 6, alignment, 256);

		int found = m_MaxCount.FindStringExact(0, max_count);
		if (found != CB_ERR)
			m_MaxCount.SetCurSel(found);
		else
			m_MaxCount.SetCurSel(m_MaxCount.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_MaxTotalTon.FindStringExact(0, max_total_ton);
		if (found != CB_ERR)
			m_MaxTotalTon.SetCurSel(found);
		else
			m_MaxTotalTon.SetCurSel(m_MaxTotalTon.GetCount() - 1);
		found = m_Alignment.FindStringExact(0, alignment);
		if (found != CB_ERR)
			m_Alignment.SetCurSel(found);
		else
			m_Alignment.SetCurSel(m_Alignment.GetCount() - 1);
		found = m_Skin.FindStringExact(0, skin);
		if (found != CB_ERR)
			m_Skin.SetCurSel(found);
		else
			m_Skin.SetCurSel(m_Skin.GetCount() - 1);
		
		// set the current team rule number
		m_TeamNum.AddString(team_num);
		m_TeamNum.SetCurSel(0);
		m_TeamNum.EnableWindow(FALSE);
	}
	else
	{
		// we are adding a new one, so set them to default
		curSel = pParent->m_TeamList.GetItemCount();
		m_MaxCount.SetCurSel(m_MaxCount.GetCount() - 1);
		m_MaxTon.SetCurSel(m_MaxTon.GetCount() - 1);
		m_MinTon.SetCurSel(m_MinTon.GetCount() - 1);
		m_MaxTotalTon.SetCurSel(m_MaxTotalTon.GetCount() - 1);
		m_Alignment.SetCurSel(m_Alignment.GetCount() - 1);
		m_Skin.SetCurSel(m_Skin.GetCount() - 1);
		
		// set the current team rule number
		char team_count[256];
		pParent->m_PropertyList.GetItemText(pParent->m_PropertyList.GetSelectionMark(), 4, team_count, 256);
		int count = atoi(team_count);
		for (int i = 0; i < count; i++)
		{
			char cur_count[256];
			itoa(i + 1, cur_count, 10);

			LVFINDINFO f_info;
			f_info.flags = LVFI_STRING;
			f_info.psz = cur_count;
			if (pParent->m_TeamList.FindItem(&f_info) == -1)
			{
				m_TeamNum.AddString(cur_count);
			}
		}
		m_TeamNum.SetCurSel(0);
	}

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