// MapManagerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mw4gameed2.h"
#include "MapManagerDlg.h"
#include <Adept\Resource.hpp>
#include <Stuff\Stuff.hpp>
#include <GameOS\ToolOS.hpp>
/////////////////////////////////////////////////////////////////////////////
// CMapManagerDlg dialog

using namespace Stuff;

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


void CMapManagerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMapManagerDlg)
	DDX_Control(pDX, IDC_SKYLIST, m_SkyList);
	DDX_Control(pDX, IDC_MAPLIST, m_MapList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMapManagerDlg, CDialog)
//{{AFX_MSG_MAP(CMapManagerDlg)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_ADDSELECTED, OnAddselected)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMapManagerDlg message handlers



void CMapManagerDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	//	CDialog::OnCancel();
}

void CMapManagerDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//	CDialog::OnOK();
}

BOOL CMapManagerDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	WIN32_FIND_DATA file_data;
	HANDLE file_find;
	int itm;
	file_find=FindFirstFile("Content\\Maps\\*.*",&file_data);
	
	if(file_find != INVALID_HANDLE_VALUE)
	{
		CString dir_name;
		do
		{
			dir_name=file_data.cFileName;
			if(file_data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY && 
				dir_name!="."  && dir_name!=".." && 
				!IsMapInBuild(dir_name))
			{
				itm=m_MapList.AddString(dir_name);
				m_MapList.SetSel(itm);
			}
		}
		while (FindNextFile(file_find,&file_data));
	}
	
	file_find=FindFirstFile("Content\\Skies\\*.*",&file_data);
	
	WIN32_FIND_DATA sub_file_data;
	HANDLE sub_file_find;
	
	if(file_find != INVALID_HANDLE_VALUE)
	{
		do
		{
			if(file_data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
			{
				CString sky_dir;
				sky_dir.Format("Content\\Skies\\%s\\*.erf",file_data.cFileName);
				sub_file_find=FindFirstFile(sky_dir,&sub_file_data);
				if(sub_file_find != INVALID_HANDLE_VALUE)
				{
					CString sky_path;
					do
					{
						sky_path.Format("%s\\%s",file_data.cFileName,sub_file_data.cFileName);
							itm=m_SkyList.AddString(sky_path);
							m_SkyList.SetSel(itm);
					}
					while (FindNextFile(sub_file_find,&sub_file_data));
				}
				
			}
		}
		while (FindNextFile(file_find,&file_data));
	}
	RemoveSkiesInBuild();
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CMapManagerDlg::AddMapToBuild(CString &map_name)
{
	Verify(!IsMapInBuild(map_name));
	
	CString map_buildpath;
	CString map_shortpath;
	CString map_instpath;
	CString map_datapath;
	CString map_longpath;
	map_shortpath.Format("Maps\\%s\\%s.build",map_name,map_name);	
	map_longpath="Content\\"+map_shortpath;
	
/*
	{
		CString str;
		str.Format("Resource\\Maps\\%s.mw4",map_name);
		NotationFile mapfile(map_longpath);
		Page *page=mapfile.AddPage(str);
		str.Format("maps\\%s\\%s.instance",map_name,map_name);
		page->SetEntry("Instance",str);
		CString tpl;
		tpl.Format("Textures\\Maps\\%s\\Textures.hint",map_name);
		page->SetEntry("TexturePool",tpl);
		mapfile.Save();
	}

	{
	map_datapath.Format("Content\\Maps\\%s\\%s.data",map_name,map_name);
	NotationFile mapdatafile(map_datapath);

	Page *page=mapdatafile.SetPage("GameData");
	Check_Object(page);
	page->SetEntry("Class","MechWarrior4::MWMap");
	page->SetEntry("Cull","NeverCull");
	page->SetEntry("Grid",map_name+".erf");
	page->SetEntry("TextureData",map_name+".fgd");

	mapdatafile.Save();
	}	
	
	{
	map_instpath.Format("Content\\Maps\\%s\\%s.instance",map_name,map_name);
	NotationFile mapdatafile(map_instpath);
	Page *page=mapdatafile.SetPage(map_name);
	Check_Object(page);
	page->SetEntry("Model",map_name+".data");
	page->SetEntry("ExecutionState","AlwaysExecuteState");
	mapdatafile.Save();
	}
*/
	
	////////////////////////////////////////////////////////////
	//	Enter the data in the build file
	Verify(gos_DoesFileExist("Content\\MechWarrior4.build"));
	
	if(gos_FileReadOnly("Content\\MechWarrior4.build"))
	{
		gos_FileSetReadWrite("Content\\MechWarrior4.build");
		MessageBox("MechWarrior4.build has been made writeable and modified","File Modification",MB_OK);
	}

	
	
	NotationFile build_file("Content\\MechWarrior4.build");
	Page *build_page = build_file.FindPage("");
	Note *texture_note = build_page->FindNote("textures.build");
	
	NotationFile texture_file;
	texture_note->GetEntry(&texture_file);
	Page *texture_page = texture_file.FindPage("");
	Note *core_note = texture_page->FindNote("core.build");

	NotationFile core_file;
	core_note->GetEntry(&core_file);
	Page *core_page = core_file.FindPage("");
	Note *prop_note = core_page->FindNote("props.build");

	NotationFile prop_file;
	prop_note->GetEntry(&prop_file);
	Page *prop_page = prop_file.FindPage("");
	Note *map_note = prop_page->FindNote(map_shortpath);


	NotationFile maps_file;
	if(!map_note)
	{
	map_note=prop_page->AddNote(map_shortpath);
	Check_Object(map_note);
	}
	else
	{
	map_note->GetEntry(&maps_file);
	}

	map_note->SetEntry(&maps_file);
	prop_note->SetEntry(&prop_file);
	core_note->SetEntry(&core_file);
	texture_note->SetEntry(&texture_file);

	build_file.Save();
	
}

void CMapManagerDlg::AddSkyToBuild(CString &sky_name)
{
	Verify(!IsSkyInBuild(sky_name));
	Verify(gos_DoesFileExist("Content\\Core.build"));
	
	if(gos_FileReadOnly("Content\\Core.build"))
	{
		gos_FileSetReadWrite("Content\\Core.build");
		MessageBox("Core.build has been made writeable and modified","File Modification",MB_OK);
	}
	
	CString full_sky_name;
	full_sky_name="Skies\\";
	full_sky_name+=sky_name;
	NotationFile core_file("Content\\Core.build");
	Page *page=core_file.SetPage("Resource\\core.mw4");
	Verify(page!=NULL);
	
	page->AppendEntry("file",(LPCSTR)full_sky_name);
	
	core_file.Save();
}

bool CMapManagerDlg::IsMapInBuild(CString map_name)
{
	
	CString map_buildpath;
	CString map_shortpath;
	CString map_longpath;
	map_shortpath.Format("Maps\\%s\\%s.build",map_name,map_name);	
	map_longpath="Content\\"+map_shortpath;
	

		
	////////////////////////////////////////////////////////////
	//	Enter the data in the build file
	Verify(gos_DoesFileExist("Content\\MechWarrior4.build"));
	
	
	NotationFile build_file("Content\\MechWarrior4.build");
	Page *build_page = build_file.FindPage("");
	Note *texture_note = build_page->FindNote("textures.build");
	if(!texture_note) return false;
	
	NotationFile texture_file;
	texture_note->GetEntry(&texture_file);
	Page *texture_page = texture_file.FindPage("");
	Note *core_note = texture_page->FindNote("core.build");
	if(!core_note) return false;

	NotationFile core_file;
	core_note->GetEntry(&core_file);
	Page *core_page = core_file.FindPage("");
	Note *prop_note = core_page->FindNote("props.build");
	if(!prop_note) return false;

	NotationFile prop_file;
	prop_note->GetEntry(&prop_file);
	Page *prop_page = prop_file.FindPage("");
	Note *map_note = prop_page->FindNote(map_shortpath);
	if(map_note) return true;

	return false;
}

void CMapManagerDlg::RemoveSkiesInBuild()
{
	CString full_sky_name;
	
	NotationFile core_file("Content\\Core.build");
	Page *page=core_file.SetPage("Resource\\core.mw4");
	Verify(page!=NULL);
	int i;
	
	Page::NoteIterator *core_notes=page->MakeNoteIterator();
	Note *core_note;
	CString note_name;
	const char *note_value;
	while ((core_note = core_notes->ReadAndNext()) != NULL)
	{
		Check_Object(core_note);
		if(!strcmpi(core_note->GetName(),"file"))
		{
			core_note->GetEntry(&note_value);
			note_name=note_value;
			core_note->GetEntry(&note_value);
			note_name=note_value;
				for(i=0;i<m_SkyList.GetCount();i++)
				{
					m_SkyList.GetText(i,full_sky_name);
					full_sky_name="Skies\\"+full_sky_name;
					if(!note_name.CompareNoCase(full_sky_name))
					{
						m_SkyList.DeleteString(i);
						i--;
					}
				}
				
		}
	}
	
}


bool CMapManagerDlg::IsSkyInBuild(CString sky_name)
{
	CString full_sky_name;
	full_sky_name="Skies\\";
	full_sky_name+=sky_name;
	
	NotationFile core_file("Content\\Core.build");
	Page *page=core_file.SetPage("Resource\\core.mw4");
	Verify(page!=NULL);
	
	
	Page::NoteIterator *core_notes=page->MakeNoteIterator();
	Note *core_note;
	CString note_name;
	const char *note_value;
	while ((core_note = core_notes->ReadAndNext()) != NULL)
	{
		Check_Object(core_note);
		if(!strcmpi(core_note->GetName(),"file"))
		{
			core_note->GetEntry(&note_value);
			note_name=note_value;
			if(!note_name.CompareNoCase(full_sky_name))
				return true;
		}
	}
	return false;
}



void CMapManagerDlg::OnExit() 
{
	CDialog::OnCancel();
}

void CMapManagerDlg::OnAddselected() 
{
	int i;
	CString str;
	bool didwork=false;
	for(i=0;i<m_MapList.GetCount();i++)
	{
		if(m_MapList.GetSel(i)) 
		{
			m_MapList.GetText(i,str);
			AddMapToBuild(str);
			didwork=true;
		}
	}
	
	for(i=0;i<m_SkyList.GetCount();i++)
	{
		if(m_SkyList.GetSel(i)) 
		{
			m_SkyList.GetText(i,str);
			AddSkyToBuild(str);
			didwork=true;
		}
	}
	
	if(didwork)
		CDialog::OnOK();	
	else
		CDialog::OnCancel();	
}

