//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <iostream>

#include "wclMessaging.hpp"
#include "wclErrors.hpp"
#include "wclBluetooth.hpp"

#pragma argsused
#pragma warn -8027
#pragma warn -8026

class TwclConsole : TObject
{
private:
  HANDLE FEvent;

  void __fastcall WriteLn(String Str)
  {
    AnsiString s(Str);
    std::cout << s.c_str() << std::endl;
  }

  void __fastcall ManagerAfterOpen(TObject* Sender)
  {
    SetEvent(FEvent);
  }

  void __fastcall ManagerClosed(TObject* Sender)
  {
    SetEvent(FEvent);
  }

  #ifndef _DXE3_
  void __fastcall ManagerDiscoveringStarted(TObject* Sender,
        const TwclBluetoothRadio* Radio)
  #else
  void __fastcall ManagerDiscoveringStarted(TObject* Sender,
        TwclBluetoothRadio* const Radio)
  #endif
  {
        WriteLn("Discovering has been started");
  }

  #ifndef _DXE3_
  void __fastcall ManagerDiscoveringCompleted(TObject* Sender,
        const TwclBluetoothRadio* Radio, const int Error)
  #else
  void __fastcall ManagerDiscoveringCompleted(TObject* Sender,
        TwclBluetoothRadio* const Radio, const int Error)
  #endif
  {
	WriteLn("Discoverign has been compledted");
	SetEvent(FEvent);
  }

  #ifndef _DXE3_
  void __fastcall ManagerDeviceFound(TObject* Sender,
        const TwclBluetoothRadio* Radio, const __int64 Address)
  #else
  void __fastcall ManagerDeviceFound(TObject* Sender,
        TwclBluetoothRadio* const Radio, const __int64 Address)
  #endif
  {
	WriteLn("Device " + IntToHex(Address, 12) + " found");
  }

public:
  void Run()
  {
	WriteLn("Bluetooth Framework Console Demo application");
	WriteLn("  The demo is very simple: it just discovers nearby classic and LE");
	WriteLn("  devices.");
	WriteLn("  The demo shows how to use Bluetooth Framework in console");
        WriteLn("  applications with APC synchronization.");
        system("pause");

        // Create classes we have to use.
        TwclBluetoothManager* Manager = new TwclBluetoothManager(NULL);
        Manager->MessageProcessing = mpAsync;
        Manager->AfterOpen = ManagerAfterOpen;
        Manager->OnClosed = ManagerClosed;
        Manager->OnDeviceFound = ManagerDeviceFound;
        Manager->OnDiscoveringCompleted = ManagerDiscoveringCompleted;
        Manager->OnDiscoveringStarted = ManagerDiscoveringStarted;

        // The event used to notify about event completion.
        FEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
        if (FEvent == NULL)
                WriteLn("Unable to create synchronization event!");
        else
        {
          // Open Bluetooth Manager
          int Res = Manager->Open();
          if (Res != WCL_E_SUCCESS)
            WriteLn("Open Bluetooth Manager failed: 0x" + IntToHex(Res, 8));
          else
          {
            // Wait for Open completion.
            WaitForSingleObject(FEvent, INFINITE);

            // Try to find available Bluetooth Radio. Use first one found.
            if (Manager->Count == 0)
              WriteLn("No Bluetooth Hardware connected.");
            else
            {
              TwclBluetoothRadio* Radio = NULL;
              for (int i = 0; i < Manager->Count; i++)
              {
                if (Manager->Radios[i]->Available)
                {
                  Radio = Manager->Radios[i];
                  break;
                }
              }

              if (Radio == NULL)
                WriteLn("Available BluetoothRadio not found.");
              else
              {
                // Discover classic devices
                WriteLn("Try to start classic discovering");
                Res = Radio->Discover(10, dkClassic);
                if (Res != WCL_E_SUCCESS)
                  WriteLn("Start classic discovering failed: 0x" + IntToHex(Res, 8));
                else
                  // Wait for discovering completion.
                  WaitForSingleObject(FEvent, INFINITE);

                // Discover LE devices
                WriteLn("Try to start LE discovering");
                Res = Radio->Discover(10, dkBle);
                if (Res != WCL_E_SUCCESS)
                  WriteLn("Start LE discovering failed: 0x" + IntToHex(Res, 8));
                else
                  // Wait for discovering completion.
                  WaitForSingleObject(FEvent, INFINITE);
              }
            }

            // Close Bluetooth Manager.
            Res = Manager->Close();
            if (Res != WCL_E_SUCCESS)
              WriteLn("Open Bluetooth Manager failed: 0x" + IntToHex(Res, 8));
            else
              // Wait for Close completion.
              WaitForSingleObject(FEvent, INFINITE);
          }

          // Do not forget about event.
          CloseHandle(FEvent);
        }

        // Do not forget to destroy Bluetooth Manager.
        delete Manager;

        system("pause");
      }
};

#ifndef _DXE2_
  int main(int argc, Shortint* argv[])
#else
  int main(int argc, char* argv[])
#endif
{
  TwclConsole* c = new TwclConsole();
  c->Run();
  delete c;

  return 0;
}
