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

#include "ErO_DgIf.hpp"

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

/////////////////////////////////////////////////////////
//Array of ID
UINT g_a_uiID[ERO_STATIC_TYPE__NUMBER_OF_TYPES - 1]=
{
	IDD_DIALOG_MORE_INFO1,		
	IDD_DIALOG_MORE_INFO2,		
	IDD_DIALOG_MORE_INFO3,		
	IDD_DIALOG_MORE_INFO4,		
	IDD_DIALOG_MORE_INFO5,		
};
/////////////////////////////////////////////////////////

#define ERO_C_SPACING_WITH_BORDER							14
#define ERO_C_NUMBER_OF_BACKGROUND_BITMAPS		2

extern HMODULE g_hModule;

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

/////////////////////////////////////////////////////////////////////////////
// ErO_MoreInfoDialog dialog

BEGIN_MESSAGE_MAP(ErO_MoreInfoDialog, CDialog)
	//{{AFX_MSG_MAP(ErO_MoreInfoDialog)
	ON_WM_MOUSEMOVE()
//	ON_BN_CLICKED(IDC_BUTTON_SEND_MESSAGE, OnButtonSendMessage)
	ON_BN_CLICKED(IDC_BUTTON_MAKE_REPORT, OnButtonMakeReport)
	ON_WM_LBUTTONUP()
	ON_WM_LBUTTONDOWN()
#ifdef ERO_BITMAP_AS_BACKGROUND
	ON_WM_PAINT()
#endif //ERO_BITMAP_AS_BACKGROUND
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//***************************************************************************
ErO_MoreInfoDialog::ErO_MoreInfoDialog(CWnd* pParent,
																			 CPoint cpPoint,
																			 enum ErO_eStaticType _tdeType)
									: CDialog(g_a_uiID[_tdeType - 1], pParent)
{
	//{{AFX_DATA_INIT(ErO_MoreInfoDialog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_pri_cpPoint = cpPoint;
	m_pri_eType = _tdeType;

	m_pri_bHasCapturedMouse= FALSE;
}

//***************************************************************************
ErO_MoreInfoDialog::~ErO_MoreInfoDialog()
{
	POSITION pos = m_pri_clListOfID.GetHeadPosition();
	while ( pos != NULL )
		delete m_pri_clListOfID.GetNext(pos);
}

//***************************************************************************
void ErO_MoreInfoDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(ErO_MoreInfoDialog)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// ErO_MoreInfoDialog message handlers

//***************************************************************************
BOOL ErO_MoreInfoDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	long lMaxLength = m_pri_fn_l_ComputeLongestString();

	m_pri_BuilListOfID();
	m_pri_MoveControls(lMaxLength);

	CRect crWindowRect;
	GetWindowRect(crWindowRect);
	//Moves Rect to right position
	crWindowRect.OffsetRect(m_pri_cpPoint.x - crWindowRect.left,
													m_pri_cpPoint.y - crWindowRect.top);

	//Adjusts Rect's size
	crWindowRect.right = crWindowRect.left + lMaxLength + 2*ERO_C_SPACING_WITH_BORDER;

	if ( m_pri_eType == ERO_STATIC_TYPE__MODULE )
	{
		//Gets Team
		ErrorOperator_Team *pclTeam = g_clListOfTeams.m_pub_fn_pclGetTeamFromModuleName(g_csModuleName);
		if ( pclTeam != NULL )
		{
			CString csProgName = pclTeam->m_csMainProgrammerName;
			csProgName.MakeLower();
			HICON hIcon;
			if ( csProgName.Find("babitch") != -1 )
			{
				hIcon = ::LoadIcon(g_hModule, MAKEINTRESOURCE(IDI_ICON_PROG_YB));
				((CStatic *)GetDlgItem(IDC_IMAGE))->SetIcon(hIcon);
			}
			else if ( csProgName.Find("billault") != -1 )
			{
 				hIcon = ::LoadIcon(g_hModule, MAKEINTRESOURCE(IDI_ICON_PROG_XB));
				((CStatic *)GetDlgItem(IDC_IMAGE))->SetIcon(hIcon);
			}
			else if ( csProgName.Find("reizer") != -1 )
			{
 				hIcon = ::LoadIcon(g_hModule, MAKEINTRESOURCE(IDI_ICON_PROG_DR));
				((CStatic *)GetDlgItem(IDC_IMAGE))->SetIcon(hIcon);
			}
			else
				GetDlgItem(IDC_IMAGE)->ShowWindow(SW_HIDE);
		}
		else
		{
			GetDlgItem(IDC_IMAGE)->ShowWindow(SW_HIDE);
			//Cannot send a message
//			GetDlgItem(IDC_BUTTON_SEND_MESSAGE)->ShowWindow(SW_HIDE);
		}
	}
	else if ( m_pri_eType == ERO_STATIC_TYPE__ACTION )
	{
		//Resizes window to show all lines of ListBox
/*		CListBox *pclListBox = (CListBox *)GetDlgItem(IDC_LIST_ADD_INFO);
		short wListBoxHeight = pclListBox->GetCount() * pclListBox->GetItemHeight(0) + 2;

		CRect crLBRect;
		pclListBox->GetWindowRect(crLBRect);
		crLBRect.bottom = crLBRect.top + wListBoxHeight;
	
		crWindowRect.bottom = crLBRect.bottom + ERO_C_SPACING_WITH_BORDER;
		
		ScreenToClient(&crLBRect);

		pclListBox->MoveWindow(crLBRect);*/
	}

	//Moves Window
	MoveWindow(crWindowRect);

	SetCapture();

