// EDACDgEr.cpp : implementation file
/////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "ErO_Op.hpp"
#include "ErOTeam.hpp"
#include "ErOReprt.hpp"
#include "ErO_Priv.hpp"

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
	#include "ErO_DgEr.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT

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

#define ERO_C_TimerID			1069
#define ERO_C_TimerDelay	200

#include "ErO_Res.h"

extern HMODULE g_hModule;

/////////////////////////////////////////////////////////////////////////////
// ERROR_Dialog_ErrorInformations dialog

BEGIN_MESSAGE_MAP(ERROR_Dialog_ErrorInformations, CDialog)
	//{{AFX_MSG_MAP(ERROR_Dialog_ErrorInformations)
	ON_BN_CLICKED(IDC_BUTTON_OPEN_SCRIPT, OnButtonOpenScript)
	ON_BN_CLICKED(IDC_BUTTON_OPEN_SOURCE, OnButtonOpenSource)
	ON_WM_TIMER()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//**********************************************************************************************
ERROR_Dialog_ErrorInformations::ERROR_Dialog_ErrorInformations()
															: CDialog(IDD_DIALOG_ERROR, NULL)
{
	//{{AFX_DATA_INIT(ERROR_Dialog_ErrorInformations)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	
	m_hOldInstance = AfxGetResourceHandle();
	AfxSetResourceHandle( g_hModule );

	m_csCurrentAction = g_csCurrentAction.IsEmpty() ? ERO_C_szUnknown : g_csCurrentAction;
	
	CStringList clStringList;
	ErO_fn_vBuildStringListForInfos(g_csAddedInfo, &clStringList);
	if ( clStringList.GetCount() > 0 )
		m_csErrorMoreInfo = clStringList.GetHead();
	else
		m_csErrorMoreInfo = ERO_C_szUnknown;

	m_csCurrentFileName = g_csCurrentFileName.IsEmpty() ? ERO_C_szUnknown : g_csCurrentFileName;
	m_csFunction = g_csCurrentFunction.IsEmpty() ? ERO_C_szUnknown : g_csCurrentFunction;
	
	m_csCurrentScriptFileName = g_csCurrentScriptFileName.IsEmpty() ? ERO_C_szUnknown : g_csCurrentScriptFileName;
	
	m_csErrorGravity = g_cslGravityMessages.GetAt(g_cslGravityMessages.FindIndex(g_eErrorGravity));

	//Gets Bitmap
	UINT nIDResource = 0;
	switch ( g_eErrorGravity )
	{
	case E_ERROR_GRAVITY_FATAL:
		nIDResource = IDB_BITMAP_ERROR_FATAL;
		break;
	case E_ERROR_GRAVITY_INSTABLE:
		nIDResource = IDB_BITMAP_ERROR_INSTABLE;
		break;
	case E_ERROR_GRAVITY_WARNING:
		nIDResource = IDB_BITMAP_ERROR_WARNING;
		break;
	case E_ERROR_GRAVITY_UNKNOWN:
		nIDResource = IDB_BITMAP_ERROR_UNKNOWN;
		break;
	}
	
	m_cbBitmap.LoadBitmap(nIDResource);

	//Sets type of Statics
	m_clMIS_Module.m_pub_fn_vSetType(ERO_STATIC_TYPE__MODULE);
	m_clMIS_Action.m_pub_fn_vSetType(ERO_STATIC_TYPE__ACTION);
	m_clMIS_Debug.m_pub_fn_vSetType(ERO_STATIC_TYPE__DEBUG);
	m_clMIS_Script.m_pub_fn_vSetType(ERO_STATIC_TYPE__SCRIPT);
	m_clMIS_Additionnal.m_pub_fn_vSetType(ERO_STATIC_TYPE__ADDITIONNAL);
}

//**************************************************************************
ERROR_Dialog_ErrorInformations::~ERROR_Dialog_ErrorInformations()
{
	AfxSetResourceHandle(m_hOldInstance);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ERROR_Dialog_ErrorInformations)
	DDX_Control(pDX, IDC_STATIC_MORE_ON_SCRIPT, m_clMIS_Script);
	DDX_Control(pDX, IDC_STATIC_MORE_ON_MODULE, m_clMIS_Module);
	DDX_Control(pDX, IDC_STATIC_MORE_ON_DEBUG, m_clMIS_Debug);
	DDX_Control(pDX, IDC_STATIC_MORE_ON_ADDITIONNAL, m_clMIS_Additionnal);
	DDX_Control(pDX, IDC_STATIC_MORE_ON_ACTION, m_clMIS_Action);
	//}}AFX_DATA_MAP
}

