// OpenMissionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MW4GameEd2.h"
#include "OpenMissionDlg.h"
#include "GameINterface.h"

/////////////////////////////////////////////////////////////////////////////
// COpenMissionDlg dialog


COpenMissionDlg::COpenMissionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COpenMissionDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COpenMissionDlg)
	m_MissionName = _T("");
	//}}AFX_DATA_INIT
}


void COpenMissionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COpenMissionDlg)
	DDX_Control(pDX, IDC_MISSIONLIST, m_MissionList);
	DDX_LBString(pDX, IDC_MISSIONLIST, m_MissionName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COpenMissionDlg, CDialog)
	//{{AFX_MSG_MAP(COpenMissionDlg)
	ON_LBN_SELCHANGE(IDC_MISSIONLIST, OnSelchangeMissionlist)
	ON_LBN_DBLCLK(IDC_MISSIONLIST, OnDblclkMissionlist)
	ON_BN_CLICKED(IDC_DAY, OnDay)
	ON_BN_CLICKED(IDC_NIGHT, OnNight)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COpenMissionDlg message handlers

void COpenMissionDlg::OnOK() 
{
	LoadAtNight=IsDlgButtonChecked(IDC_NIGHT)?true:false;
	CDialog::OnOK();
}

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

BOOL COpenMissionDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	CheckDlgButton(IDC_DAY,1);
	CheckDlgButton(IDC_NIGHT,0);
	WIN32_FIND_DATA file_data;
	HANDLE file_find;

	// [editor data fix] List ALL source missions under Content\Missions\ -- every subdirectory
	// that contains a matching <name>.instance, which is exactly what CMainFrame::LoadMission
	// loads (Content\Missions\<name>\<name>.Instance). The original code listed only
	// Resource\UserMissions\*.mw4 (compiled/published user-mission packages), so the editor
	// showed just the handful of published missions instead of all ~50 source missions.
	file_find=FindFirstFile("Content\\Missions\\*",&file_data);
	if(file_find != INVALID_HANDLE_VALUE)
	{
		do
		{
			if( !(file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
				continue;					// only directories
			if( file_data.cFileName[0]=='.' )
				continue;					// skip "." and ".."
			CString mis_name = file_data.cFileName;
			CString inst;
			inst.Format("Content\\Missions\\%s\\%s.instance", mis_name, mis_name);
			if( GetFileAttributes(inst) == 0xFFFFFFFF )		// INVALID_FILE_ATTRIBUTES (not defined in VC6 SDK)
				continue;					// only dirs with a loadable <name>.instance
			m_MissionList.AddString(mis_name);
		}
		while (FindNextFile(file_find,&file_data));
		FindClose(file_find);
	}
	m_MissionList.SetCurSel(0);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void COpenMissionDlg::OnSelchangeMissionlist() 
{
	
}

void COpenMissionDlg::OnDblclkMissionlist() 
{
	UpdateData(TRUE);	
	OnOK();
}

void COpenMissionDlg::OnDay() 
{
	// TODO: Add your control notification handler code here
	
}

void COpenMissionDlg::OnNight() 
{
	// TODO: Add your control notification handler code here
	
}
