// NewMissionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed.h"
#include "NewMissionDlg.h"
#include "GenericListDlg.h"
#include <direct.h>
#include <errno.h>

//extern int _errno;
extern CMW4GameEdApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CNewMissionDlg dialog


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

	m_MapFilePath = "";
	m_SkyFilePath = "";
}


void CNewMissionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNewMissionDlg)
	DDX_Control(pDX, IDC_MISSIONSKYPATH, m_SkyPath);
	DDX_Control(pDX, IDC_MISSIONNAMEEDIT, m_Name);
	DDX_Control(pDX, IDC_MISSIONMAPPATH, m_MapPath);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CNewMissionDlg, CDialog)
	//{{AFX_MSG_MAP(CNewMissionDlg)
	ON_BN_CLICKED(IDC_MISSIONMAPPATHBTN, OnMissionmappathbtn)
	ON_BN_CLICKED(IDC_MISSIONSKYPATHBTN, OnMissionskypathbtn)
	ON_EN_CHANGE(IDC_MISSIONNAMEEDIT, OnChangeMissionnameedit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNewMissionDlg message handlers

void CNewMissionDlg::OnOK() 
{
	if (theApp.m_editorState != CMW4GameEdApp::NoGameIdleState)
	{
		theApp.OnFileClose();
	}

	CString missionName;

	m_Name.GetWindowText(missionName);
	missionName.TrimRight();
	if (!missionName)
	{
		MessageBox("You must name a mission before it can be created.","Mission Creation Error");	// Localize
		return;
	}
	if (missionName.GetLength() > 32)
	{
		MessageBox("The mission name is too long.","Mission Creation Error");	// Localize
		return;
	}

	if (!m_MapFilePath)
	{
		MessageBox("You must select a map to create a mission.","Mission Creation Error");	// Localize
		return;
	}
	if (!m_SkyFilePath)
	{
		MessageBox("You must select a sky to create a mission.","Mission Creation Error");	// Localize
		return;
	}

	char oldDir[MAX_PATH];
	_getcwd(oldDir,MAX_PATH - 1);

	CString missionPath = "Content\\Missions\\";
	missionPath += missionName;

	if (_chdir(missionPath) == 0)
	{
		MessageBox("A mission with that name already exists. Please rename the mission.","New Mission Error");	// Localize
		_chdir(oldDir);
		return;
	}
	else
	{
		theApp.m_MissionData.InitValues();
		if (theApp.CreateNewScenario(missionName.GetBuffer(MAX_PATH),m_MapFilePath.GetBuffer(MAX_PATH),m_SkyFilePath.GetBuffer(MAX_PATH)))
		{
			char missionInstance[MAX_PATH];
			sprintf(missionInstance,"Content\\Missions\\%s\\%s.instance",missionName.GetBuffer(MAX_PATH),missionName.GetBuffer(MAX_PATH));
			theApp.OpenFile(missionInstance);
		}
	}
	CDialog::OnOK();
}

void CNewMissionDlg::OnMissionmappathbtn() 
{
	CGenericListDlg	dlg;

	dlg.m_Title = "Select Map";					// localize
	dlg.m_Path = "Content\\Maps";
	dlg.m_Extension = "instance";

	if (dlg.DoModal() == IDOK)
	{
		m_MapFilePath = dlg.m_Path;
		m_MapPath.SetWindowText(dlg.m_Name);
	}
}

void CNewMissionDlg::OnMissionskypathbtn() 
{
	CGenericListDlg	dlg;

	dlg.m_Title = "Select Sky";					// localize
	dlg.m_Path = "Content\\Skies";
	dlg.m_Extension = "erf";

	if (dlg.DoModal() == IDOK)
	{
		m_SkyFilePath = dlg.m_Path;
		m_SkyPath.SetWindowText(dlg.m_Name);
	}
}

void CNewMissionDlg::OnChangeMissionnameedit() 
{
}

BOOL CNewMissionDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
		
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
