// PanelSettings.cpp : implementation file
//

#include "stdafx.h"
#include <io.h>
#include "binarytool.h"
#include "PanelSettings.h"
#include "GlobalData.h"
#include "util.h"
#include "file.h"
#include "Tasks.h"

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

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// CPanelSettings creation and initialisation


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

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

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

void CPanelSettings::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPanelSettings)
	DDX_Control(pDX, IDC_CHECK_AutoDetectErrors, m_CheckAutoDetectErrors);
	DDX_Control(pDX, IDC_EDIT_MainDirectory, m_EditMainDirectory);
	DDX_Control(pDX, IDC_EDIT_BinaryDirectory, m_EditBinDirectory);
	DDX_Control(pDX, IDC_EDIT_ExeName, m_EditExeName);
	//}}AFX_DATA_MAP
}

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

BEGIN_MESSAGE_MAP(CPanelSettings, CDialog)
	//{{AFX_MSG_MAP(CPanelSettings)
	ON_BN_CLICKED(IDC_BUTTON_BrowseMainDirectory, OnBrowseMainDirectory)
	ON_BN_CLICKED(IDC_BUTTON_BrowseBinaryDirectory, OnBrowseBinaryDirectory)
	ON_BN_CLICKED(IDC_BUTTON_BrowseExeName, OnBrowseExeName)
	ON_EN_CHANGE(IDC_EDIT_MainDirectory, OnChangeMainDirectory)
	ON_EN_CHANGE(IDC_EDIT_BinaryDirectory, OnChangeBinaryDirectory)
	ON_EN_CHANGE(IDC_EDIT_ExeName, OnChangeExeName)
	ON_BN_CLICKED(IDC_RADIO_Release, OnRADIODebugOrRelease)
	ON_WM_DROPFILES()
	ON_BN_CLICKED(IDC_CHECK_AutoDetectErrors, OnCHECKAutoDetectErrors)
	ON_BN_CLICKED(IDC_RADIO_Debug, OnRADIODebugOrRelease)
	ON_BN_CLICKED(IDC_BUTTON_MakeVersion, OnBUTTONMakeVersion)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

BOOL CPanelSettings::Create( CWnd* pParentWnd )
{
	BOOL bResult = CDialog::Create( IDD, pParentWnd );

	if( bResult ) 
	{
		CWinApp *p_oApp = AfxGetApp();
		CString csSection = m_szTitle;

		// Read registry, update global data and controls
		m_CheckAutoDetectErrors.SetCheck( p_oApp -> GetProfileInt( csSection, _T("AutoDetectErrors"), 1 ) );
		g_stTheGlobalData.bAutoDetectErrors      = m_CheckAutoDetectErrors.GetCheck();

		g_stTheGlobalData.bDebugVersion = p_oApp -> GetProfileInt( csSection, _T("Version"), (int)FALSE );
		((CButton *)GetDlgItem( IDC_RADIO_Debug ))->SetCheck( g_stTheGlobalData.bDebugVersion );
		((CButton *)GetDlgItem( IDC_RADIO_Release ))->SetCheck( ! g_stTheGlobalData.bDebugVersion );

		g_stTheGlobalData.csMainDirectory = p_oApp -> GetProfileString( csSection, _T("MainDirectory"), NULL );
		g_stTheGlobalData.csBinDirectory = p_oApp -> GetProfileString( csSection, _T("BinDirectory"), NULL );

		g_stTheGlobalData.csExecutableNames.SetSize(2);
		g_stTheGlobalData.csExecutableNames[0] = p_oApp -> GetProfileString( csSection, _T("ExeRelease"), NULL );
		g_stTheGlobalData.csExecutableNames[1] = p_oApp -> GetProfileString( csSection, _T("ExeDebug"), NULL );

		if( g_stTheGlobalData.csExecutableNames[0].IsEmpty() )
			g_stTheGlobalData.csExecutableNames[0] = "MainWinR.exe";
		if( g_stTheGlobalData.csExecutableNames[1].IsEmpty() )
			g_stTheGlobalData.csExecutableNames[1] = "MainWinD.exe";


		g_stTheGlobalData.csExeName = g_stTheGlobalData.bDebugVersion ? g_stTheGlobalData.csExecutableNames[1] : g_stTheGlobalData.csExecutableNames[0];

		m_EditExeName.SetWindowText( g_stTheGlobalData.csExeName );
		m_EditMainDirectory.SetWindowText( g_stTheGlobalData.csMainDirectory );
		m_EditBinDirectory.SetWindowText( g_stTheGlobalData.csBinDirectory );

		m_bMainDirectoryIsValid = FALSE;
		m_bBinDirectoryIsValid = FALSE;
		m_bGameExeIsValid = FALSE;

		DragAcceptFiles( TRUE );
	}

	return bResult;

}


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

	p_oApp -> WriteProfileInt( csSection, _T("AutoDetectErrors"), g_stTheGlobalData.bAutoDetectErrors );

	p_oApp -> WriteProfileInt( csSection, _T("Version"), g_stTheGlobalData.bDebugVersion );
	p_oApp -> WriteProfileString( csSection, _T("MainDirectory"), g_stTheGlobalData.csMainDirectory );
	p_oApp -> WriteProfileString( csSection, _T("BinDirectory"), g_stTheGlobalData.csBinDirectory );
	p_oApp -> WriteProfileString( csSection, _T("ExeRelease"), g_stTheGlobalData.csExecutableNames[0] );
	p_oApp -> WriteProfileString( csSection, _T("ExeDebug"), g_stTheGlobalData.csExecutableNames[1] );
}


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// CPanelSettings message handlers