#ifdef ERO_BITMAP_AS_BACKGROUND
	//Selects a bitmap
/*	srand((unsigned)time( NULL ));
	int iRandomNumber = rand();
	iRandomNumber = (iRandomNumber * ERO_C_NUMBER_OF_BACKGROUND_BITMAPS) / RAND_MAX + 1;
	CString csResourceName;
	csResourceName.Format("IDB_BITMAP_BACKGROUND_%i", iRandomNumber);
	m_pri_cBackgroundBitmap.LoadBitmap(csResourceName);*/
	m_pri_cBackgroundBitmap.LoadBitmap(IDB_BITMAP_BACKGROUND_1);
#endif //ERO_BITMAP_AS_BACKGROUND

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

//***************************************************************************
void ErO_MoreInfoDialog::OnMouseMove(UINT nFlags, CPoint point) 
{
	if ( !m_pri_bHasCapturedMouse )
	{
		SetCapture();

		GetWindowRect(m_pri_crWindowRect);
		m_pri_bHasCapturedMouse = TRUE;
	}

	if ( m_pri_bHasCapturedMouse )
	{
		CPoint cpTempPoint(point);
		ClientToScreen(&cpTempPoint);
		if ( !m_pri_crWindowRect.PtInRect(cpTempPoint) )
		{
			ReleaseCapture();
			m_pri_bHasCapturedMouse = FALSE;
			EndDialog(IDOK);
		}
	}
	
	//For buttons response
	if ( m_pri_eType == ERO_STATIC_TYPE__MODULE )
	{
		WPARAM wParam = nFlags;
		LPARAM lParam = point.x << (sizeof(LPARAM) / 2) + point.y;

//		GetDlgItem(IDC_BUTTON_SEND_MESSAGE)->SendMessage(WM_MOUSEMOVE, wParam, lParam);
	}

	CDialog::OnMouseMove(nFlags, point);
}

//***************************************************************************
void ErO_MoreInfoDialog::OnLButtonUp(UINT nFlags, CPoint point) 
{
	//For buttons response
	if ( m_pri_eType == ERO_STATIC_TYPE__MODULE )
	{
		WPARAM wParam = nFlags;
		LPARAM lParam = point.x << (sizeof(LPARAM) / 2) + point.y;

//		GetDlgItem(IDC_BUTTON_SEND_MESSAGE)->SendMessage(WM_LBUTTONUP, wParam, lParam);
	}

	CDialog::OnLButtonUp(nFlags, point);
}

//***************************************************************************
void ErO_MoreInfoDialog::OnLButtonDown(UINT nFlags, CPoint point) 
{
	//For buttons response
	if ( m_pri_eType == ERO_STATIC_TYPE__MODULE )
	{
		WPARAM wParam = nFlags;
		LPARAM lParam = point.x << (sizeof(LPARAM) / 2) + point.y;

//		GetDlgItem(IDC_BUTTON_SEND_MESSAGE)->SendMessage(WM_LBUTTONDOWN, wParam, lParam);
	}
	
	CDialog::OnLButtonDown(nFlags, point);
}

//***************************************************************************
/*void ErO_MoreInfoDialog::OnButtonSendMessage() 
{
	//Gets Team
	ErrorOperator_Team *pclTeam = g_clListOfTeams.m_pub_fn_pclGetTeamFromModuleName(g_csModuleName);
	if ( pclTeam != NULL )
	{
		CString csProgName = pclTeam->m_csMainProgrammerName;

		//Builds a name with first name first letter and name
		short wIndex = csProgName.Find(" ");
		if ( wIndex != -1 )
		{
			CString csNetSendName = csProgName.Left(1) + csProgName.Right(csProgName.GetLength() - wIndex - 1);

			CString csNetSendMessage = CString('"') + "Your module '" + g_csModuleName 
																+ "' has crashed while : " + g_csCurrentAction + '"';

			CString csCommanLine = "net send " + csNetSendName + " " + csNetSendMessage;
			WinExec(csCommanLine, SW_HIDE);
		}
	}
}	 */

