// GameTypesPanel.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "GameTypesPanel.h"
#include "ObjectManager.h"
#include "AVSDlg.h"

#include <Adept\Entity.hpp>
using namespace Adept;


/////////////////////////////////////////////////////////////////////////////
// CGameTypesPanel dialog


CGameTypesPanel::CGameTypesPanel(ObjectManager *gobj,UndoCommand **cmd,CWnd* pParent)
: CDialog(CGameTypesPanel::IDD, pParent)
{
	ObjMan=gobj;
	CmdList=cmd;
	deleteed=false;
	//{{AFX_DATA_INIT(CGameTypesPanel)
	//}}AFX_DATA_INIT
}


void CGameTypesPanel::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGameTypesPanel)
	DDX_Control(pDX, IDC_SELECTSCRIPTS, m_SelectScripts);
	DDX_Control(pDX, IDC_TEAMS, m_TeamsCon);
	DDX_Control(pDX, IDC_ALIGNMENT, m_AlgnCon);
	DDX_Control(pDX, IDC_GTINCLUDELIST, m_IncludeList);
	DDX_Control(pDX, IDC_GTEXCLUDLIST, m_ExcludeList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CGameTypesPanel, CDialog)
//{{AFX_MSG_MAP(CGameTypesPanel)
ON_BN_CLICKED(IDC_ADDSELECTED, OnAddselected)
ON_BN_CLICKED(IDC_REMOVESELECTED, OnRemoveselected)
ON_LBN_SELCHANGE(IDC_GTINCLUDELIST, OnSelchangeGtincludelist)
ON_CBN_KILLFOCUS(IDC_TEAMS, OnKillfocusTeams)
ON_CBN_KILLFOCUS(IDC_ALIGNMENT, OnKillfocusAlignment)
ON_CBN_SELCHANGE(IDC_TEAMS, OnSelchangeTeams)
	ON_BN_CLICKED(IDC_SELECTSCRIPTS, OnSelectscripts)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGameTypesPanel message handlers
void CGameTypesPanel::OnOK() 
{
	if(deleteed) MessageBox("Removing a game type may require the removal of scenarios in the .NFO file","Warning",MB_OK);
	UpdateData(TRUE);	
	CDialog::OnOK();
}

void CGameTypesPanel::OnCancel() 
{
	CDialog::OnCancel();
}

void CGameTypesPanel::UpdateLists()
{
	int i,itm;
	bool isempty=true;
	
	m_ExcludeList.ResetContent();
	m_IncludeList.ResetContent();
	
	for(i=0;i<ObjectManager::GT_TOTALGAMES;i++)
	{
		
		if(ObjMan->MPGames[i].Supported)
		{
			itm=m_IncludeList.AddString(ObjectManager::MPGameTypeNames[i]);
			m_IncludeList.SetItemData(itm,i);
			isempty=false;
		}
		else
		{
			itm=m_ExcludeList.AddString(ObjectManager::MPGameTypeNames[i]);
			m_ExcludeList.SetItemData(itm,i);
		}
		
	}
	
	if(!isempty)
	{
		m_IncludeList.SetCurSel(0);	
	}
	
}


