// WiFiDirectSoftAPDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WiFiDirectSoftAP.h"
#include "WiFiDirectSoftAPDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CWiFiDirectSoftAPDlg dialog




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

void CWiFiDirectSoftAPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_SSID, edSsid);
	DDX_Control(pDX, IDC_EDIT_PASSPHRASE, edPassphrase);
	DDX_Control(pDX, IDC_LIST_LOG, lbLog);
}

BEGIN_MESSAGE_MAP(CWiFiDirectSoftAPDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_START, &CWiFiDirectSoftAPDlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CWiFiDirectSoftAPDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_GET_IP_SETTINGS, &CWiFiDirectSoftAPDlg::OnBnClickedButtonGetIpSettings)
	ON_BN_CLICKED(IDC_BUTTON_SET_IP_SETTINGS, &CWiFiDirectSoftAPDlg::OnBnClickedButtonSetIpSettings)
	ON_BN_CLICKED(IDC_BUTTON_RESET_IP_SETTINGS, &CWiFiDirectSoftAPDlg::OnBnClickedButtonResetIpSettings)
END_MESSAGE_MAP()


// CWiFiDirectSoftAPDlg message handlers

BOOL CWiFiDirectSoftAPDlg::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

	__hook(&CwclWiFiSoftAP::OnStarted, &wclWiFiSoftAP, &CWiFiDirectSoftAPDlg::SoftApStarted);
	__hook(&CwclWiFiSoftAP::OnStopped, &wclWiFiSoftAP, &CWiFiDirectSoftAPDlg::SoftApStopped);
	__hook(&CwclWiFiSoftAP::OnDeviceAccept, &wclWiFiSoftAP, &CWiFiDirectSoftAPDlg::SoftApDeviceAccept);
	__hook(&CwclWiFiSoftAP::OnDeviceConnected, &wclWiFiSoftAP, &CWiFiDirectSoftAPDlg::SoftApDeviceConnected);
	__hook(&CwclWiFiSoftAP::OnDeviceConnectError, &wclWiFiSoftAP, &CWiFiDirectSoftAPDlg::SoftApDeviceConnectError);
	__hook(&CwclWiFiSoftAP::OnDeviceDisconnected, &wclWiFiSoftAP, &CWiFiDirectSoftAPDlg::SoftApDeviceDisconnected);

	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 CWiFiDirectSoftAPDlg::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 CWiFiDirectSoftAPDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


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

	wclWiFiSoftAP.Stop();
	__unhook(&wclWiFiSoftAP);
}

void CWiFiDirectSoftAPDlg::SoftApStarted(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	lbLog.AddString(_T("Started"));

	tstring Str;
	CString s;

	int Res = wclWiFiSoftAP.GetSsid(Str);
	if (Res == WCL_E_SUCCESS)
		s = CString(Str.c_str());
	else
		s.Format(_T("Error 0x%.8X"), Res);
	lbLog.AddString(_T("SSID: ") + s);

	Res = wclWiFiSoftAP.GetPassphrase(Str);
	if (Res == WCL_E_SUCCESS)
		s = CString(Str.c_str());
	else
		s.Format(_T("Error 0x%.8X"), Res);
	lbLog.AddString(_T("Passphrase: ") + s);
}

void CWiFiDirectSoftAPDlg::SoftApStopped(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	lbLog.AddString(_T("Stopped"));
}

void CWiFiDirectSoftAPDlg::SoftApDeviceConnected(void* Sender, 
	CwclWiFiDirectDevice* const Device)
{
	UNREFERENCED_PARAMETER(Sender);

	tstring s = _T("Device connected: ") + Device->Name;
	lbLog.AddString(s.c_str());
	s = _T("  Local IP: ") + Device->LocalAddress;
	lbLog.AddString(s.c_str());
	s = _T("  Remote IP: ") + Device->RemoteAddress;
	lbLog.AddString(s.c_str());
}