//***************************************************************************
void ErO_MoreInfoDialog::OnButtonMakeReport() 
{
	ErrorOperator_DialogReport ReportDial(this);
	ReportDial.DoModal();
	SetCapture();
}

//***************************************************************************
#define ERO_M_ADD_ID(ID)	\
	 pclUI = new UINT;			\
	 *pclUI = ID;						\
	 m_pri_clListOfID.AddTail(pclUI);

//***************************************************************************
void ErO_MoreInfoDialog::m_pri_BuilListOfID()
{
	UINT *pclUI;
	switch ( m_pri_eType )
	{
		case ERO_STATIC_TYPE__MODULE:
			ERO_M_ADD_ID(IDC_TEXT_TITLE);
			ERO_M_ADD_ID(IDC_MODULE_NAME);
			ERO_M_ADD_ID(IDC_MODULE_VERSION);
			ERO_M_ADD_ID(IDC_MODULE_MAIN_NAME);
			ERO_M_ADD_ID(IDC_MODULE_MAIN_PHONE);
			ERO_M_ADD_ID(IDC_MODULE_MAIN_MAIL);
			ERO_M_ADD_ID(IDC_MODULE_SECOND_NAME);
			ERO_M_ADD_ID(IDC_MODULE_SECOND_PHONE);
			ERO_M_ADD_ID(IDC_MODULE_SECOND_MAIL);
			ERO_M_ADD_ID(IDC_MODULE_RESPONSIBLE_NAME);
			ERO_M_ADD_ID(IDC_MODULE_RESPONSIBLE_PHONE);
			ERO_M_ADD_ID(IDC_MODULE_RESPONSIBLE_MAIL);
			break;

		case ERO_STATIC_TYPE__ACTION:
			ERO_M_ADD_ID(IDC_TEXT_TITLE);
			ERO_M_ADD_ID(IDC_CURRENT_ACTION);
			ERO_M_ADD_ID(IDC_LIST_ADD_INFO);
			break;

		case ERO_STATIC_TYPE__DEBUG:
			ERO_M_ADD_ID(IDC_TEXT_TITLE);
			ERO_M_ADD_ID(IDC_FILE_NAME);
			ERO_M_ADD_ID(IDC_LINE_NUMBER);
			ERO_M_ADD_ID(IDC_FILE_DATE);
			ERO_M_ADD_ID(IDC_FUNCTION);
			break;

		case ERO_STATIC_TYPE__SCRIPT:
			ERO_M_ADD_ID(IDC_TEXT_TITLE);
			ERO_M_ADD_ID(IDC_FILE_NAME);
			ERO_M_ADD_ID(IDC_LINE_NUMBER);
			break;

		case ERO_STATIC_TYPE__ADDITIONNAL:
			ERO_M_ADD_ID(IDC_TEXT_TITLE);
			ERO_M_ADD_ID(IDC_ERROR_TYPE);
			ERO_M_ADD_ID(IDC_EXPLANATION);
			break;
	};
}

//***************************************************************************
void ErO_MoreInfoDialog::m_pri_MoveControls(long _lWidth)
{
	CWnd *pclWnd;
	CRect crControlRect;
	POSITION pos = m_pri_clListOfID.GetHeadPosition();
	while ( pos != NULL )
	{
		pclWnd = GetDlgItem(*m_pri_clListOfID.GetNext(pos));

		if ( pclWnd != NULL )
		{
			pclWnd->GetWindowRect(crControlRect);
			ScreenToClient(crControlRect);
			crControlRect.right = crControlRect.left + _lWidth;

			pclWnd->MoveWindow(crControlRect);
		}
	}
}

