// MissionCopyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MissionCopy.h"
#include "MissionCopyDlg.h"

#include <GameOS\ToolOS.hpp>

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
	
	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA
	
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	
	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMissionCopyDlg dialog

CMissionCopyDlg::CMissionCopyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMissionCopyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMissionCopyDlg)
	m_FName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMissionCopyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMissionCopyDlg)
	DDX_Control(pDX, IDC_MISSIONLIST, m_MisList);
	DDX_Text(pDX, IDC_FNAME, m_FName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMissionCopyDlg, CDialog)
//{{AFX_MSG_MAP(CMissionCopyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_DOCOPY, OnDocopy)
ON_LBN_SELCHANGE(IDC_MISSIONLIST, OnSelchangeMissionlist)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMissionCopyDlg message handlers

BOOL CMissionCopyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	// Add "About..." menu item to system menu.
	
	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);
	
	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}
	
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	WIN32_FIND_DATA file_data;
	HANDLE mission_search = FindFirstFile("Content\\Missions\\*.*" , &file_data);
	
	if(mission_search != INVALID_HANDLE_VALUE)
	{
		do
		if(file_data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
		{
			CString mis_name(file_data.cFileName);
			if(mis_name.CompareNoCase(".") && mis_name.CompareNoCase(".."))
			{
				
				m_MisList.AddString(file_data.cFileName);
			}
		}
		while (FindNextFile(mission_search,&file_data));
	}	
	
	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMissionCopyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMissionCopyDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting
		
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
		
		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMissionCopyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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

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

void CMissionCopyDlg::OnExit() 
{
	CDialog::OnOK();
}

void CMissionCopyDlg::OnDocopy() 
{
	UpdateData(TRUE);
	CString new_mis_name=m_FName;
	CString new_mis_path="Content\\Missions\\"+new_mis_name;
	int pos=m_MisList.GetCurSel();
	CString old_mis_name;
	if(pos<=0) return;
	m_MisList.GetText(pos,old_mis_name);
	CString old_mis_path="Content\\Missions\\"+old_mis_name;

	if(gos_DoesFileExist(new_mis_path))
	{
		MessageBox("This Mission Already exists","Error",MB_OK);
	}
	else
	{
	gos_CreateDirectory(new_mis_path);
	gos_CreateDirectory(new_mis_path+"\\Scripts");
	gos_CreateDirectory(new_mis_path+"\\Paths");
	gos_CreateDirectory(new_mis_path+"\\DropZones");
	ReplaceInNoteFile(old_mis_path+"\\"+old_mis_name+".data",old_mis_name,new_mis_name);
	ReplaceInNoteFile(old_mis_path+"\\"+old_mis_name+"graph.data",old_mis_name,new_mis_name);
	ReplaceInNoteFile(old_mis_path+"\\"+old_mis_name+".instance",old_mis_name,new_mis_name);
	ReplaceInNoteFile(old_mis_path+"\\"+old_mis_name+".build",old_mis_name,new_mis_name);
	ReplaceInNoteFile(old_mis_path+"\\"+old_mis_name+".lights",old_mis_name,new_mis_name);
	ReplaceInNoteFile(old_mis_path+"\\"+old_mis_name+"night.lights",old_mis_name,new_mis_name);
	ReplaceInNoteFile(old_mis_path+"\\"+old_mis_name+".ini",old_mis_name,new_mis_name);
	CopyAndRenameDir(old_mis_path,old_mis_name,new_mis_name);
	RenameInBuildFile(old_mis_name,new_mis_name);
	}
	
	
	
}

void CMissionCopyDlg::OnSelchangeMissionlist() 
{
	// TODO: Add your control notification handler code here
	int pos=m_MisList.GetCurSel();
	if(pos>=0) 
	{
		m_MisList.GetText(pos,m_FName);
		m_FName="CopyOf"+m_FName;
		UpdateData(FALSE);
	}
}

void CMissionCopyDlg::ReplaceInNoteFile(CString nfile,CString old_str,CString new_str)
{
	nfile.MakeLower();
	old_str.MakeLower();
	new_str.MakeLower();
	int occ;
	bool mod=false;
	
	if(!gos_DoesFileExist(nfile)) return;
	{
		NotationFile note_file(nfile);
		
		NotationFile::PageIterator *pages=note_file.MakePageIterator();
		Page *page;
		const char *ccs;
		while(page=pages->ReadAndNext())
		{
			CString page_name(page->GetName());
			page_name.MakeLower();
			occ=page_name.Replace(old_str,new_str);
			if(occ>0) page->SetName(page_name);
			
			Page::NoteIterator *notes=page->MakeNoteIterator();
			Note *note;	
			while(note=notes->ReadAndNext())
			{
				note->GetEntry(&ccs);
				CString note_val(ccs);
				note_val.MakeLower();
				occ=note_val.Replace(old_str,new_str);
				if(occ>0) note->SetEntry(note_val);
			}
		}
		
		nfile.Replace(old_str,new_str);
		note_file.SaveAs(nfile);
	}
	
}

void CMissionCopyDlg::CopyAndRenameDir(CString old_dir_name,CString old_str,CString new_str)
{
old_dir_name.MakeLower();
old_str.MakeLower();
new_str.MakeLower();
CString new_dir_name=old_dir_name;
new_dir_name.Replace(old_str,new_str);

	
	WIN32_FIND_DATA file_data;
	HANDLE mission_search = FindFirstFile(old_dir_name+"\\*.*" , &file_data);
	
	if(mission_search != INVALID_HANDLE_VALUE)
	{
		do
		{
		CString str(file_data.cFileName);
		CString old_file_name=old_dir_name+"\\"+file_data.cFileName;
		if(file_data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
		{
			if(str.CompareNoCase(".") && str.CompareNoCase(".."))
			{
				CopyAndRenameDir(old_file_name,old_str,new_str);
			}
		}
	else
	{
	CString new_file_name=old_file_name;
	new_file_name.MakeLower();
	new_file_name.Replace(old_str,new_str);
	if(!gos_DoesFileExist(new_file_name))
		{
		CopyFile(old_file_name,new_file_name,true);
		}
	}
		
		}
		while (FindNextFile(mission_search,&file_data));
	}	

}

void CMissionCopyDlg::RenameInBuildFile(CString old_str,CString new_str)
{
	old_str.MakeLower();
	new_str.MakeLower();
	NotationFile build_file("Content\\MechWarrior4.build");
	Page *build_page = build_file.FindPage("");
	Check_Object(build_page);
	Note *texture_note = build_page->FindNote("textures.build");
	Check_Object(texture_note);
	
	NotationFile texture_file;
	texture_note->GetEntry(&texture_file);
	Page *texture_page = texture_file.FindPage("");
	Check_Object(texture_page);
	Note *core_note = texture_page->FindNote("core.build");
	Check_Object(core_note);

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

	NotationFile prop_file;
	prop_note->GetEntry(&prop_file);
	Page *prop_page = prop_file.FindPage("");
	Check_Object(prop_page);
	Page::NoteIterator *map_notes=prop_page->MakeNoteIterator();
	
	Note *map_note;
	while(map_note=map_notes->ReadAndNext())
	{
	NotationFile mission_file;
	map_note->GetEntry(&mission_file);
	Page *mission_page=mission_file.GetPage("");
	if(	mission_page)
	{
	Page::NoteIterator *mission_notes=mission_page->MakeNoteIterator();
	Note *mission_note;
	while(mission_note=mission_notes->ReadAndNext())
		{
		CString mis_name(mission_note->GetName());
		mis_name.MakeLower();
		if(mis_name.Replace(old_str,new_str)>0)
			{	
			mission_note->SetName(mis_name);
			}

		}
	}
	map_note->SetEntry(&mission_file);

	}


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


	build_file.Save();

}

