#include "Dialogs.h"



BOOL ConnectDlg(LPENDPOINT lpEp, HWND hWndParent)
{
	if(DialogBoxParam(g_hInstance, MAKEINTRESOURCE(IDD_CONNECT_DIALOG), 
		hWndParent, ConnectDialogProc, (LPARAM)lpEp) == IDOK)
		return TRUE;
	else
		return FALSE;
}

BOOL SettingsDlg(HWND hWndParent)
{
	if(DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_SETTINGS_DIALOG), 
		hWndParent, SettingsDialogProc) == IDOK)
		return TRUE;
	else
		return FALSE;

}

INT_PTR CALLBACK AboutDialogProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
		
	case WM_INITDIALOG:		
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hWndDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

INT_PTR CALLBACK ConnectDialogProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_INITDIALOG:	
		SetWindowLongPtr(hWndDlg, GWLP_USERDATA, lParam);
		SetDlgItemInt(hWndDlg, IDC_SERVER_PORT, ((LPENDPOINT)lParam)->uPort, TRUE);
		SendDlgItemMessage(hWndDlg, IDC_SERVER_PORT_SPIN, UDM_SETRANGE32, 0, 65535);
		SendDlgItemMessage(hWndDlg, IDC_SERVER_PORT_SPIN, UDM_SETPOS32, 0, ((LPENDPOINT)lParam)->uPort);
		SendDlgItemMessage(hWndDlg, IDC_IPADDRESS, IPM_SETADDRESS, 0, ((LPENDPOINT)lParam)->dwAddr);

		return (INT_PTR)TRUE;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDOK:
			{
				LPENDPOINT lpEp = (LPENDPOINT) GetWindowLongPtr(hWndDlg, GWLP_USERDATA);					

				lpEp->uPort = GetDlgItemInt(hWndDlg, IDC_SERVER_PORT, NULL, FALSE);
				SendDlgItemMessage(hWndDlg, IDC_IPADDRESS, IPM_GETADDRESS, 0, (LPARAM)&lpEp->dwAddr);			

				EndDialog(hWndDlg, LOWORD(wParam));			
			}
			break;
		case IDCANCEL:
			{
				EndDialog(hWndDlg, LOWORD(wParam));	
			}
			break;

		case IDC_SERVER_PORT:
			if(HIWORD(wParam) == EN_UPDATE)
			{
				INT iVal = GetDlgItemInt(hWndDlg, LOWORD(wParam), NULL, TRUE);

				if(iVal > 0xffff)
				{
					EDITBALLOONTIP ebt;
					ebt.cbStruct	= sizeof(EDITBALLOONTIP);
					ebt.pszTitle	= TEXT("Neprijateľná hodnota");
					ebt.pszText		= TEXT("Číslo portu serveru môže nadobúdať len hodnoty 0 až 65535.");
					ebt.ttiIcon		= TTI_ERROR;
					SetDlgItemInt(hWndDlg, LOWORD(wParam), 0xffff, TRUE);
					Edit_ShowBalloonTip((HWND)lParam, &ebt);
				}
				else if(iVal < 0)
				{					
					SetDlgItemInt(hWndDlg, LOWORD(wParam), 0, TRUE);					
				}
			}
			break;

		}
		return (INT_PTR)TRUE;
		break;
	}
	return (INT_PTR)FALSE;
}

INT_PTR CALLBACK SettingsDialogProc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{		
	case WM_INITDIALOG:
		{
			TCHAR	szCamName[256];
			INT		i, cnt;	

			SetDlgItemInt(hWndDlg, IDC_SERVER_PORT, g_sSettings.iPort, TRUE);
			SetDlgItemInt(hWndDlg, IDC_VIDEO_W, g_sSettings.iVideoW, TRUE);
			SetDlgItemInt(hWndDlg, IDC_VIDEO_H, g_sSettings.iVideoH, TRUE);

			SendDlgItemMessage(hWndDlg, IDC_SERVER_PORT_SPIN, UDM_SETRANGE32, 0, 65535);
			SendDlgItemMessage(hWndDlg, IDC_SERVER_PORT_SPIN, UDM_SETPOS32, 0, g_sSettings.iPort);

			cnt = SendFGMessage(g_lpFrameGrabber, FG_LISTDEV, 0,0);

			if(cnt == 0)
				EnableWindow(GetDlgItem(hWndDlg, IDC_CAM_SETTINGS), FALSE);

			for(i = 0; i < cnt; i++)
			{
				FGGetDeviceName(g_lpFrameGrabber, i, szCamName, 256);
				SendDlgItemMessage(hWndDlg, IDC_CAMERA, CB_ADDSTRING, 0, (LPARAM)szCamName);
			}
			SendDlgItemMessage(hWndDlg, IDC_CAMERA, CB_SETCURSEL, g_sSettings.iVIDeviceID, 0);

		}
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDOK:
			g_sSettings.iVIDeviceID = SendDlgItemMessage(hWndDlg, IDC_CAMERA, CB_GETCURSEL, 0, 0);
			g_sSettings.iPort		= GetDlgItemInt(hWndDlg, IDC_SERVER_PORT, NULL, TRUE);
			g_sSettings.iVideoW		= GetDlgItemInt(hWndDlg, IDC_VIDEO_W, NULL, TRUE);
			g_sSettings.iVideoH		= GetDlgItemInt(hWndDlg, IDC_VIDEO_H, NULL, TRUE);
			EndDialog(hWndDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
			
		case IDCANCEL:
			EndDialog(hWndDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;

		case IDC_CAM_SETTINGS:
			{
				INT iDevId = SendDlgItemMessage(hWndDlg, IDC_CAMERA, CB_GETCURSEL, 0, 0);
				SendFGMessage(g_lpFrameGrabber, FG_SHOWSETTINGS, iDevId, 0);
				return (INT_PTR)TRUE;
			}
			break;	

		case IDC_SERVER_PORT:
			if(HIWORD(wParam) == EN_UPDATE)
			{
				INT iVal = GetDlgItemInt(hWndDlg, LOWORD(wParam), NULL, TRUE);

				if(iVal > 0xffff)
				{
					EDITBALLOONTIP ebt;
					ebt.cbStruct	= sizeof(EDITBALLOONTIP);
					ebt.pszTitle	= TEXT("Neprijateľná hodnota");
					ebt.pszText		= TEXT("Číslo portu serveru môže nadobúdať len hodnoty 0 až 65535.");
					ebt.ttiIcon		= TTI_ERROR;
					SetDlgItemInt(hWndDlg, LOWORD(wParam), 0xffff, TRUE);
					Edit_ShowBalloonTip((HWND)lParam, &ebt);
				}
				else if(iVal < 0)
				{					
					SetDlgItemInt(hWndDlg, LOWORD(wParam), 0, TRUE);					
				}
			}
			break;
		}		
		break;	
	}
	return (INT_PTR)FALSE;
}