//***************************************************************************
long ErO_MoreInfoDialog::m_pri_fn_l_ComputeLongestString()
{
	CClientDC dc(this);
	dc.SelectObject(GetFont());
	long lMaxLength = 0;
	CString csTempString;

	//Gets Team
	ErrorOperator_Team *pclTeam = g_clListOfTeams.m_pub_fn_pclGetTeamFromModuleName(g_csModuleName);

	switch ( m_pri_eType )
	{
		case ERO_STATIC_TYPE__MODULE:
			csTempString = "Module additionnal informations";
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);

			csTempString = g_csModuleName.IsEmpty() ? ERO_C_szUnknown : g_csModuleName;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_NAME)->SetWindowText(csTempString);
			
			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csModuleVersion;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_VERSION)->SetWindowText(csTempString);
			
			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csMainProgrammerName;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_MAIN_NAME)->SetWindowText(csTempString);

			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csMainProgrammerPhoneNumber;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_MAIN_PHONE)->SetWindowText(csTempString);

			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csMainProgrammerEMailAddress;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_MAIN_MAIL)->SetWindowText(csTempString);

			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csResponsibleName;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_RESPONSIBLE_NAME)->SetWindowText(csTempString);

			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csResponsiblePhoneNumber;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_RESPONSIBLE_PHONE)->SetWindowText(csTempString);

			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csResponsibleEMailAddress;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_RESPONSIBLE_MAIL)->SetWindowText(csTempString);

			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csSecondProgrammerName;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_SECOND_NAME)->SetWindowText(csTempString);

			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csSecondProgrammerPhoneNumber;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_SECOND_PHONE)->SetWindowText(csTempString);

			csTempString = (pclTeam == NULL) ? ERO_C_szUnknown : pclTeam->m_csSecondProgrammerEMailAddress;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_MODULE_SECOND_MAIL)->SetWindowText(csTempString);
			
			break;

		case ERO_STATIC_TYPE__ACTION:
		{
			csTempString = "Current action additionnal informations";
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);

			csTempString = g_csCurrentAction.IsEmpty() ? ERO_C_szUnknown : g_csCurrentAction;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_CURRENT_ACTION)->SetWindowText(csTempString);
			
			//Fills list box with strings
			CStringList clStringList;
			ErO_fn_vBuildStringListForInfos(g_csAddedInfo, &clStringList);
			POSITION pos = clStringList.GetHeadPosition();
			while (pos != NULL )
			{
 				csTempString = clStringList.GetNext(pos);
				lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
				((CListBox *)GetDlgItem(IDC_LIST_ADD_INFO))->AddString(csTempString);
			}
		}
			break;

		case ERO_STATIC_TYPE__DEBUG:
			csTempString = "Debug additionnal informations";
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);

			csTempString = g_csCurrentFileName;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_FILE_NAME)->SetWindowText(csTempString);
			
			csTempString.Format("%i", g_wCurrentFileLine);
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_LINE_NUMBER)->SetWindowText(csTempString);

			csTempString = g_csLastModif;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_FILE_DATE)->SetWindowText(csTempString);

			csTempString = g_csCurrentFunction.IsEmpty() ? ERO_C_szUnknown : g_csCurrentFunction;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_FUNCTION)->SetWindowText(csTempString);
			
			break;

		case ERO_STATIC_TYPE__SCRIPT:
			csTempString = "Script additionnal informations";
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			
			csTempString = g_csCurrentScriptFileName.IsEmpty() ? ERO_C_szUnknown : g_csCurrentScriptFileName;
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_FILE_NAME)->SetWindowText(csTempString);

			if ( !g_csCurrentScriptFileName.IsEmpty() )
				csTempString.Format("%i", g_wCurrentScriptLine);
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_LINE_NUMBER)->SetWindowText(csTempString);

			break;

		case ERO_STATIC_TYPE__ADDITIONNAL:
			csTempString = "Global informations";
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);

			csTempString = g_cslGravityMessages.GetAt(g_cslGravityMessages.FindIndex(g_eErrorGravity));
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_ERROR_TYPE)->SetWindowText(csTempString);
			
			csTempString = g_cslGravityExplanations.GetAt(g_cslGravityExplanations.FindIndex(g_eErrorGravity));
			lMaxLength = max(dc.GetTextExtent(csTempString).cx, lMaxLength);
			GetDlgItem(IDC_EXPLANATION)->SetWindowText(csTempString);

			break;
	};

	return lMaxLength + 10;
}

#ifdef ERO_BITMAP_AS_BACKGROUND
//***************************************************************************
void ErO_MoreInfoDialog::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	//Displays BackGround bitmap
	CDC pdc;
	pdc.CreateCompatibleDC(&dc);
	pdc.SelectObject(m_pri_cBackgroundBitmap);

	//Gets Bitmap Size
	short wBitmapHeight, wBitmapWidth;
	wBitmapHeight = wBitmapWidth = 160;
	CRect crClientRect;
	GetClientRect(crClientRect);
	char cXTileNumber = crClientRect.Width() / wBitmapWidth + 1;
	char cYTileNumber = crClientRect.Height() / wBitmapHeight + 1;
	char cXC, cYC;

	for (cXC = 0; cXC < cXTileNumber; cXC ++)
	{
		for (cYC = 0; cYC < cYTileNumber; cYC ++)
		{
			//Tiles Bitmap
			dc.BitBlt(cXC * wBitmapWidth,
								cYC * wBitmapHeight,
								wBitmapHeight, wBitmapWidth,
								&pdc,
								0, 0,
								SRCCOPY);
		}
	}


	// Do not call CDialog::OnPaint() for painting messages
}
#endif //ERO_BITMAP_AS_BACKGROUND
