// BatchReportDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "BatchReportDlg.h"
#include "GameInterface.h"

/////////////////////////////////////////////////////////////////////////////
// CBatchReportDlg dialog


CBatchReportDlg::CBatchReportDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBatchReportDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBatchReportDlg)
	m_DestPath = _T("");
	//}}AFX_DATA_INIT
	MissionNames=NULL;
	 StringCount=0;
	
}


void CBatchReportDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBatchReportDlg)
	DDX_Control(pDX, IDC_MISSIONLIST, m_MissionList);
	DDX_Text(pDX, IDC_DESTPATH, m_DestPath);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBatchReportDlg, CDialog)
	//{{AFX_MSG_MAP(CBatchReportDlg)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBatchReportDlg message handlers

BOOL CBatchReportDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	WIN32_FIND_DATA file_data;
	HANDLE file_find;

	file_find=FindFirstFile("Resource\\UserMissions\\*.mw4",&file_data);
	
	if(file_find != INVALID_HANDLE_VALUE)
	{
		CString mis_name;
		do
		{
			mis_name.Format("Resource\\UserMissions\\%s", file_data.cFileName);
			TranslateMissionNameBackIfNecessary(mis_name);
			mis_name=mis_name.Left(mis_name.ReverseFind('.'));
			if (mis_name.ReverseFind('\\') >= 0)
				mis_name=mis_name.Right(mis_name.GetLength() - (mis_name.ReverseFind('\\')+1));
			m_MissionList.AddString(mis_name);
		}
		while (FindNextFile(file_find,&file_data));
	}

	int i;
	for(i=0;i<m_MissionList.GetCount();i++)
	{
	m_MissionList.SetSel(i,TRUE);
	}

	char pbuf[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,pbuf);
	m_DestPath=pbuf;
	
	UpdateData(FALSE);

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

void CBatchReportDlg::OnBrowse() 
{
	char pname[MAX_PATH];
	BROWSEINFO bi; 
	
	bi.hwndOwner = this->m_hWnd; 
	bi.pidlRoot = NULL; 
	bi.pszDisplayName = NULL; 
	bi.lpszTitle = "Choose Game Path..."; 
	bi.ulFlags = BIF_RETURNONLYFSDIRS; 
	bi.lpfn = NULL; 
	bi.lParam = (LPARAM)this; 
	
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi); 
	
	if (SHGetPathFromIDList(pidl,pname))  
	{
		m_DestPath=pname;
	}
	
	if (bi.pidlRoot) 
	{ 
		LPITEMIDLIST pidl= const_cast< ITEMIDLIST* >( bi.pidlRoot ) ;
		LPMALLOC pMalloc; 
		
		if (pidl) 
		{ 
			SHGetMalloc(&pMalloc); 
			pMalloc->Free( pidl); 
			pMalloc->Release(); 
			
		} 
		
	} 
	
	UpdateData(FALSE);	
		
}

void CBatchReportDlg::OnOK() 
{
if(m_MissionList.GetSelCount()>0)
{ 
	StringCount=0;
	MissionNames=new CString[m_MissionList.GetSelCount()];
	for(int i=0;i<m_MissionList.GetCount();i++)
{
if(m_MissionList.GetSel(i))
	{
	m_MissionList.GetText(i,MissionNames[StringCount++]);
	}
}
}
	CDialog::OnOK();
}
