// PanelBeforeBin.cpp : implementation file
//

#include "stdafx.h"
#include "BinaryTool.h"
#include "PanelBeforeBin.h"

#include "GlobalData.h"
#include "util.h"
#include "file.h"
#include "Process.h"
#include "Threads.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// CPanelBeforeBin creation and initialisation


CPanelBeforeBin::CPanelBeforeBin(CWnd* pParent /*=NULL*/ )
	: CPanel(IDD, pParent)
{
	//{{AFX_DATA_INIT(CPanelBeforeBin)
	//}}AFX_DATA_INIT

	_tcscpy( m_szTitle, _T("Preparation") );
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

void CPanelBeforeBin::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPanelBeforeBin)
	DDX_Control(pDX, IDC_EDIT_TempTextureDirectory, m_oEditTempDirForTextures);
	//}}AFX_DATA_MAP
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

BEGIN_MESSAGE_MAP(CPanelBeforeBin, CDialog)
	//{{AFX_MSG_MAP(CPanelBeforeBin)
	ON_BN_CLICKED(IDC_BUTTON_MakeSoundBanks, OnMakeSoundBanks)
	ON_BN_CLICKED(IDC_BUTTON_CopySound, OnCopySound)
	ON_BN_CLICKED(IDC_BUTTON_MakeGF, OnMakeGF)
	ON_BN_CLICKED(IDC_BUTTON_BrowseTempTextureDirectory, OnBrowseTempTextureDirectory)
	ON_BN_CLICKED(IDC_BUTTON_MakeBigFile, OnMakeBigFile)
	ON_BN_CLICKED(IDC_BUTTON_MakeA3i, OnMakeA3i)
	ON_EN_KILLFOCUS(IDC_EDIT_TempTextureDirectory, OnKillfocusEDITTempTextureDirectory)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

