// AVSDlg.cpp : implementation file
//

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



/////////////////////////////////////////////////////////////////////////////
// CAVSDlg dialog


CAVSDlg::CAVSDlg(CString mname,MPGame *gme,CWnd* pParent /*=NULL*/)
: CDialog(CAVSDlg::IDD, pParent)
{
	MyGame=gme;
	MisName=mname;
	//{{AFX_DATA_INIT(CAVSDlg)
	//}}AFX_DATA_INIT
}


void CAVSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAVSDlg)
	DDX_Control(pDX, IDC_INCLUDELIST, m_IncludeList);
	DDX_Control(pDX, IDC_EXCLUDLIST, m_ExcludeList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAVSDlg, CDialog)
//{{AFX_MSG_MAP(CAVSDlg)
	ON_BN_CLICKED(IDC_ADDSELECTED, OnAddselected)
	ON_BN_CLICKED(IDC_REMOVESELECTED, OnRemoveselected)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAVSDlg message handlers

BOOL CAVSDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	WIN32_FIND_DATA file_data;
	HANDLE file_find;
	file_find=FindFirstFile("Content\\Missions\\"+MisName+"\\Scripts\\*.abl",&file_data);
	
	if(file_find != INVALID_HANDLE_VALUE)
	{
		CString fname;
		do
		{
			fname=file_data.cFileName;
			fname=fname.Left(fname.ReverseFind('.'));
			m_ExcludeList.AddString(fname);
		}
		while (FindNextFile(file_find,&file_data));
	}

	


	// TODO: Add extra initialization here
	for(int i=0;i<MyGame->ScriptTotal;i++)
	{
		int eval=m_ExcludeList.FindString(-1,MyGame->ScriptPaths[i]);
		if(eval!=LB_ERR)
		{
		m_ExcludeList.DeleteString(eval);
		m_IncludeList.AddString(MyGame->ScriptPaths[i]);
		}
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CAVSDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	MyGame->ScriptTotal=m_IncludeList.GetCount();
	for(int i=0;i<MyGame->ScriptTotal;i++)
	{
		m_IncludeList.GetText(i,MyGame->ScriptPaths[i]);
	}
	
	
	CDialog::OnOK();
}

void CAVSDlg::OnAddselected() 
{

	int selcount=m_ExcludeList.GetSelCount();	
	if(selcount==0) return;
	int *selitms=new int[selcount];
	m_ExcludeList.GetSelItems(selcount,selitms);
	CString str;


	int i;

	for(i=0;i<selcount;i++)
	{
		m_ExcludeList.GetText(selitms[i],str);
		m_IncludeList.AddString(str);
	}

	for( i=0;i<m_IncludeList.GetCount();i++)
	{
		m_IncludeList.GetText(i,str);
		int idx=m_ExcludeList.FindString(-1,str);
		if(idx!=LB_ERR)
			m_ExcludeList.DeleteString(idx);
	}


	delete selitms;


}

void CAVSDlg::OnRemoveselected() 
{

	int selcount=m_IncludeList.GetSelCount();	
	if(selcount==0) return;
	int *selitms=new int[selcount];
	m_IncludeList.GetSelItems(selcount,selitms);
	CString str;


	int i;

	for(i=0;i<selcount;i++)
	{
		m_IncludeList.GetText(selitms[i],str);
		m_ExcludeList.AddString(str);
	}

	for( i=0;i<m_ExcludeList.GetCount();i++)
	{
		m_ExcludeList.GetText(i,str);
		int idx=m_IncludeList.FindString(-1,str);
		if(idx!=LB_ERR)
			m_IncludeList.DeleteString(idx);
	}


	delete selitms;
	
}