void CPanelSettings::OnBrowseMainDirectory() 
{
	CString csNewDir = fn_csBrowseDirectory( "Select main directory", g_stTheGlobalData.csMainDirectory );

	if( ! csNewDir.IsEmpty() )
	{
		g_stTheGlobalData.csMainDirectory = csNewDir;
		m_EditMainDirectory.SetWindowText( csNewDir );
	}
}

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

void CPanelSettings::OnBrowseBinaryDirectory() 
{
	CString csNewDir = fn_csBrowseDirectory( "Select binary directory", g_stTheGlobalData.csBinDirectory );
	
	if( ! csNewDir.IsEmpty() )
	{
		g_stTheGlobalData.csBinDirectory = csNewDir;
		m_EditBinDirectory.SetWindowText( csNewDir );
	}
}

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

void CPanelSettings::OnBrowseExeName() 
{
	CString csBaseDir;
	CFileDialog *p_oFileDlg = 
	    new CFileDialog(
	        TRUE, NULL, "MainWinR.exe", 
	        OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_LONGNAMES|OFN_EXPLORER|OFN_HIDEREADONLY,
	        "Exe files (*.exe)|*.exe|All files|*||", NULL );

	m_EditMainDirectory.GetWindowText( csBaseDir );
	if( ! csBaseDir.IsEmpty() && _access( csBaseDir, 04 ) != -1 )
	{
		p_oFileDlg->m_ofn.lpstrInitialDir = csBaseDir;
	}

	if( p_oFileDlg->DoModal() == IDOK )
	{
		g_stTheGlobalData.csExeName = p_oFileDlg->GetFileName();
		m_EditExeName.SetWindowText( p_oFileDlg->GetFileName() );
	}
}

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

void CPanelSettings::OnChangeMainDirectory() 
{
	m_bMainDirectoryIsValid = FALSE;
}

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

void CPanelSettings::OnChangeBinaryDirectory() 
{
	m_bBinDirectoryIsValid = FALSE;
}

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

void CPanelSettings::OnChangeExeName() 
{
	m_bGameExeIsValid = FALSE;
}

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

void CPanelSettings::OnRADIODebugOrRelease() 
{
	if( g_stTheGlobalData.bDebugVersion )
		m_EditExeName.GetWindowText( g_stTheGlobalData.csExecutableNames[1] );
	else
		m_EditExeName.GetWindowText( g_stTheGlobalData.csExecutableNames[0] );

	g_stTheGlobalData.bDebugVersion = ((CButton *)GetDlgItem( IDC_RADIO_Debug ))->GetCheck();
	if( g_stTheGlobalData.bDebugVersion )
		m_EditExeName.SetWindowText( g_stTheGlobalData.csExecutableNames[1] );
	else
		m_EditExeName.SetWindowText( g_stTheGlobalData.csExecutableNames[0] );
}

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

// user drag and drop file(s) on dialog...
void CPanelSettings::OnDropFiles(HDROP hDropInfo) 
{
	char szFile[256];
	
	// get first dropped file
	DragQueryFile( hDropInfo, 0, szFile, 256 );

	// If file is not a directory => set exe
	if( ! (GetFileAttributes( szFile ) & FILE_ATTRIBUTE_DIRECTORY) )
	{
		m_EditExeName.SetWindowText( szFile );
		return;
	}

	// Else


	CDialog::OnDropFiles(hDropInfo);
}

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

void CPanelSettings::OnCHECKAutoDetectErrors() 
{
	g_stTheGlobalData.bAutoDetectErrors = m_CheckAutoDetectErrors.GetCheck();
}


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

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// CPanelSettings user methods


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

// When user wants to go to another panel, we make sure all settings are valid.
BOOL CPanelSettings::m_fn_bCanChangePanel()
{

	if( ! m_bMainDirectoryIsValid )
	{
		m_EditMainDirectory.GetWindowText( g_stTheGlobalData.csMainDirectory );
		if( ! (m_bMainDirectoryIsValid = fn_bValidateMainDirectorySetting()) )
			return FALSE;
	}

	if(	! m_bBinDirectoryIsValid )
	{
		m_EditBinDirectory.GetWindowText( g_stTheGlobalData.csBinDirectory );
		if( ! (m_bBinDirectoryIsValid = fn_bValidateBinDirectorySetting()) )
			return FALSE;
	}

	if( ! m_bGameExeIsValid )
	{
		m_EditExeName.GetWindowText( g_stTheGlobalData.csExeName );
		if( ! (m_bGameExeIsValid = fn_bValidateGameExeSetting()) )
			return FALSE;
	}

	return TRUE;
}


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


// When displaying panel, we update the state of error autodetection 
// because it can be changed from another panel.
void CPanelSettings::m_fn_vOnDisplayPanel()
{
	m_CheckAutoDetectErrors.SetCheck( g_stTheGlobalData.bAutoDetectErrors );
}


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


// Here, the user wants binarytool to make the whole version by itself
// (what a lasy user !)
// We launch rm2sndparser, textures and binarisation....
void CPanelSettings::OnBUTTONMakeVersion() 
{
	// Notify main dlg for beginning of work
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyStartOfWork();

	// Do all the work
	fn_e_TASK_Version();

	// Empty the map list => it will be filled when displaying panel levels.
	g_stTheGlobalData.a_csLevelNames.SetSize( 0 );

	// End of work ! We keep the result screen displayed.
	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyEndOfWork( TRUE );
}