//**********************************************************************************************
BOOL ERROR_Dialog_ErrorInformations::OnInitDialog() 
{
	CDialog::OnInitDialog();

	//Loads bitmaps
	m_a_cbArrayOfBitmaps[7].LoadBitmap(IDB_PLUS_1);
	m_a_cbArrayOfBitmaps[6].LoadBitmap(IDB_PLUS_2);
	m_a_cbArrayOfBitmaps[5].LoadBitmap(IDB_PLUS_3);
	m_a_cbArrayOfBitmaps[4].LoadBitmap(IDB_PLUS_4);
	m_a_cbArrayOfBitmaps[3].LoadBitmap(IDB_PLUS_5);
	m_a_cbArrayOfBitmaps[2].LoadBitmap(IDB_PLUS_6);
	m_a_cbArrayOfBitmaps[1].LoadBitmap(IDB_PLUS_7);
	m_a_cbArrayOfBitmaps[0].LoadBitmap(IDB_PLUS_8);

	SetTimer(ERO_C_TimerID, ERO_C_TimerDelay, NULL);

	//Sets texts
	m_fn_vSetModuleText();
	m_fn_vSetCurrentActionText();
	m_fn_vSetCurrentFileNameText();
	m_fn_vSetFunctionText();
	m_fn_vSetCurrentScriptFileNameText();
	m_fn_vSetErrorGravityText();
	m_fn_vSetErrorMoreInfoText();

	//Sets bitmap
	CStatic *pclBitmapStatic = (CStatic *)GetDlgItem(IDC_STATIC_IMAGE);
	pclBitmapStatic->SetBitmap(HBITMAP(m_cbBitmap));

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

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::OnClose() 
{
	KillTimer(ERO_C_TimerID);
	
	CDialog::OnClose();
}

/////////////////////////////////////////////////////////////////////////////
// ERROR_Dialog_ErrorInformations message handlers

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::m_fn_vSetModuleText(CString csTextToDisplay)
{
	CString csTrueModuleName = (g_csModuleName.IsEmpty()) ? ERO_C_szUnknown : g_csModuleName;
	
	m_pri_fn_vSetTextToFitWindow(GetDlgItem(IDC_STATIC_MODULE_NAME), csTrueModuleName);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::m_fn_vSetCurrentActionText(CString csTextToDisplay)
{
	m_pri_fn_vSetTextToFitWindow(GetDlgItem(IDC_TEXT_CURRENT_ACTION),
											csTextToDisplay.IsEmpty() ? m_csCurrentAction : csTextToDisplay);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::m_fn_vSetCurrentFileNameText(CString csTextToDisplay)
{
	m_pri_fn_vSetTextToFitWindow(GetDlgItem(IDC_TEXT_FILE_NAME),
											csTextToDisplay.IsEmpty() ? m_pri_fn_csExtractFileNameFromCompletePath(m_csCurrentFileName) : csTextToDisplay);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::m_fn_vSetFunctionText(CString csTextToDisplay)
{
	m_pri_fn_vSetTextToFitWindow(GetDlgItem(IDC_TEXT_FUNCTION),
											csTextToDisplay.IsEmpty() ? m_csFunction : csTextToDisplay);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::m_fn_vSetCurrentScriptFileNameText(CString csTextToDisplay)
{
	m_pri_fn_vSetTextToFitWindow(GetDlgItem(IDC_TEXT_SCRIPT_FILE_NAME),
										csTextToDisplay.IsEmpty() ? m_pri_fn_csExtractFileNameFromCompletePath(m_csCurrentScriptFileName) : csTextToDisplay);

	if ( m_csCurrentScriptFileName.Compare(ERO_C_szUnknown) == 0 )
		GetDlgItem(IDC_BUTTON_OPEN_SCRIPT)->EnableWindow(FALSE);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::m_fn_vSetErrorGravityText(CString csTextToDisplay)
{
	m_pri_fn_vSetTextToFitWindow(GetDlgItem(IDC_TEXT_ERROR_GRAVITY),
										csTextToDisplay.IsEmpty() ? m_csErrorGravity : csTextToDisplay);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::m_fn_vSetErrorMoreInfoText(CString csTextToDisplay)
{
	m_pri_fn_vSetTextToFitWindow(GetDlgItem(IDC_TEXT_ERROR_MORE_INFO),
										csTextToDisplay.IsEmpty() ? m_csErrorMoreInfo : csTextToDisplay);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::OnButtonOpenScript() 
{
	CString csCommandLine = "Notepad.exe " + m_csCurrentScriptFileName;
	WinExec(csCommandLine, SW_SHOWNORMAL);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::OnButtonOpenSource() 
{
	CString csCommandLine = "Notepad.exe " + m_csCurrentFileName;
	WinExec(csCommandLine, SW_SHOWNORMAL);
}

//**********************************************************************************************
CString ERROR_Dialog_ErrorInformations::m_pri_fn_csExtractFileNameFromCompletePath(CString _csCompletePath)
{
	CString csStringToReturn;

	short wIndex = _csCompletePath.ReverseFind('\\');
	if ( wIndex != -1 )
		csStringToReturn = _csCompletePath.Right(_csCompletePath.GetLength() - wIndex - 1);
	else
		csStringToReturn = _csCompletePath;

	return csStringToReturn;
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::m_pri_fn_vSetTextToFitWindow(CWnd *_pclWnd, CString _csTextToDisplay)
{
	//Gets window size
	CRect crWindowRect;
	_pclWnd->GetWindowRect(crWindowRect);

	CString csDisplayedString = _csTextToDisplay;
	CString csTruelyDisplayedString;
	CClientDC dc(_pclWnd);
	dc.SelectObject(_pclWnd->GetFont());

	if ( dc.GetTextExtent(csDisplayedString).cx > crWindowRect.Width() )
	{
		csTruelyDisplayedString = csDisplayedString + "...";

		while ( dc.GetTextExtent(csTruelyDisplayedString).cx > crWindowRect.Width() )
		{
			csDisplayedString	= csDisplayedString.Left(csDisplayedString.GetLength() - 1);
			csTruelyDisplayedString = csDisplayedString + "...";
		}

		//Deletes one more character
		csDisplayedString	= csDisplayedString.Left(csDisplayedString.GetLength() - 1);
		csTruelyDisplayedString = csDisplayedString + "...";

		_pclWnd->SetWindowText(csTruelyDisplayedString);
	}
	else
		_pclWnd->SetWindowText(_csTextToDisplay);
}

//**********************************************************************************************
void ERROR_Dialog_ErrorInformations::OnTimer(UINT nIDEvent) 
{
	static char s_cCurrentBitmapIndex = 0;

	if (nIDEvent == ERO_C_TimerID)
	{
		m_clMIS_Module.SetBitmap(HBITMAP(m_a_cbArrayOfBitmaps[s_cCurrentBitmapIndex]));
		m_clMIS_Action.SetBitmap(HBITMAP(m_a_cbArrayOfBitmaps[(s_cCurrentBitmapIndex + 1) % ERO_C_NUMBER_OF_BITMAPS]));
		m_clMIS_Debug.SetBitmap(HBITMAP(m_a_cbArrayOfBitmaps[(s_cCurrentBitmapIndex + 2) % ERO_C_NUMBER_OF_BITMAPS]));
		m_clMIS_Script.SetBitmap(HBITMAP(m_a_cbArrayOfBitmaps[(s_cCurrentBitmapIndex + 3) % ERO_C_NUMBER_OF_BITMAPS]));
		m_clMIS_Additionnal.SetBitmap(HBITMAP(m_a_cbArrayOfBitmaps[(s_cCurrentBitmapIndex + 4) % ERO_C_NUMBER_OF_BITMAPS]));

		s_cCurrentBitmapIndex = (s_cCurrentBitmapIndex + 1) % ERO_C_NUMBER_OF_BITMAPS;
	}

	CDialog::OnTimer(nIDEvent);
}

