#include "pch.h"
#include "framework.h"
#include "GenericBkMFC.h"
#include "GenericBkMFCDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define IDS_MAILADDR	_T("mailto:info@softechsoftware.it")
#define IDS_WEBADDR		_T("http://www.softechsoftware.it")
#define	IDS_ICONSADDR	_T("http://www.softechsoftware.it/iconsdll.html")

#ifdef	_WIN64
#define	ICONS_FILENAME		_T("Icons64.dll")
#else
#define	ICONS_FILENAME		_T("Icons.dll")
#endif
#define	ICONS_REQVER_MAJOR	4
#define	ICONS_REQVER_MINOR	0
#define	ICONS_REQVER_BUILD	1
#define	ICONS_REQVER_QFE	0

CGenericBkMFCDlg::CGenericBkMFCDlg(CWnd* pParent /*=nullptr*/)
	: CBkResizableDialogST(IDD_GENERICBKMFC_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGenericBkMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CBkResizableDialogST::DoDataExchange(pDX);

	DDX_Control(pDX, IDOK, m_btnExit);
	DDX_Control(pDX, IDC_BTNFULLSCREEN, m_btnFullScreen);

	DDX_Control(pDX, IDC_EMAILLINK, m_EMailLink);
	DDX_Control(pDX, IDC_HOMEPAGELINK, m_HomePageLink);
}

BEGIN_MESSAGE_MAP(CGenericBkMFCDlg, CBkResizableDialogST)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNFULLSCREEN, &CGenericBkMFCDlg::OnBnClickedBtnfullscreen)
END_MESSAGE_MAP()

BOOL CGenericBkMFCDlg::OnInitDialog()
{
	BOOL	bIconsFound = TRUE;
	DWORD	dwIconsDLLresult = 0;
	CString	sIconsReqVer;

	CBkResizableDialogST::OnInitDialog();

	// 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

	// Registry home
	m_Registry.SetKeys(HKEY_CURRENT_USER, _T("Software\\SoftechSoftware\\GenericBkMFC"));

	sIconsReqVer.Format(_T("%d.%d.%d.%d"), ICONS_REQVER_MAJOR, ICONS_REQVER_MINOR, ICONS_REQVER_BUILD, ICONS_REQVER_QFE);

	dwIconsDLLresult = LoadIconsDLL(&m_IconsDLL, sIconsReqVer);
	switch (dwIconsDLLresult)
	{
		case 0:
			break;
		case 1:
			if (NotifyIconsNotFound(sIconsReqVer) == TRUE)
			{
				PostQuitMessage(0);
				return TRUE;
			} // if
			break;
		case 2:
			if (NotifyIconsVersionMismatch(sIconsReqVer) == TRUE)
			{
				PostQuitMessage(0);
				return TRUE;
			} // if
			break;
	} // if

	ShowSizeGrip(FALSE);
	SetBitmap(m_IconsDLL.LoadBitmap(DLL_IDB_BACKGROUND14, LR_SHARED));
	ActivateEasyMoveMode(TRUE);

	// Set HyperLink for E-Mail
	m_EMailLink.SetURL(IDS_MAILADDR);
	m_EMailLink.SetUnderline(CHyperLink::ulAlways);
	m_EMailLink.DrawTransparent(FALSE);

	// Set HyperLink for Home Page
	m_HomePageLink.SetURL(IDS_WEBADDR);
	m_HomePageLink.SetUnderline(CHyperLink::ulAlways);
	m_HomePageLink.DrawTransparent(FALSE);

	// Button exit
	m_btnExit.SetThemeHelper(&m_Theme);
	m_btnExit.SetIcon(m_IconsDLL.LoadIcon(DLL_IDI_SET03_CANCEL, 32, 32));
	m_btnExit.SetTooltipText(_T("Close the application"));

	// Button full screen
	m_btnFullScreen.SetThemeHelper(&m_Theme);
	m_btnFullScreen.SetIcon(m_IconsDLL.LoadIcon(DLL_IDI_GENERAL_FULLSCREEN01, 32, 32));
	m_btnFullScreen.SetTooltipText(_T("Switch to full screen"));

	if (m_Theme.IsAppThemed() == FALSE)
	{
		m_btnExit.DrawTransparent();
		m_btnFullScreen.DrawTransparent();
	} // if

	// Preset layout
	AddAnchor(m_btnExit.m_hWnd, BOTTOM_RIGHT);
	AddAnchor(m_btnFullScreen.m_hWnd, TOP_RIGHT);
	AddAnchor(m_EMailLink.m_hWnd, BOTTOM_LEFT);
	AddAnchor(m_HomePageLink.m_hWnd, BOTTOM_LEFT);

	return TRUE;
} // End of OnInitDialog

void CGenericBkMFCDlg::OnCancel()
{
	if (m_FullScreen.IsFullScreen())
	{
		OnBnClickedBtnfullscreen();
	} // if
	else
	{
		CBkResizableDialogST::OnCancel();
	} // else
} // End of OnCancel

void CGenericBkMFCDlg::OnPostEraseBkgnd(CDC* pDC)
{
	if (m_Theme.IsAppThemed() == FALSE)
	{
		m_btnExit.SetBk(pDC);
		m_btnFullScreen.SetBk(pDC);
	} // if

	m_EMailLink.SetBk(pDC);
	m_HomePageLink.SetBk(pDC);
} // End of OnPostEraseBkgnd

