// OpenMapFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MW4GameEd.h"
#include "OpenMapFileDlg.h"
#include <direct.h>
#include <io.h>

/////////////////////////////////////////////////////////////////////////////
// COpenMapFileDlg dialog
extern CMW4GameEdApp theApp;


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

	contentFile[0] = 0;
	LoadString(theApp.m_hInstance,IDS_DEFAULTMAPDLGTITLE,title,MAX_PATH);
	deleteEnabled = TRUE;
}


void COpenMapFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COpenMapFileDlg)
	DDX_Control(pDX, IDCANCEL, m_CancelButton);
	DDX_Control(pDX, IDC_DELETEFILE, m_DeleteButton);
	DDX_Control(pDX, IDC_FILELIST, m_FileList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COpenMapFileDlg, CDialog)
	//{{AFX_MSG_MAP(COpenMapFileDlg)
	ON_BN_CLICKED(IDC_DELETEFILE, OnDeleteFile)
	ON_BN_CLICKED(IDOK, OnOpenFile)
	ON_LBN_DBLCLK(IDC_FILELIST, OnDblclkFileList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// COpenMapFileDlg message handlers

void COpenMapFileDlg::OnDeleteFile() 
{
	NotationFile infile("Content\\Games\\UserGames.contents");


	// Find the entry to delete

	char file[MAX_PATH];
	const char *entryData;
	char entryName[16];
	int c = m_FileList.GetItemData(m_FileList.GetCurSel());

	m_FileList.DeleteString(m_FileList.GetCurSel());

	sprintf(entryName,"EntryData%d",c);
	Page *user_page = infile.GetPage("UserGames");
	Check_Object(user_page);
	user_page->GetEntry(entryName,&entryData);
	strcpy(file,entryData);
	user_page->DeleteNote(entryName);
	sprintf(entryName,"EntryText%d",c);
	user_page->DeleteNote(entryName);

	
	// Delete all the associated files

	struct _finddata_t fileInfo;
	char oldDir[MAX_PATH];
	char drive[_MAX_DRIVE],dir[_MAX_DIR],fname[_MAX_FNAME],ext[_MAX_EXT];

	_splitpath(file,drive,dir,fname,ext);
	_getcwd(oldDir,MAX_PATH);
	_chdir(dir);

	long fhand = _findfirst("*.*",&fileInfo);
	remove(fileInfo.name);
	int i = 0;
	while(!i)
	{
		i = _findnext(fhand,&fileInfo);
		remove(fileInfo.name);
	}
	_chdir(oldDir);
	_rmdir(dir);


	// Now we deal with the fact our numbers are all messed up

	Page::NoteIterator *user_games = user_page->MakeNoteIterator();
	Check_Object(user_games);
	i = user_games->GetSize() / 2;

	while (c < i)
	{
		sprintf(entryName,"EntryText%d",c+1);
		user_page->GetEntry(entryName,&entryData);
		strcpy(file,entryData);
		user_page->DeleteNote(entryName);
		sprintf(entryName,"EntryText%d",c);
		user_page->AppendEntry(entryName,file);

		sprintf(entryName,"EntryData%d",c+1);
		user_page->GetEntry(entryName,&entryData);
		strcpy(file,entryData);
		user_page->DeleteNote(entryName);
		sprintf(entryName,"EntryData%d",c);
		user_page->AppendEntry(entryName,file);

		c++;
	}
	delete user_games;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void COpenMapFileDlg::OnOpenFile() 
{
	NotationFile infile(contentFile);

	char entryName[16];
	const char *tabName;

	sprintf(entryName,"EntryData%d",m_FileList.GetItemData(m_FileList.GetCurSel()));
	Page *user_page = infile.GetPage("UserGames");
	Check_Object(user_page);
	user_page->GetEntry(entryName,&tabName);

	char drive[_MAX_DRIVE],dir[_MAX_DIR],fname[_MAX_FNAME],ext[_MAX_EXT],oldDir[MAX_PATH];
	_splitpath(tabName,drive,dir,fname,ext);

	_getcwd(oldDir,MAX_PATH);

	if (deleteEnabled && _chdir(dir))	// if !create and chdir failed
	{
		char string[MAX_PATH],title[MAX_PATH];
		LoadString(theApp.m_hInstance,IDS_OPENSCENARIOERRORTITLE,title,MAX_PATH);
		LoadString(theApp.m_hInstance,IDS_MAPDELETEDERROR,string,MAX_PATH);
		MessageBox(string,title);
		OnDeleteFile();
		_chdir(oldDir);
		SetUpListBox();
		return;
	}
	_chdir(oldDir);
	strcpy(contentFile,tabName);
	EndDialog(IDOK);	
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void COpenMapFileDlg::SetUpListBox()
{
	while (m_FileList.DeleteString(0) > 0);
	
	NotationFile infile(contentFile);

	char entryName[16];
	const char *tab_name;

	Page *user_page = infile.GetPage("UserGames");
	Check_Object(user_page);
	user_page->GetEntry("TabName",&tab_name);

	int e = 0;
	while (1)
	{
		sprintf(entryName,"EntryText%d",e);
		if (!user_page->GetEntry(entryName,&tab_name))
			break;

		m_FileList.AddString(tab_name);
		m_FileList.SetItemData(m_FileList.FindStringExact(0,tab_name),e);
		e++;
	}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
BOOL COpenMapFileDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	SetWindowText(title);

	if (!deleteEnabled)
	{
		RECT r;
		m_DeleteButton.ShowWindow(SW_HIDE);
		m_DeleteButton.GetWindowRect(&r);
		ScreenToClient(&r);
		m_CancelButton.MoveWindow(&r);
	}

	SetUpListBox();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void COpenMapFileDlg::OnCancel() 
{
	contentFile[0] = 0;
	
	CDialog::OnCancel();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void COpenMapFileDlg::OnDblclkFileList() 
{
	OnOpenFile();
}
