// AudioReceiverDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AudioReceiver.h"
#include "AudioReceiverDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CString IntToHex(const int i)
{
	CString s;
	s.Format(_T("%.8X"), i);
	return s;
}

// CAudioReceiverDlg dialog



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

void CAudioReceiverDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_LOG, lbLog);
	DDX_Control(pDX, IDC_LIST_DEVICES, lvDevices);
}

BEGIN_MESSAGE_MAP(CAudioReceiverDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, &CAudioReceiverDlg::OnBnClickedButtonClear)
	ON_BN_CLICKED(IDC_BUTTON_START, &CAudioReceiverDlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CAudioReceiverDlg::OnBnClickedButtonStop)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_OPEN, &CAudioReceiverDlg::OnBnClickedButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CAudioReceiverDlg::OnBnClickedButtonClose)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, &CAudioReceiverDlg::OnBnClickedButtonConnect)
END_MESSAGE_MAP()


// CAudioReceiverDlg message handlers

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

	lvDevices.InsertColumn(0, _T("Device ID"), 0, 250);
	lvDevices.InsertColumn(1, _T("Name"), 0, 150);
	lvDevices.SetExtendedStyle(lvDevices.GetExtendedStyle() | LVS_EX_FULLROWSELECT);

	AudioWatcher = new CwclBluetoothAudioWatcher();
	__hook(&CwclBluetoothAudioWatcher::OnDeviceAdded, AudioWatcher,
		&CAudioReceiverDlg::AudioWatcherDeviceAdded);
	__hook(&CwclBluetoothAudioWatcher::OnDeviceRemoved, AudioWatcher,
		&CAudioReceiverDlg::AudioWatcherDeviceRemoved);
	__hook(&CwclBluetoothAudioWatcher::OnStarted, AudioWatcher,
		&CAudioReceiverDlg::AudioWatcherStarted);
	__hook(&CwclBluetoothAudioWatcher::OnStopped, AudioWatcher,
		&CAudioReceiverDlg::AudioWatcherStopped);

	AudioReceiver = new CwclBluetoothAudioReceiver();
	__hook(&CwclBluetoothAudioReceiver::OnClosed, AudioReceiver,
		&CAudioReceiverDlg::AudioReceiverClosed);
	__hook(&CwclBluetoothAudioReceiver::OnListen, AudioReceiver,
		&CAudioReceiverDlg::AudioReceiverListen);
	__hook(&CwclBluetoothAudioReceiver::OnConnected, AudioReceiver,
		&CAudioReceiverDlg::AudioReceiverConnected);
	__hook(&CwclBluetoothAudioReceiver::OnDisconnected, AudioReceiver,
		&CAudioReceiverDlg::AudioReceiverDisconnected);

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


void CAudioReceiverDlg::OnBnClickedButtonClear()
{
	lbLog.ResetContent();
}

void CAudioReceiverDlg::OnBnClickedButtonStart()
{
	int Res = AudioWatcher->Start();
	if (Res != WCL_E_SUCCESS)
		lbLog.AddString(_T("Watcher start failed: 0x") + IntToHex(Res));
}

void CAudioReceiverDlg::OnBnClickedButtonStop()
{
	int Res = AudioWatcher->Stop();
	if (Res != WCL_E_SUCCESS)
		lbLog.AddString(_T("Watcher stop failed: 0x") + IntToHex(Res));
}

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

	__unhook(AudioReceiver);
	AudioReceiver->Close();
	delete AudioReceiver;

	__unhook(AudioWatcher);
	AudioWatcher->Stop();
	delete AudioWatcher;
}

void CAudioReceiverDlg::AudioWatcherDeviceAdded(void* Sender, const tstring& Id,
	const tstring& Name)
{
	UNREFERENCED_PARAMETER(Sender);

	int Item = lvDevices.GetItemCount();
	lvDevices.InsertItem(Item, Id.c_str());
	lvDevices.SetItemText(Item, 1, Name.c_str());
	
	lbLog.AddString(CString(_T("Device added: ")) + CString(Id.c_str()));
}

void CAudioReceiverDlg::AudioWatcherDeviceRemoved(void* Sender, const tstring& Id)
{
	UNREFERENCED_PARAMETER(Sender);

	if (lvDevices.GetItemCount() > 0)
	{
		for (int i = 0; i < lvDevices.GetItemCount(); i++)
		{
			CString ItemId = lvDevices.GetItemText(i, 0);
			if (ItemId == CString(Id.c_str()))
			{
				lvDevices.DeleteItem(i);
				break;
			}
		}
	}
	
	lbLog.AddString(CString(_T("Device removed: ")) + CString(Id.c_str()));
}

void CAudioReceiverDlg::AudioWatcherStarted(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);
	
	lbLog.AddString(_T("Watcher started"));
	lvDevices.DeleteAllItems();
}

void CAudioReceiverDlg::AudioWatcherStopped(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);
	
	lbLog.AddString(_T("Watcher stopped"));
}

void CAudioReceiverDlg::AudioReceiverClosed(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	lbLog.AddString(_T("Audio receiver closed"));
}

void CAudioReceiverDlg::AudioReceiverListen(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);
	
	lbLog.AddString(_T("Audio receiver listening"));
}

void CAudioReceiverDlg::AudioReceiverConnected(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);
	
	lbLog.AddString(_T("Remote device connected"));
}

void CAudioReceiverDlg::AudioReceiverDisconnected(void* Sender)
{
	UNREFERENCED_PARAMETER(Sender);

	lbLog.AddString(_T("Remote device disconnected"));
}

void CAudioReceiverDlg::OnBnClickedButtonOpen()
{
	POSITION Pos = lvDevices.GetFirstSelectedItemPosition();
	if (Pos == NULL)
		AfxMessageBox(_T("Select device"));
	else
	{
		int Item = lvDevices.GetNextSelectedItem(Pos);
		int Res = AudioReceiver->Listen(tstring(lvDevices.GetItemText(Item, 0)));
		if (Res != WCL_E_SUCCESS)
			lbLog.AddString(_T("Open failed: 0x") + IntToHex(Res));
	}
}

void CAudioReceiverDlg::OnBnClickedButtonClose()
{
	int Res = AudioReceiver->Close();
	if (Res != WCL_E_SUCCESS)
		lbLog.AddString(_T("Close failed: 0x") + IntToHex(Res));
}

void CAudioReceiverDlg::OnBnClickedButtonConnect()
{
	int Res = AudioReceiver->Connect();
	if (Res != WCL_E_SUCCESS)
		lbLog.AddString(_T("Connect failed: 0x") + IntToHex(Res));
}
