// BinaryToolDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BinaryTool.h"
#include "BinaryToolDlg.h"
#include "GlobalData.h"
#include "PanelBeforeBin.h"
#include "PanelResults.h"
#include "PanelSettings.h"
#include "PanelLevels.h"
#include "PanelAfter.h"
#include "scripts.h"

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

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// 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()

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// CBinaryToolDlg Creation and initialisation

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

	memset( m_a_pPanels, 0, sizeof(m_a_pPanels) );
}

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

void CBinaryToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBinaryToolDlg)
	DDX_Control(pDX, IDC_TAB_Panel, m_TabControl);
	//}}AFX_DATA_MAP
}

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

BEGIN_MESSAGE_MAP(CBinaryToolDlg, CDialog)
	//{{AFX_MSG_MAP(CBinaryToolDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_Panel, OnSelchangeTabPanel)
	ON_BN_CLICKED(ID_BOUTON_Quit, OnQuit)
	ON_WM_CLOSE()
	ON_BN_CLICKED(ID_BUTTON_Stop, OnStopWorkInProgress)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

BOOL CBinaryToolDlg::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
	

	m_csDlgTitle.LoadString( IDS_AppTitle );
	SetWindowText( m_csDlgTitle );
	// Init part of GlobalData struct
	g_stTheGlobalData.p_oMainDlg = this;

	// Create all panels for tabcontrol :

	// Get rectangle for tabcontrol
	RECT stPanelRectangle;
	TC_ITEM TabCtrlItem;

	m_TabControl.GetWindowRect( &stPanelRectangle );

	stPanelRectangle.right -= 10;
	stPanelRectangle.bottom -= 30;

	m_iNumberOfPanels = 0;

	m_a_pPanels[m_iNumberOfPanels++] = new CPanelSettings( this );
	m_a_pPanels[m_iNumberOfPanels++] = new CPanelBeforeBin( this );
	m_a_pPanels[m_iNumberOfPanels++] = new CPanelLevels( this );
	m_a_pPanels[m_iNumberOfPanels++] = new CPanelAfter( this );
	m_a_pPanels[m_iNumberOfPanels++] = new CPanelResults( this );   // This panel must be the last one

	m_iResultPanel = m_iNumberOfPanels - 1;
	m_iPanelBeforeStartOfWork = 0;
	m_iCurrentPanel = 0;

	for( int i=0; i<m_iNumberOfPanels; i++ )
	{
		// Create the tab panel
		m_a_pPanels[i]->Create( this );
		m_a_pPanels[i]->MoveWindow( &stPanelRectangle, FALSE );

		// Insert it in the tab control
		TabCtrlItem.mask = TCIF_TEXT;
		TabCtrlItem.pszText = m_a_pPanels[i]->m_szTitle;
		m_TabControl.InsertItem( i, &TabCtrlItem );
	}

	m_a_pPanels[m_iCurrentPanel]->ShowWindow( SW_SHOW );

	DragAcceptFiles( TRUE );

	if( stristr( GetCommandLine(), "-MakeVersion" ) )
	{
		m_a_pPanels[0]->PostMessage( 
			WM_COMMAND, 
			MAKEWPARAM(IDC_BUTTON_MakeVersion,BN_CLICKED), 
			(LPARAM)m_a_pPanels[0]->GetDlgItem(IDC_BUTTON_MakeVersion)->m_hWnd );
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

CBinaryToolDlg::~CBinaryToolDlg()
{
}

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

BOOL CBinaryToolDlg::DestroyWindow() 
{
	for( int i=0; i<m_iNumberOfPanels; i++ )
	{
		if( m_a_pPanels[i] ) 
		{
			m_a_pPanels[i]->m_fn_vUpdateRegistry();
			m_a_pPanels[i]->DestroyWindow();
			delete m_a_pPanels[i];
		}
	}
	
	return CDialog::DestroyWindow();
}


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// CBinaryToolDlg message handlers

void CBinaryToolDlg::OnOK()
{
}

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

void CBinaryToolDlg::OnQuit() 
{

	CDialog::OnOK();
}

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

void CBinaryToolDlg::OnClose() 
{
	CDialog::OnClose();
}

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

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

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

void CBinaryToolDlg::OnSelchangeTabPanel(NMHDR* pNMHDR, LRESULT* pResult) 
{
	if( m_a_pPanels[m_iCurrentPanel]->m_fn_bCanChangePanel() )
	{
		int iNextPanel = m_TabControl.GetCurSel();
		m_a_pPanels[iNextPanel]->m_fn_vOnDisplayPanel();
		m_fn_vSetCurrentPanel( iNextPanel );
	}
	else
		m_TabControl.SetCurSel( m_iCurrentPanel );

	*pResult = 0;
}

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


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// CBinaryToolDlg user methods

// When a new job begin, display the result panel
// and disable all controls
void CBinaryToolDlg::m_fn_vNotifyStartOfWork()
{
	m_iPanelBeforeStartOfWork = m_iCurrentPanel;
	m_fn_vSetCurrentPanel( m_iResultPanel );

	GetDlgItem( IDC_TAB_Panel ) -> EnableWindow( FALSE );
	GetDlgItem( ID_BOUTON_Quit ) -> EnableWindow( FALSE );

	ResetEvent( g_stTheGlobalData.hEventStop );
	g_stTheGlobalData.bStopWork = FALSE;

	// Show the button stop.
	GetDlgItem( ID_BUTTON_Stop ) -> EnableWindow( TRUE );
	GetDlgItem( ID_BUTTON_Stop ) -> ShowWindow( SW_SHOW );

	// Change title
	TCHAR szTitle[256];
	_stprintf( szTitle, "%s - Working...", (LPCTSTR)m_csDlgTitle );
	SetWindowText( szTitle );
}

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

// When a job is finished, restore control and panel
// (except if caller wish to keep the result panel on, in case of error for example)
void CBinaryToolDlg::m_fn_vNotifyEndOfWork( BOOL _bKeepDisplayResultPanel )
{
	if( ! _bKeepDisplayResultPanel )
	{
		m_fn_vSetCurrentPanel( m_iPanelBeforeStartOfWork );
	}

	ResetEvent( g_stTheGlobalData.hEventStop );
	g_stTheGlobalData.bStopWork = FALSE;

	GetDlgItem( IDC_TAB_Panel ) -> EnableWindow( TRUE );
	GetDlgItem( ID_BOUTON_Quit ) -> EnableWindow( TRUE );

	// Hide the button stop.
	GetDlgItem( ID_BUTTON_Stop ) -> ShowWindow( SW_HIDE );

	SetWindowText( m_csDlgTitle );
}

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

// Button stop is pressed : user wants to stop a job in progress.
void CBinaryToolDlg::OnStopWorkInProgress() 
{
	// Tell to other threads to stop.
	SetEvent( g_stTheGlobalData.hEventStop );
	g_stTheGlobalData.bStopWork = TRUE;

	// Gray the button stop
	GetDlgItem( ID_BUTTON_Stop ) -> EnableWindow( FALSE );

	// Change title
	TCHAR szTitle[256];
	_stprintf( szTitle, "%s - Stopping...", (LPCTSTR)m_csDlgTitle );
	SetWindowText( szTitle );
}

// The user  doesn't want to stop work in progress anymore
void CBinaryToolDlg::m_fn_vCancelStopWorkInProgress()
{
	ResetEvent( g_stTheGlobalData.hEventStop );
	g_stTheGlobalData.bStopWork = FALSE;

	// Enable the button stop
	GetDlgItem( ID_BUTTON_Stop ) -> EnableWindow( TRUE );

	// Change title
	TCHAR szTitle[256];
	_stprintf( szTitle, "%s - Working...", (LPCTSTR)m_csDlgTitle );
	SetWindowText( szTitle );
}

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

void CBinaryToolDlg::m_fn_vSetCurrentPanel( int _iNewPanel )
{
	if( _iNewPanel != m_iCurrentPanel )
	{
		m_a_pPanels[m_iCurrentPanel]->ShowWindow( SW_HIDE );
		m_iCurrentPanel = _iNewPanel;
		m_a_pPanels[m_iCurrentPanel]->ShowWindow( SW_SHOW );

		m_TabControl.SetCurSel( _iNewPanel );
	}
}

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


BOOL CBinaryToolDlg::m_fn_bRefreshLevelList()
{
	// Read game.dsc
	BOOL bResult = fn_bReadGameDsc();

	// Tell the panels that level list was refreshed
	for( int i=0; i<m_iNumberOfPanels; i++ )
	{
		// Create the tab panel
		m_a_pPanels[i]->m_fn_vLevelListHasChanged( ! bResult );
	}

	return bResult;
}
