// InstanceFileCreatorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "InstanceFileCreator.h"
#include "InstanceFileCreatorDlg.h"
#include <Stuff\Stuff.hpp>
#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()

/////////////////////////////////////////////////////////////////////////////
// CInstanceFileCreatorDlg dialog

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

void CInstanceFileCreatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInstanceFileCreatorDlg)
	DDX_Control(pDX, IDC_OUTPUT, m_Output);
	DDX_Text(pDX, IDC_GAMEDIR, m_GameDir);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CInstanceFileCreatorDlg, CDialog)
//{{AFX_MSG_MAP(CInstanceFileCreatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
ON_BN_CLICKED(IDC_CREATE, OnCreate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInstanceFileCreatorDlg message handlers

BOOL CInstanceFileCreatorDlg::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
	char path[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,path);
	m_GameDir=path;
	
	int pos;
	pos=m_GameDir.ReverseFind('\\');
	if(pos!=-1) m_GameDir=m_GameDir.Left(pos);
	
	pos=m_GameDir.ReverseFind('\\');
	if(pos!=-1) m_GameDir=m_GameDir.Left(pos);
	
	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CInstanceFileCreatorDlg::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 CInstanceFileCreatorDlg::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 CInstanceFileCreatorDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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


void CInstanceFileCreatorDlg::OnBrowse() 
{
	char pname[MAX_PATH];
	BROWSEINFO bi; 
	
	bi.hwndOwner = this->m_hWnd; 
	bi.pidlRoot = NULL; 
	bi.pszDisplayName = NULL; 
	bi.lpszTitle = "Choose Game Path..."; 
	bi.ulFlags = BIF_RETURNONLYFSDIRS; 
	bi.lpfn = NULL; 
	bi.lParam = (LPARAM)this; 
	
	LPITEMIDLIST pidl = SHBrowseForFolder(&bi); 
	
	if (SHGetPathFromIDList(pidl,pname))  
	{
		m_GameDir=pname;
	}
	
	if (bi.pidlRoot) 
	{ 
		LPITEMIDLIST pidl= const_cast< ITEMIDLIST* >( bi.pidlRoot ) ;
		LPMALLOC pMalloc; 
		
		if (pidl) 
		{ 
			SHGetMalloc(&pMalloc); 
			pMalloc->Free( pidl); 
			pMalloc->Release(); 
			
		} 
		
	} 
	
	UpdateData(FALSE);	
}

void CInstanceFileCreatorDlg::OnOK() 
{
	//	CDialog::OnOK();
}

void CInstanceFileCreatorDlg::OnCancel() 
{
	//	CDialog::OnCancel();
}

void CInstanceFileCreatorDlg::OnCreate() 
{
	UpdateData(TRUE);
	CString path=m_GameDir;
	path+="\\Content";
	
	SetCurrentDirectory(path);

	ParseDir("Vehicles");
	ParseDir("Buildings");
	
}

void CInstanceFileCreatorDlg::ParseDir(CString dpath)
{
	CString lpath;

	WIN32_FIND_DATA dir_data,file_data;
	HANDLE dirFind,fileFind;

	Stuff::NotationFile build_file(dpath+"\\Resource.build.addtions");
	build_file.DeleteAllPages();
	Stuff::Page *build_page=build_file.AddPage("Additions");

	dirFind=FindFirstFile(dpath+"\\*.*",&dir_data);
	
	if(dirFind != INVALID_HANDLE_VALUE)
	{
		CString dname;
		do
		{
			dname=dir_data.cFileName;
			
			if(dname.CompareNoCase(".") && 
				dname.CompareNoCase("..") && 
				dir_data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY )
			{
				lpath=dpath+"\\";
				lpath+=dname;
				fileFind=FindFirstFile(lpath+"\\*.data",&file_data);
				if(fileFind != INVALID_HANDLE_VALUE)
				{
					CString fname,basename,instname,instpath,tstr,tpath;
					
					do
					{
						fname=file_data.cFileName;
						basename=fname;
						basename=basename.Left(basename.ReverseFind('.'));
						instname=basename+".instance";
						instpath=lpath+"\\";
						instpath+=instname;
						
						if(fname.CompareNoCase("basic.data") && !gos_DoesFileExist((LPCSTR)instpath))
						{
							Stuff::NotationFile inst_file((LPCSTR)instpath);
							Stuff::Page *page=inst_file.AddPage((char *)(LPCSTR)basename);
							page->SetEntry("Model",(LPCSTR)fname);
							page->SetEntry("ExecutionState","NeverExecuteState");
							
							tstr=dname+".contents";
							tpath=lpath+"\\"+tstr;
							if(gos_DoesFileExist((LPCSTR)(tpath)))
							{
							page->SetEntry("Armature",tstr);
							}

							tstr=dname+".subsystems";
							tpath=lpath+"\\"+tstr;
							if(gos_DoesFileExist((LPCSTR)tpath))
							{
							page->SetEntry("Subsystems",tstr);
							}
							
							tstr=dname+".damage";
							tpath=lpath+"\\"+tstr;
							if(gos_DoesFileExist((LPCSTR)tpath))
							{
							page->SetEntry("DamageObjects",tstr);
							}
							inst_file.Save();
							
							CString rbpath;
							rbpath.Format("%s\\%s\\%s",dpath,dname,instname);
							build_page->AppendEntry("instance",(LPCSTR)rbpath);
							OutputMessage(instpath);
						}
						
					}
					while (FindNextFile(fileFind,&file_data));
				}
				
				
			}
			
		}
		while (FindNextFile(dirFind,&dir_data));
	}

	build_file.Save();	
}

void CInstanceFileCreatorDlg::OutputMessage(CString fname)
{
CString msg;
msg.Format("Createing %s",fname);
	m_Output.AddString(msg);
}