void CWiFiDirectSoftAPDlg::SoftApDeviceAccept(void* Sender, 
	CwclWiFiDirectDevice* const Device, bool& Accept)
{
	UNREFERENCED_PARAMETER(Sender);

	tstring s = _T("Accept connection from ") + Device->Name + _T(" ?");
	Accept = (AfxMessageBox(s.c_str(), MB_YESNO | MB_APPLMODAL | MB_ICONQUESTION) == IDYES);
}

void CWiFiDirectSoftAPDlg::SoftApDeviceConnectError(void* Sender, 
	CwclWiFiDirectDevice* const Device, const int Error)
{
	UNREFERENCED_PARAMETER(Sender);

	tstring s = _T("Device ") + Device->Name + _T(" connect error: ");
	CString cs;
	cs.Format(_T("0x%.8X"), Error);
	CString ss(s.c_str());
	ss = ss + cs;
	lbLog.AddString(ss);
}

void CWiFiDirectSoftAPDlg::SoftApDeviceDisconnected(void* Sender, 
	CwclWiFiDirectDevice* const Device)
{
	UNREFERENCED_PARAMETER(Sender);

	tstring s = _T("Device disconnected: ") + Device->Name;
	lbLog.AddString(s.c_str());
	s = _T("  Local IP: ") + Device->LocalAddress;
	lbLog.AddString(s.c_str());
	s = _T("  Remote IP: ") + Device->RemoteAddress;
	lbLog.AddString(s.c_str());
}

void CWiFiDirectSoftAPDlg::OnBnClickedButtonStart()
{
	CString Ssid;
	CString Passphrase;
	edSsid.GetWindowText(Ssid);
	edPassphrase.GetWindowText(Passphrase);
	int Res = wclWiFiSoftAP.Start(tstring(Ssid), tstring(Passphrase));
	CString s;
	s.Format(_T("Start: 0x%.8X"), Res);
	lbLog.AddString(s);
}

void CWiFiDirectSoftAPDlg::OnBnClickedButtonStop()
{
	int Res = wclWiFiSoftAP.Stop();
	CString s;
	s.Format(_T("Stop: 0x%.8X"), Res);
	lbLog.AddString(s);
}

void CWiFiDirectSoftAPDlg::OnBnClickedButtonGetIpSettings()
{
	tstring Address;
	tstring Mask;
	tstring Gateway;
	tstring Dns1;
	tstring Dns2;
	int Res = wclWiFiSoftAP.GetIpSettings(Address, Mask, Gateway, Dns1, Dns2);
	if (Res != WCL_E_SUCCESS)
	{
		CString s;
		s.Format(_T("Read IP settings failed: 0x%.8X"), Res);
		lbLog.AddString(s);
	}
	else
	{
		lbLog.AddString(_T("IP settings:"));
		tstring s = _T("  Address: ") + Address;
		lbLog.AddString(s.c_str());
		s = _T("  Mask: ") + Mask;
		lbLog.AddString(s.c_str());
		s = _T("  Gateway: ") + Gateway;
		lbLog.AddString(s.c_str());
		s = _T("  Dns1: ") + Dns1;
		lbLog.AddString(s.c_str());
		s = _T("  Dns2: ") + Dns2;
		lbLog.AddString(s.c_str());
	}
}

void CWiFiDirectSoftAPDlg::OnBnClickedButtonSetIpSettings()
{
	int Res = wclWiFiSoftAP.SetIpSettings(_T("192.168.2.10"), _T("255.255.255.0"),
		_T("192.168.2.1"), _T(""), _T(""));
	if (Res != WCL_E_SUCCESS)
	{
		CString s;
		s.Format(_T("Write IP settings failed: 0x%.8X"), Res);
		lbLog.AddString(s);
	}
}

void CWiFiDirectSoftAPDlg::OnBnClickedButtonResetIpSettings()
{
	int Res = wclWiFiSoftAP.ResetIpSettings();
	if (Res != WCL_E_SUCCESS)
	{
		CString s;
		s.Format(_T("Reset IP settings failed: 0x%.8X"), Res);
		lbLog.AddString(s);
	}
}
