// AttributeChangerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AttributeChanger.h"
#include "AttributeChangerDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CAttributeChangerDlg dialog

CAttributeChangerDlg::CAttributeChangerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAttributeChangerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAttributeChangerDlg)
	m_Attrib = _T("");
	m_Data = _T("");
	m_NewData = _T("");
	m_Path = _T("");
	m_WildCard = _T("");
	m_Recurse = TRUE;
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAttributeChangerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAttributeChangerDlg)
	DDX_Text(pDX, IDC_ATTRIB, m_Attrib);
	DDX_Text(pDX, IDC_DATA, m_Data);
	DDX_Text(pDX, IDC_NEWDATA, m_NewData);
	DDX_Text(pDX, IDC_PATH, m_Path);
	DDX_Text(pDX, IDC_WILDCARD, m_WildCard);
	DDX_Check(pDX, IDC_RECURSE, m_Recurse);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAttributeChangerDlg, CDialog)
	//{{AFX_MSG_MAP(CAttributeChangerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHANGE, OnChange)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAttributeChangerDlg message handlers

BOOL CAttributeChangerDlg::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);
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	char buf[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,buf);
	m_Path=buf;
	m_WildCard="*.data";
/*
	m_Path="C:\\Code\\mw4\\Content\\Vehicles";
	m_WildCard="*Destroyed.data";
	m_Attrib="Class";
	m_Data="Adept::Entity";
	m_NewData="MechWarrior4::Cultural";
*/
	UpdateData(FALSE);	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAttributeChangerDlg::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 CAttributeChangerDlg::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();
	}
}

HCURSOR CAttributeChangerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

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

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

void CAttributeChangerDlg::OnChange() 
{
UpdateData(TRUE);
if(!m_Recurse)  ChangeInDir(m_Path); 
	else
ParseDir(m_Path);
	
}

void CAttributeChangerDlg::ParseDir(CString dpath)
{
ChangeInDir(dpath);

	WIN32_FIND_DATA dir_data;
	HANDLE dirFind;


	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)
			{ 
			ParseDir(dpath+"\\"+dir_data.cFileName);
			}
		}
		while (FindNextFile(dirFind,&dir_data));
	}

}

void CAttributeChangerDlg::ChangeInDir(CString dpath)
{

	WIN32_FIND_DATA file_data;
	HANDLE fileFind;


	fileFind=FindFirstFile(dpath+"\\"+m_WildCard,&file_data);
	
	if(fileFind != INVALID_HANDLE_VALUE)
	{
		CString file_name;
		do
		{
			file_name=dpath+"\\"+file_data.cFileName;
			Stuff::NotationFile note_file(file_name);
			Stuff::NotationFile::PageIterator *pages=note_file.MakePageIterator();

			pages->First();

			Stuff::Page *page;
			while((page=(Stuff::Page *)pages->ReadAndNext())!=NULL)
			{
			if(m_Data!="")
				{
								Stuff::Note *note=page->FindNote(m_Attrib);
				if(note)
					{
					const char *buf;
					note->GetEntry(&buf);
					if(!m_Data.CompareNoCase(buf))
						{
						note->SetEntry(m_NewData);
						}
					}

				}
			else
				{
				}
			}

			note_file.Save();
		}
		while (FindNextFile(fileFind,&file_data));
	}
}


void CAttributeChangerDlg::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_Path=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 CAttributeChangerDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
	
}
