
#include "stdafx.h"
#include "MultiZebrasDemo.h"
#include "MultiZebrasDemoDlg.h"
#include "FileOpenCombo.h"
#include "afxdialogex.h"

#include "ZebraInfoDlg.h"

TCHAR *printZebraType(int nZebraType);
TCHAR *printfZebraStatus(int nZebraStatus);

#ifndef ZEBRAZ4U3DLLWIN32_EXPORTS
#include "ZebraZ4U3DllWin32.h"
#else
__PZEBRA_BOARD_INFO__ getZebraBoardInfo(__HZHANDLE__ hZhandle, TCHAR **pUsbPortName);
int ZebraCardNumber(void);
#endif


//HWND	g_hwndZebrs = NULL;  // Window handle of dialog box 
BOOL CALLBACK ZebraInfoDlg(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	HWND hItemWnd;
	UINT nSel;
	__HZHANDLE__ hZhandle;
	static CMultiZebrasDemoDlg *pMultiZebra = NULL;

	switch (message)
	{
	case WM_INITDIALOG:
		pMultiZebra = (CMultiZebrasDemoDlg *)lParam;

		if (pMultiZebra->m_ZebraInfoDlgOpenMode == __zInfoDlgOpen_View__)
		{
			hItemWnd = GetDlgItem(hwndDlg, IDOK);
			EnableWindow(hItemWnd, FALSE);
		}
		// turn off all Zebra items
		hItemWnd = GetDlgItem(hwndDlg, IDC_RDO_Z1_SEL);
		EnableWindow(hItemWnd, FALSE);
		SetDlgItemText(hwndDlg, IDC_STC_Z1_INFO, L"");

		hItemWnd = GetDlgItem(hwndDlg, IDC_RDO_Z2_SEL);
		EnableWindow(hItemWnd, FALSE);
		SetDlgItemText(hwndDlg, IDC_STC_Z2_INFO, L"");

		hItemWnd = GetDlgItem(hwndDlg, IDC_RDO_Z3_SEL);
		EnableWindow(hItemWnd, FALSE);
		SetDlgItemText(hwndDlg, IDC_STC_Z3_INFO, L"");

		hItemWnd = GetDlgItem(hwndDlg, IDC_RDO_Z4_SEL);
		EnableWindow(hItemWnd, FALSE);
		SetDlgItemText(hwndDlg, IDC_STC_Z4_INFO, L"");

		// reget again zebra count, maybe some one is inserted or removed.
		hZhandle = pMultiZebra->m_FoundZebras = ZebraCardNumber();
		switch (hZhandle)
		{
			TCHAR *pUsbPortName, szPrintInfoBuffer[128];
			__PZEBRA_BOARD_INFO__ pZebraBdInfo;
		case 4:
			pZebraBdInfo = getZebraBoardInfo(--hZhandle, &pUsbPortName);
			if (pZebraBdInfo)
				swprintf(szPrintInfoBuffer, L"id:%04x  at  %-22s is a  %s  and  %s", pZebraBdInfo->nZebraBoardId, pUsbPortName,
				printZebraType(pZebraBdInfo->nZebraType), printfZebraStatus(pZebraBdInfo->nZebraStatus));
			SetDlgItemText(hwndDlg, IDC_STC_Z4_INFO, szPrintInfoBuffer);
			//.
			if (pZebraBdInfo->nZebraStatus == __Zebra_NotYet_Init__)
			{
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z4_INFO);
				EnableWindow(hItemWnd, TRUE);
				//.
				hItemWnd = GetDlgItem(hwndDlg, IDC_RDO_Z4_SEL);
				EnableWindow(hItemWnd, TRUE);
			}
			else if (pZebraBdInfo->nZebraStatus >= __Zebra_Open_DrvNoApp__)
			{
				// open by me
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z4_INFO);
				EnableWindow(hItemWnd, TRUE);
			}
			else //if (pZebraBdInfo->nZebraStatus < __Zebra_NotYet_Init__)
			{
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z4_INFO);
				EnableWindow(hItemWnd, FALSE);
			}

		case 3:
			pZebraBdInfo = getZebraBoardInfo(--hZhandle, &pUsbPortName);
			if (pZebraBdInfo)
				swprintf(szPrintInfoBuffer, L"id:%04x  at  %-22s is a  %s  and  %s", pZebraBdInfo->nZebraBoardId, pUsbPortName,
				printZebraType(pZebraBdInfo->nZebraType), printfZebraStatus(pZebraBdInfo->nZebraStatus));
			SetDlgItemText(hwndDlg, IDC_STC_Z3_INFO, szPrintInfoBuffer);
			//.
			if (pZebraBdInfo->nZebraStatus == __Zebra_NotYet_Init__)
			{
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z3_INFO);
				EnableWindow(hItemWnd, TRUE);
				//.
				hItemWnd = GetDlgItem(hwndDlg, IDC_RDO_Z3_SEL);
				EnableWindow(hItemWnd, TRUE);
			}
			else if (pZebraBdInfo->nZebraStatus >= __Zebra_Open_DrvNoApp__)
			{
				// open by me
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z3_INFO);
				EnableWindow(hItemWnd, TRUE);
			}
			else //if (pZebraBdInfo->nZebraStatus < __Zebra_NotYet_Init__)
			{
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z3_INFO);
				EnableWindow(hItemWnd, FALSE);
			}

		case 2:
			pZebraBdInfo = getZebraBoardInfo(--hZhandle, &pUsbPortName);
			if (pZebraBdInfo)
				swprintf(szPrintInfoBuffer, L"id:%04x  at  %-22s is a  %s  and  %s", pZebraBdInfo->nZebraBoardId, pUsbPortName,
				printZebraType(pZebraBdInfo->nZebraType), printfZebraStatus(pZebraBdInfo->nZebraStatus));
			SetDlgItemText(hwndDlg, IDC_STC_Z2_INFO, szPrintInfoBuffer);
			//.
			if (pZebraBdInfo->nZebraStatus == __Zebra_NotYet_Init__)
			{
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z2_INFO);
				EnableWindow(hItemWnd, TRUE);
				//.
				hItemWnd = GetDlgItem(hwndDlg, IDC_RDO_Z2_SEL);
				EnableWindow(hItemWnd, TRUE);
			}
			else if (pZebraBdInfo->nZebraStatus >= __Zebra_Open_DrvNoApp__)
			{
				// open by me
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z2_INFO);
				EnableWindow(hItemWnd, TRUE);
			}
			else //if (pZebraBdInfo->nZebraStatus < __Zebra_NotYet_Init__)
			{
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z2_INFO);
				EnableWindow(hItemWnd, FALSE);
			}

		case 1:
			pZebraBdInfo = getZebraBoardInfo(--hZhandle, &pUsbPortName);
			if (pZebraBdInfo)
				swprintf(szPrintInfoBuffer, L"id:%04x  at  %-22s is a  %s  and  %s", pZebraBdInfo->nZebraBoardId, pUsbPortName,
				printZebraType(pZebraBdInfo->nZebraType), printfZebraStatus(pZebraBdInfo->nZebraStatus));
			SetDlgItemText(hwndDlg, IDC_STC_Z1_INFO, szPrintInfoBuffer);
			//.
			if (pZebraBdInfo->nZebraStatus == __Zebra_NotYet_Init__)
			{
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z1_INFO);
				EnableWindow(hItemWnd, TRUE);
				//.
				hItemWnd = GetDlgItem(hwndDlg, IDC_RDO_Z1_SEL);
				EnableWindow(hItemWnd, TRUE);
			}
			else if (pZebraBdInfo->nZebraStatus >= __Zebra_Open_DrvNoApp__)
			{
				// open by me
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z1_INFO);
				EnableWindow(hItemWnd, TRUE);
			}
			else //if (pZebraBdInfo->nZebraStatus < __Zebra_NotYet_Init__)
			{
				hItemWnd = GetDlgItem(hwndDlg, IDC_STC_Z1_INFO);
				EnableWindow(hItemWnd, FALSE);
			}
			break;
		}
		return TRUE;

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
			pMultiZebra->m_sZebraUI[pMultiZebra->m_SelectIndx].m_AppZebraHandle = __USB_HANDLE_NOT_FOUND__;
			if (pMultiZebra->m_ZebraInfoDlgOpenMode == __zInfoDlgOpen_Select__)
			{
				__PZEBRA_BOARD_INFO__ pZebraBdInfo;
				hZhandle = pMultiZebra->m_FoundZebras;
				switch (hZhandle)
				{
				case 4:
					pZebraBdInfo = getZebraBoardInfo(--hZhandle, NULL);
					if (pZebraBdInfo->nZebraStatus == __Zebra_NotYet_Init__)
					{
						if (BST_CHECKED == (nSel = IsDlgButtonChecked(hwndDlg, IDC_RDO_Z4_SEL)))
						{
							pMultiZebra->m_sZebraUI[pMultiZebra->m_SelectIndx].m_AppZebraHandle = hZhandle;
							pMultiZebra->m_sZebraUI[pMultiZebra->m_SelectIndx].m_AppZebraId = pZebraBdInfo->nZebraBoardId;
							break;
						}
					}
					//.
				case 3:
					pZebraBdInfo = getZebraBoardInfo(--hZhandle, NULL);
					if (pZebraBdInfo->nZebraStatus == __Zebra_NotYet_Init__)
					{
						if (BST_CHECKED == (nSel = IsDlgButtonChecked(hwndDlg, IDC_RDO_Z3_SEL)))
						{
							pMultiZebra->m_sZebraUI[pMultiZebra->m_SelectIndx].m_AppZebraHandle = hZhandle;
							pMultiZebra->m_sZebraUI[pMultiZebra->m_SelectIndx].m_AppZebraId = pZebraBdInfo->nZebraBoardId;
							break;
						}
					}
					//.
				case 2:
					pZebraBdInfo = getZebraBoardInfo(--hZhandle, NULL);
					if (pZebraBdInfo->nZebraStatus == __Zebra_NotYet_Init__)
					{
						if (BST_CHECKED == (nSel = IsDlgButtonChecked(hwndDlg, IDC_RDO_Z2_SEL)))
						{
							pMultiZebra->m_sZebraUI[pMultiZebra->m_SelectIndx].m_AppZebraHandle = hZhandle;
							pMultiZebra->m_sZebraUI[pMultiZebra->m_SelectIndx].m_AppZebraId = pZebraBdInfo->nZebraBoardId;
							break;
						}
					}
					//.
				case 1:
					pZebraBdInfo = getZebraBoardInfo(--hZhandle, NULL);
					if (pZebraBdInfo->nZebraStatus == __Zebra_NotYet_Init__)
					{
						if (BST_CHECKED == (nSel = IsDlgButtonChecked(hwndDlg, IDC_RDO_Z1_SEL)))
						{
							pMultiZebra->m_sZebraUI[pMultiZebra->m_SelectIndx].m_AppZebraHandle = hZhandle;
							pMultiZebra->m_sZebraUI[pMultiZebra->m_SelectIndx].m_AppZebraId = pZebraBdInfo->nZebraBoardId;
							break;
						}
					}
				}	// switch
			}

			EndDialog(hwndDlg, wParam);	// for modal dialog box
			return TRUE;

		case IDCANCEL:
			EndDialog(hwndDlg, wParam);	// for modal dialog box

			// When modeless dialog --- CreateDialog(...)
			// DestroyWindow(hwndDlg);
			// g_hwndZebrs = NULL;
			return TRUE;
		}
	}
	return FALSE;
}


TCHAR *printZebraType(int nZebraType)
{
	switch (nZebraType)
	{
	case __ZebraType_Z4__:
		return L"Zebra 4";
	case __ZebraType_Z5__:
		return L"Zebra 5";
	case __ZebraType_Z4U3__:
		return L"Z4U3";
	}
	return NULL;
}

TCHAR *printfZebraStatus(int nZebraStatus)
{
	switch (nZebraStatus)
	{
	case __Zebra_Open_usable__:
		return L"open by me";
	case __Zebra_Open_DrvNoApp__:
		return L"open no AP";
	case __Zebra_NotYet_Init__:
		return L"valid     ";
	case __Zebra_InValid__:
		return L"in valid  ";
	case __Zebra_Removed__:
		return L"removed   ";
	}
	return L"unknown   ";
}