BOOL CPanelBeforeBin::Create( CWnd* pParentWnd )
{
	return CDialog::Create( IDD, pParentWnd );
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

BOOL CPanelBeforeBin::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// Read registry
	CWinApp *p_oApp = AfxGetApp();
	CString csSection = m_szTitle;

	m_oEditTempDirForTextures.SetWindowText( p_oApp -> GetProfileString( csSection, _T("TmpDir"), NULL ) );
	m_fn_vValidateTempTextureDirectory();

	BOOL bOptimizedAnims = p_oApp -> GetProfileInt( csSection, _T("OptimizedAnims"), 1 );
	((CButton *)GetDlgItem( IDC_RADIO_OptimizedAnims ))->SetCheck( bOptimizedAnims );
	((CButton *)GetDlgItem( IDC_RADIO_NonOptimizedAnims ))->SetCheck( ! bOptimizedAnims );

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// CPanelBeforeBin message handlers


void CPanelBeforeBin::OnMakeSoundBanks() 
{
	char     szSndParserExeName[512];
	char     szCommandLine[1024];

	// Notify main dlg for beginning of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyStartOfWork();

	fn_vOutputLine( "Making sound banks...\r\n" );
	sprintf( szSndParserExeName, "%s\\rm2SndParser.exe", (const char *)g_stTheGlobalData.csStartupDirectory );
	fn_vMakeSndParserCommandLine( szCommandLine );
	switch( fn_eSpawnAndWaitForProcessWithInputEvents( szSndParserExeName, szCommandLine ) )
	{
		case STATUS_C_Error:
			fn_vOutputLine( "\tBreak on error.\r\n" );
			break;
		default:
			break;
	}
	fn_vOutputLine( "\r\n" );

	// Notify main dlg for end of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyEndOfWork( FALSE );
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


void CPanelBeforeBin::OnCopySound() 
{
//	MSG stMsg;

	// Notify main dlg for beginning of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyStartOfWork();

	fn_eSpawnThreadAndWaitWithInputEvents( fn_uiThreadCopySoundDirectory, NULL );

	// Notify main dlg for end of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyEndOfWork( FALSE );
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


void CPanelBeforeBin::OnBrowseTempTextureDirectory() 
{
	CString csTextureDir;
	m_oEditTempDirForTextures.GetWindowText( csTextureDir );

	CString csNewDir = fn_csBrowseDirectory( "Select temp directory", csTextureDir );

	if( ! csNewDir.IsEmpty() )
	{
		m_oEditTempDirForTextures.SetWindowText( csNewDir );
		m_fn_vValidateTempTextureDirectory();
	}
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


void CPanelBeforeBin::OnMakeGF() 
{
	BOOL                  bKeepDisplayResults;

	// Notify main dlg for beginning of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyStartOfWork();

	if( fn_eSpawnThreadAndWaitWithInputEvents( fn_uiThreadMakeGFTextures, NULL ) == STATUS_C_Error )
		bKeepDisplayResults = TRUE;

	// Notify main dlg for end of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyEndOfWork( bKeepDisplayResults );
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


void CPanelBeforeBin::OnMakeBigFile() 
{
	BOOL                  bKeepDisplayResults = FALSE;
	CString               csTextureDir;

	// Notify main dlg for beginning of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyStartOfWork();

	m_oEditTempDirForTextures.GetWindowText( csTextureDir );

	if( fn_eSpawnThreadAndWaitWithInputEvents( fn_uiThreadMakeBigFileTextures, (void *)(LPCTSTR)csTextureDir ) == STATUS_C_Error )
		bKeepDisplayResults = TRUE;

	// Notify main dlg for end of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyEndOfWork( bKeepDisplayResults );
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


void CPanelBeforeBin::OnMakeA3i() 
{
	BOOL                  bKeepDisplayResults = FALSE;
	CString               csTextureDir;

	// Notify main dlg for beginning of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyStartOfWork();

	BOOL bOptimizedAnims = ((CButton *)GetDlgItem( IDC_RADIO_OptimizedAnims ))->GetCheck();
	if( fn_eSpawnThreadAndWaitWithInputEvents( fn_uiThreadAnims, (LPVOID)&bOptimizedAnims ) == STATUS_C_Error )
		bKeepDisplayResults = TRUE;

	// Notify main dlg for end of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyEndOfWork( bKeepDisplayResults );
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


void CPanelBeforeBin::OnKillfocusEDITTempTextureDirectory() 
{
	m_fn_vValidateTempTextureDirectory();
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// User methods

void CPanelBeforeBin::m_fn_vUpdateRegistry()
{
	CWinApp *p_oApp = AfxGetApp();
	CString csSection = m_szTitle;

	p_oApp -> WriteProfileString( csSection, _T("TmpDir"), g_stTheGlobalData.csTempTextureDirectory );

	BOOL bOptimizedAnims = ((CButton *)GetDlgItem( IDC_RADIO_OptimizedAnims ))->GetCheck();
	p_oApp -> WriteProfileInt( csSection, _T("OptimizedAnims"), bOptimizedAnims );
	
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


void CPanelBeforeBin::m_fn_vValidateTempTextureDirectory()
{
	CString csTextureDir;

	m_oEditTempDirForTextures.GetWindowText( csTextureDir );

	if( csTextureDir.Compare( g_stTheGlobalData.csTempTextureDirectory ) == 0 )
		return;

	// If temp path is not found get the window temp path
	if( csTextureDir.IsEmpty() )
	{
		DWORD dwRes = GetTempPath( _MAX_PATH, csTextureDir.GetBuffer( _MAX_PATH ) );

		csTextureDir.ReleaseBuffer();
		if( dwRes== 0 )
		{
			csTextureDir.Empty();
		}
	}

	// Remove trailing '\'
	int iLength = csTextureDir.GetLength() - 1;
	if( iLength > 0 && csTextureDir[iLength] == '\\' )
		csTextureDir.SetAt( iLength, 0 );

	g_stTheGlobalData.csTempTextureDirectory = csTextureDir;
	m_oEditTempDirForTextures.SetWindowText( csTextureDir );
}