//	Return values:
//	0	= DLL loaded successfully
//	1	= DLL not found
//	2	= DLL version mismatch
//
DWORD CGenericBkMFCDlg::LoadIconsDLL(CResourceServerST* pIconsDLL, LPCTSTR lpszReqVersion)
{
	TCHAR	szFilename[_MAX_PATH];
	TCHAR	szDrive[_MAX_PATH];
	TCHAR	szDir[_MAX_PATH];
	TCHAR	szPathToDLL[_MAX_PATH];
	DWORD	dwRetValue = 0;
	DWORD	dwRetValueFunc = 0;

	::GetModuleFileName(NULL, szFilename, _MAX_PATH);

	// Split path into components
	_tsplitpath_s(szFilename, szDrive, _MAX_PATH, szDir, _MAX_PATH, NULL, 0, NULL, 0);

	// Full path to DLL
	_stprintf_s(szPathToDLL, _MAX_PATH, _T("%s%s%s"), szDrive, szDir, ICONS_FILENAME);

	CFileVersionInfo fvi;
	TCHAR szVer[256] = { 0 };

	if (fvi.Open(szPathToDLL))
	{
		if (fvi.GetFileVersionMajor() < ICONS_REQVER_MAJOR)	return 2;
		if (fvi.GetFileVersionMinor() < ICONS_REQVER_MINOR)	return 2;
		if (fvi.GetFileVersionBuild() < ICONS_REQVER_BUILD)	return 2;
		if (fvi.GetFileVersionQFE() < ICONS_REQVER_QFE)		return 2;
	} // if

	if (pIconsDLL)
	{
		//AfxMessageBox(szPathToDLL);

		dwRetValue = pIconsDLL->Load(szPathToDLL);
		if (dwRetValue != RESSRVST_OK)
		{
			dwRetValueFunc = 1;
		} // if
	} // if

	return dwRetValueFunc;
} // End of LoadIconsDLL

//	Return values:
//	TRUE	= Open download link
//	FALSE	= Continue without icons
//
BOOL CGenericBkMFCDlg::NotifyIconsNotFound(LPCTSTR lpszReqVersion)
{
	CHyperLink	hlWeb;
	CString		sMessage;
	BOOL		bRetValue = FALSE;
	int			nRetValue = 0;

	sMessage = ICONS_FILENAME;
	sMessage += _T(" not found.\r\n");
	sMessage +=	_T("The application requires this DLL version ");
	sMessage +=	lpszReqVersion;
	sMessage +=	_T("\r\n\r\n");
	sMessage +=	_T("This DLL is required by the application but is not ");
	sMessage +=	_T("essential to properly work.\r\n");
	sMessage +=	_T("Do you want to download the DLL ?\r\n\r\n");
	sMessage +=	_T("Press 'Yes' to go to download page on SoftechSoftware site.\r\n");
	sMessage +=	_T("Press 'No' to continue without icons and bitmaps.\r\n");

	nRetValue = MessageBox(	sMessage,
							_T("SoftechSoftware"),
							MB_ICONQUESTION | MB_YESNO);
	if (nRetValue == IDYES)
	{
		hlWeb.GotoURL(IDS_ICONSADDR, SW_SHOW);
		bRetValue = TRUE;
	} // if

	return bRetValue;
} // End of NotifyIconsNotFound

//	Return values:
//	TRUE	= Open download link
//	FALSE	= Continue with found DLL
//
BOOL CGenericBkMFCDlg::NotifyIconsVersionMismatch(LPCTSTR lpszReqVersion)
{
	CHyperLink	hlWeb;
	CString		sMessage;
	BOOL		bRetValue = FALSE;
	int			nRetValue = 0;

	sMessage = _T("This application requires ");
	sMessage += ICONS_FILENAME;
	sMessage += _T(" version ");
	sMessage +=	lpszReqVersion;
	sMessage +=	_T("\r\n\r\n");
	sMessage +=	_T("A different version has been found installed with this application.\r\n");
	sMessage +=	_T("Do you want to download the correct version ?\r\n\r\n");
	sMessage +=	_T("Press 'Yes' to go to download page on SoftechSoftware site.\r\n");
	sMessage +=	_T("Press 'No' to continue with the installed version.\r\n");

	nRetValue = MessageBox(	sMessage,
							_T("SoftechSoftware"),
							MB_ICONQUESTION | MB_YESNO);
	if (nRetValue == IDYES)
	{
		hlWeb.GotoURL(IDS_ICONSADDR, SW_SHOW);
		bRetValue = TRUE;
	} // if

	return bRetValue;
} // End of NotifyIconsVersionMismatch

// 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 CGenericBkMFCDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
	{
		CBkResizableDialogST::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGenericBkMFCDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CGenericBkMFCDlg::OnBnClickedBtnfullscreen()
{
	m_FullScreen.ToggleFullScreen(m_hWnd, FALSE);
	ActivateEasyMoveMode(!m_FullScreen.IsFullScreen());
} // End of OnBnClickedBtnfullscreen