BOOL CGameTypesPanel::OnInitDialog() 
{
	CDialog::OnInitDialog();
	m_TeamsCon.EnableWindow(FALSE);
	m_AlgnCon.EnableWindow(FALSE);
	
	
	int i,itm;
	CString str;
	for(i=0;i<8;i++)
	{
		str.Format("%i",i);
		itm=m_AlgnCon.AddString(str);
		m_AlgnCon.SetItemData(itm,i);
	}
	
		m_SelectScripts.EnableWindow(FALSE);
	UpdateLists();
	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CGameTypesPanel::OnAddselected() 
{
	int selcount=m_ExcludeList.GetSelCount();	
	if(selcount==0) return;
	int *selitms=new int[selcount];
	m_ExcludeList.GetSelItems(selcount,selitms);
	
	for(int i=0;i<selcount;i++)
	{
		ObjMan->MPGames[m_ExcludeList.GetItemData(selitms[i])].Supported=true;
		ObjMan->MPGames[m_ExcludeList.GetItemData(selitms[i])].AddScenario=true;
	}
	
	delete selitms;
	UpdateLists();
}

void CGameTypesPanel::OnRemoveselected() 
{
	int selcount=m_IncludeList.GetSelCount();	
	if(selcount==0) return;
	int *selitms=new int[selcount];
	m_IncludeList.GetSelItems(selcount,selitms);
	
	for(int i=0;i<selcount;i++)
	{
		ObjMan->MPGames[m_IncludeList.GetItemData(selitms[i])].Supported=false;
		ObjMan->MPGames[m_ExcludeList.GetItemData(selitms[i])].AddScenario=false;
	}
	
	deleteed=true;
	delete selitms;
	UpdateLists();
}

void CGameTypesPanel::OnSelchangeGtincludelist() 
{
	int sel=m_IncludeList.GetCurSel();
	if(sel<0) 
	{
		m_TeamsCon.EnableWindow(FALSE);
		m_AlgnCon.EnableWindow(FALSE);
		return;
	}
	
	m_TeamsCon.EnableWindow(TRUE);
	m_AlgnCon.EnableWindow(TRUE);
	int idx=m_IncludeList.GetItemData(sel);
	
	//	case ObjectManager::GT_CustomDestruction		 :
	//	case ObjectManager::GT_CustomAttrition			 :	
	//	case ObjectManager::GT_CustomTeamDestruction	 :	
	//	case ObjectManager::GT_CustomTeamAttrition		 :	
	//	case ObjectManager::GT_CustomCaptureTheFlag	 :		
	//	case ObjectManager::GT_CustomKingOfTheHill		 :	
	//	case ObjectManager::GT_CustomTeamKingOfTheHill	 :	
	//	case ObjectManager::GT_CustomTerritories		 :		
	//	case ObjectManager::GT_CustomStealTheBacon		 :	
	//	case ObjectManager::GT_CustomCaptureBase		 :		
	//	case ObjectManager::GT_CustomDestroyObjective	 :	
	//	case ObjectManager::GT_CustomEscort			 :				
	//	case ObjectManager::GT_CustomCampaign			 :			
	//	case ObjectManager::GT_CustomMasterTrial		 :			
	//	case ObjectManager::GT_CustomUndefined			 :			  
	
	
	switch(idx)
	{
	case ObjectManager::GT_SinglePlayer			: 		
	case ObjectManager::GT_StockAttrition			: 		
	case ObjectManager::GT_StockEscort				 :	
	case ObjectManager::GT_StockDestruction		: 
	case ObjectManager::GT_StockKingOfTheHill		: 
		m_TeamsCon.EnableWindow(FALSE);
		m_AlgnCon.EnableWindow(FALSE);
		break;	
		
		
		//	case ObjectManager::GT_StockTeamAttrition		: 	
		//	case ObjectManager::GT_StockTeamDestruction	: 		
		//	case ObjectManager::GT_StockTeamKingOfTheHill	: 	
		//	case ObjectManager::GT_StockCaptureTheFlag		: 		
		//	case ObjectManager::GT_StockCaptureBase		: 		
		m_AlgnCon.EnableWindow(FALSE);
		break;
	}

	switch(idx)
	{
			case ObjectManager::GT_CustomDestruction	:	
			case ObjectManager::GT_CustomTeamDestruction	:
			case ObjectManager::GT_CustomAttrition	:		
			case ObjectManager::GT_CustomTeamAttrition:		
			case ObjectManager::GT_CustomCaptureTheFlag	:
			case ObjectManager::GT_CustomKingOfTheHill:		
			case ObjectManager::GT_CustomTeamKingOfTheHill :
			case ObjectManager::GT_CustomTerritories	:	
			case ObjectManager::GT_CustomStealTheBacon:		
			case ObjectManager::GT_CustomCaptureBase	:	
			case ObjectManager::GT_CustomDestroyObjective:	
			case ObjectManager::GT_CustomEscort	:		
			case ObjectManager::GT_CustomMasterTrial:			
			case ObjectManager::GT_CustomSiegeAssault:			
			case ObjectManager::GT_CustomCampaign:			
			case ObjectManager::GT_CustomUndefined:			
			m_SelectScripts.EnableWindow(TRUE);
			break;
			default:
						m_SelectScripts.EnableWindow(FALSE);
			break;
			
	}

	
	
	
	
	m_TeamsCon.SetCurSel(ObjMan->MPGames[idx].Team-1);
	
	int gsel=m_IncludeList.GetCurSel();
	if(gsel>=0)
	{
		int idx=m_IncludeList.GetItemData(gsel);
		
		if(m_TeamsCon.GetCurSel()==0 &&
			(idx==ObjectManager::GT_StockTeamDestruction	||
			idx==ObjectManager::GT_StockTeamAttrition		||	
			idx==ObjectManager::GT_StockTeamKingOfTheHill	||
			idx==ObjectManager::GT_CustomTeamDestruction	||
			idx==ObjectManager::GT_CustomTeamAttrition		||	
			idx==ObjectManager::GT_CustomTeamKingOfTheHill )
			)
		{
			m_TeamsCon.SetCurSel(1);
		}
	}
	

	ObjMan->MPGames[idx].Alignment=ObjMan->MPGames[idx].Alignment<ObjMan->MPGames[idx].Team?ObjMan->MPGames[idx].Alignment:ObjMan->MPGames[idx].Team;
	
	CString str;
	m_AlgnCon.ResetContent();
	
	for(int i=1;i<=ObjMan->MPGames[idx].Team;i++)
	{
		str.Format("%i",i);
		m_AlgnCon.AddString(str);
	}
	
	m_AlgnCon.SetCurSel(ObjMan->MPGames[idx].Alignment-1);
	
	
	UpdateData(FALSE);
}

void CGameTypesPanel::OnKillfocusTeams() 
{
	
	int sel=m_IncludeList.GetCurSel();
	if(sel<0) return;
	
	int idx=m_IncludeList.GetItemData(sel);
	
	
	
	
	UpdateData(TRUE);
	
	
	ObjMan->MPGames[idx].Team=m_TeamsCon.GetCurSel()+1;
	
}

void CGameTypesPanel::OnKillfocusAlignment() 
{
	
	int sel=m_IncludeList.GetCurSel();
	if(sel<0) return;
	
	UpdateData(TRUE);
	
	int idx=m_IncludeList.GetItemData(sel);
	ObjMan->MPGames[idx].Alignment=m_AlgnCon.GetCurSel()+1;
}



void CGameTypesPanel::OnSelchangeTeams() 
{
	int sel=m_IncludeList.GetCurSel();
	int teams=m_TeamsCon.GetCurSel()+1;
	
	int i,itm;
	CString str;
	m_AlgnCon.ResetContent();
	
	int gsel=m_IncludeList.GetCurSel();
	if(gsel>=0)
	{
		int idx=m_IncludeList.GetItemData(gsel);
		
		if(m_TeamsCon.GetCurSel()==0 &&
			(idx==ObjectManager::GT_StockTeamDestruction	||
			idx==ObjectManager::GT_StockTeamAttrition		||	
			idx==ObjectManager::GT_StockTeamKingOfTheHill	||
			idx==ObjectManager::GT_CustomTeamDestruction	||
			idx==ObjectManager::GT_CustomTeamAttrition		||	
			idx==ObjectManager::GT_CustomTeamKingOfTheHill )
			)
		{
			m_TeamsCon.SetCurSel(1);
			sel=1;
		}
	}
	
	for(i=1;i<=teams;i++)
	{
		str.Format("%i",i);
		itm=m_AlgnCon.AddString(str);
		m_AlgnCon.SetItemData(itm,i);
	}
	
	int idx=m_IncludeList.GetItemData(sel);
	ObjMan->MPGames[idx].Alignment=ObjMan->MPGames[idx].Alignment<teams?ObjMan->MPGames[idx].Alignment:teams;
	m_AlgnCon.SetCurSel(ObjMan->MPGames[idx].Alignment-1);
	
}

void CGameTypesPanel::OnSelectscripts() 
{
	int sel=m_IncludeList.GetCurSel();
	int idx=m_IncludeList.GetItemData(sel);
	CAVSDlg dlg(ObjMan->GetName(),&ObjMan->MPGames[idx],this);
	dlg.DoModal();
	
}
