// MobileHotspotDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MobileHotspot.h"
#include "MobileHotspotDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMobileHotspotDlg dialog

CString IntToStr(const unsigned long Val)
{
	CString s;
	s.Format(_T("%d"), Val);
	return s;
}


CMobileHotspotDlg::CMobileHotspotDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMobileHotspotDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMobileHotspotDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, edSsid);
	DDX_Control(pDX, IDC_EDIT2, edPassword);
	DDX_Control(pDX, IDC_COMBO1, cbBand);
	DDX_Control(pDX, IDC_LIST_CONNECTED_CLIENTS, lvConnectedClients);
}

BEGIN_MESSAGE_MAP(CMobileHotspotDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_OPEN, &CMobileHotspotDlg::OnBnClickedButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON2, &CMobileHotspotDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CMobileHotspotDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CMobileHotspotDlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON9, &CMobileHotspotDlg::OnBnClickedButton9)
	ON_BN_CLICKED(IDC_BUTTON5, &CMobileHotspotDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON7, &CMobileHotspotDlg::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON6, &CMobileHotspotDlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON8, &CMobileHotspotDlg::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON10, &CMobileHotspotDlg::OnBnClickedButton10)
	ON_BN_CLICKED(IDC_BUTTON11, &CMobileHotspotDlg::OnBnClickedButton11)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_IS_SUPPORTED, &CMobileHotspotDlg::OnBnClickedButtonIsSupported)
	ON_BN_CLICKED(IDC_BUTTON1_GET_BAND, &CMobileHotspotDlg::OnBnClickedButton1GetBand)
	ON_BN_CLICKED(IDC_BUTTON1_SET_BAND, &CMobileHotspotDlg::OnBnClickedButton1SetBand)
	ON_BN_CLICKED(IDC_BUTTON_BUTTON_CONNECTED_CLIENTS, &CMobileHotspotDlg::OnBnClickedButtonButtonConnectedClients)
	ON_BN_CLICKED(IDC_BUTTON_CHECK_TIMEOUT, &CMobileHotspotDlg::OnBnClickedButtonCheckTimeout)
	ON_BN_CLICKED(IDC_BUTTON1_ENABLE_TIMEOUT, &CMobileHotspotDlg::OnBnClickedButton1EnableTimeout)
	ON_BN_CLICKED(IDC_BUTTON1_DISABLE_TIMEOUT, &CMobileHotspotDlg::OnBnClickedButton1DisableTimeout)
END_MESSAGE_MAP()

void CMobileHotspotDlg::Trace(const CString& Msg)
{
	AfxMessageBox(Msg);
}

void CMobileHotspotDlg::Trace(const CString& Msg, const int Error)
{
	CString s;
	s.Format(_T(". Error: 0x%.8X"), Error);
	s = Msg + s;
	Trace(s);
}


// CMobileHotspotDlg message handlers

BOOL CMobileHotspotDlg::OnInitDialog()
{
	CDialog::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

	// TODO: Add extra initialization here

	cbBand.AddString(_T("Auto"));
	cbBand.AddString(_T("2.4"));
	cbBand.AddString(_T("6.8"));
	cbBand.SetCurSel(0);
	
	lvConnectedClients.InsertColumn(0, _T("Mac"), 0, 150);
	lvConnectedClients.InsertColumn(1, _T("Host name"), 0, 250);
	lvConnectedClients.SetExtendedStyle(lvConnectedClients.GetExtendedStyle() | LVS_EX_FULLROWSELECT);
	
	__hook(&CwclMobileHotspot::OnOpened, &wclMobileHotspot, &CMobileHotspotDlg::HotspotOpened);
	__hook(&CwclMobileHotspot::OnClosed, &wclMobileHotspot, &CMobileHotspotDlg::HotspotClosed);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CMobileHotspotDlg::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
	{
		CDialog::OnPaint();
	}
}

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


void CMobileHotspotDlg::OnBnClickedButtonOpen()
{
	int Res = wclMobileHotspot.Open();
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Open failed"), Res);
}

void CMobileHotspotDlg::OnBnClickedButton2()
{
	int Res = wclMobileHotspot.Close();
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Close failed"), Res);
}

void CMobileHotspotDlg::HotspotOpened(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	Trace(_T("Mobile hotspot opened."));
	tstring Ssid;
	if (wclMobileHotspot.GetSsid(Ssid) == WCL_E_SUCCESS)
		edSsid.SetWindowText(Ssid.c_str());
	tstring Passphrase;
	if (wclMobileHotspot.GetPassphrase(Passphrase) == WCL_E_SUCCESS)
		edPassword.SetWindowText(Passphrase.c_str());
}

void CMobileHotspotDlg::HotspotClosed(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	Trace(_T("Mobile hotspot closed."));
	edSsid.SetWindowText(_T(""));
	edPassword.SetWindowText(_T(""));
}

void CMobileHotspotDlg::OnBnClickedButton3()
{
	int Res = wclMobileHotspot.Start();
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Start failed"), Res);
}

void CMobileHotspotDlg::OnBnClickedButton4()
{
	int Res = wclMobileHotspot.Stop();
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Stop failed"), Res);
}

