// GattServerDlg.h : header file
//

#pragma once
#include "afxwin.h"

#include "..\..\..\..\Source\Bluetooth\wclBluetooth.h"
#include "afxcmn.h"

using namespace wclCommon;
using namespace wclBluetooth;

// CGattServerDlg dialog
class CGattServerDlg : public CDialog
{
// Construction
public:
	CGattServerDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
	enum { IDD = IDD_GATTSERVER_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support


// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()

	virtual BOOL PreTranslateMessage(MSG* pMsg) override;

private:
	CwclBluetoothManager FManager;
	CwclGattServer FServer;

	CButton btStart;
	CButton btStop;
	CButton btDisconnect;
	CListCtrl lvDevices;
	CEdit edData;
	CEdit edRepeats;
	CButton btNotify;
	CButton btNotifyAll;
	CComboBox cbParameters;
	CButton btSetParameters;
	CListBox lbLog;

	void Trace(const CString& Msg);
	void Trace(const BYTE Spaces, const CString& Msg);
	void Trace(const CString& Msg, const int Error);
	void Trace(const BYTE Spaces, const CString& Msg,
		const int Error);

	bool InitializeBluetooth(CwclBluetoothRadio*& Radio);
	void UninitializeBluetooth();

	bool InitializeServer(CwclBluetoothRadio* const Radio);
	void UninitializeServer();

	bool AddCharacteristics(CwclGattLocalService* const Service);
	CwclGattLocalService* AddService();

	void Start();
	void Stop();

	void BluetoothManagerAfterOpen(void* Sender);
	void BluetoothManagerBeforeClose(void* Sender);
	void BluetoothManagerClosed(void* Sender);

	int FindItem(CwclGattServerClient* const Client);

	void UpdateMaxPduSize(CwclGattServerClient* const Client);
	void UpdateConnectionParams(CwclGattServerClient* const Client);
	void UpdatePhy(CwclGattServerClient* const Client);
	void UpdateNotificationSize(CwclGattServerClient* const Client);

	void UpdateNotifyAllStatus();

	void Notify(const __int64 Address);

	void GattServerStarted(void* Sender);
	void GattServerStopped(void* Sender);
	void GattServerClientConnected(void* Sender, CwclGattServerClient* const Client);
	void GattServerClientDisconnected(void* Sender, CwclGattServerClient* const Client);
	void GattServerMaxPduSizeChanged(void* Sender, CwclGattServerClient* const Client);
	void GattServerConnectionParamsChanged(void* Sender, CwclGattServerClient* const Client);
	void GattServerConnectionPhyChanged(void* Sender, CwclGattServerClient* const Client);
	void GattServerNotificationSizeChanged(void* Sender, CwclGattServerClient* const Client);
	void GattServerSubscribed(void* Sender, CwclGattServerClient* const Client,
		CwclGattLocalCharacteristic* const Characteristic);
	void GattServerUnsubscribed(void* Sender, CwclGattServerClient* const Client,
		CwclGattLocalCharacteristic* const Characteristic);
	void GattServerWrite(void* Sender, CwclGattServerClient* const Client,
		CwclGattLocalCharacteristic* const Characteristic,
		CwclGattLocalCharacteristicWriteRequest* const Request);
	void GattServerRead(void* Sender, CwclGattServerClient* const Client,
		CwclGattLocalCharacteristic* const Characteristic,
		CwclGattLocalCharacteristicReadRequest* const Request);

public:
	afx_msg void OnBnClickedButtonClear();
	afx_msg void OnDestroy();
	afx_msg void OnBnClickedButtonStart();
	afx_msg void OnBnClickedButtonStop();
	afx_msg void OnLvnItemchangedListDevices(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnBnClickedButtonDisconnect();
	afx_msg void OnBnClickedButtonSetParameters();
	afx_msg void OnBnClickedButtonNotifyAll();
	afx_msg void OnBnClickedButtonNptify();
};