void CMobileHotspotDlg::OnBnClickedButton9()
{
	wclMobileHotspotState State;
	int Res = wclMobileHotspot.GetState(State);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Get state failed"), Res);
	else
	{
		switch (State)
		{
			case mhsUnknown:
				Trace(_T("State: unknown"));
				break;
			case mhsOn:
				Trace(_T("State: on"));
				break;
			case mhsOff:
				Trace(_T("State: off"));
				break;
			case mhsInTransition:
				Trace(_T("State: switching"));
				break;
		}
	}
}

void CMobileHotspotDlg::OnBnClickedButton5()
{
	tstring Ssid;
	int Res = wclMobileHotspot.GetSsid(Ssid);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Get SSID failed"), Res);
	else
		edSsid.SetWindowText(Ssid.c_str());
}

void CMobileHotspotDlg::OnBnClickedButton7()
{
	CString s;
	edSsid.GetWindowText(s);
	int Res = wclMobileHotspot.SetSsid(tstring(s.GetBuffer()));
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Set SSID failed"), Res);
}

void CMobileHotspotDlg::OnBnClickedButton6()
{
	tstring Pwd;
	int Res = wclMobileHotspot.GetPassphrase(Pwd);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Get passphrase failed"), Res);
	else
		edPassword.SetWindowText(Pwd.c_str());
}

void CMobileHotspotDlg::OnBnClickedButton8()
{
	CString s;
	edPassword.GetWindowText(s);
	int Res = wclMobileHotspot.SetPassphrase(tstring(s.GetBuffer()));
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Set passphrase failed"), Res);
}

void CMobileHotspotDlg::OnBnClickedButton10()
{
	unsigned long Count;
	int Res = wclMobileHotspot.GetMaxClientCount(Count);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Get max client connections failed"), Res);
	else
		Trace(_T("Maximum allowed connections: ") + IntToStr(Count));
}

void CMobileHotspotDlg::OnBnClickedButton11()
{
	unsigned long Count;
	int Res = wclMobileHotspot.GetClientCount(Count);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Get connectioned clients count failed"), Res);
	else
		Trace(_T("Connected clients: ") + IntToStr(Count));
}

void CMobileHotspotDlg::OnDestroy()
{
	CDialog::OnDestroy();

	// TODO: Add your message handler code here

	wclMobileHotspot.Close();
	__unhook(&wclMobileHotspot);
}

void CMobileHotspotDlg::OnBnClickedButtonIsSupported()
{
	wclMobileHotspotBand Band;
	switch (cbBand.GetCurSel())
	{
		case 0:
			Band = mhbAuto;
			break;
		case 1:
			Band = mhbTwoPointFourGigahertz;
			break;
		default:
			Band = mhbFiveGigahertz;
			break;
	}
	
	bool Supp;
	int Res = wclMobileHotspot.IsBandSupported(Band, Supp);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Check band failed"), Res);
	else
	{
		if (Supp)
			AfxMessageBox(_T("Band is supported"));
		else
			AfxMessageBox(_T("Band is NOT supported"));
	}
}

void CMobileHotspotDlg::OnBnClickedButton1GetBand()
{
	wclMobileHotspotBand Band;
	int Res = wclMobileHotspot.GetBand(Band);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Get band failed"), Res);
	else
	{
		switch (Band)
		{
			case mhbAuto:
				AfxMessageBox(_T("Band: Auto"));
				break;
			case mhbTwoPointFourGigahertz:
				AfxMessageBox(_T("Band: 2.4"));
				break;
			default:
				AfxMessageBox(_T("Band: 5.8"));
				break;
		}
	}
}

void CMobileHotspotDlg::OnBnClickedButton1SetBand()
{
	wclMobileHotspotBand Band;
	switch (cbBand.GetCurSel())
	{
		case 0:
			Band = mhbAuto;
			break;
		case 1:
			Band = mhbTwoPointFourGigahertz;
			break;
		default:
			Band = mhbFiveGigahertz;
			break;
	}
	
	int Res = wclMobileHotspot.SetBand(Band);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Set band failed"), Res);
}

void CMobileHotspotDlg::OnBnClickedButtonButtonConnectedClients()
{
	lvConnectedClients.DeleteAllItems();
	wclMobileHotspotClients Clients;
	int Res = wclMobileHotspot.GetClients(Clients);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Get connected clients failed"), Res);
	else
	{
		if (Clients.size() > 0)
		{
			int j = 0;
			for (wclMobileHotspotClients::iterator i = Clients.begin(); i != Clients.end(); i++)
			{
				lvConnectedClients.InsertItem(j, (*i).Mac.c_str());
				lvConnectedClients.SetItemText(j, 1, (*i).Name.c_str());
				j++;
			}
		}
	}
}

void CMobileHotspotDlg::OnBnClickedButtonCheckTimeout()
{
	bool Enabled;
	int Res = wclMobileHotspot.IsNoConnectionsTimeoutEnabled(Enabled);
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Get no connection timeout status failed"), Res);
	else
	{
		if (Enabled)
			AfxMessageBox(_T("No connection timeout enabled"));
		else
			AfxMessageBox(_T("No connection timeout disabled"));
	}
}

void CMobileHotspotDlg::OnBnClickedButton1EnableTimeout()
{
	int Res = wclMobileHotspot.EnableNoConnectionsTimeout();
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Enable No Connection Timeout failed"), Res);
}

void CMobileHotspotDlg::OnBnClickedButton1DisableTimeout()
{
	int Res = wclMobileHotspot.DisableNoConnectionsTimeout();
	if (Res != WCL_E_SUCCESS)
		Trace(_T("Disable No Connection Timeout failed"), Res);
}
