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

#include <vcl.h>
#pragma hdrstop

#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "wclAudio"
#pragma resource "*.dfm"
TfmMain *fmMain;
//---------------------------------------------------------------------------
__fastcall TfmMain::TfmMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::btClearClick(TObject *Sender)
{
        lbLog->Items->Clear();        
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::btStartClick(TObject *Sender)
{
        int Res = wclBluetoothAudioWatcher->Start();
        if (Res != WCL_E_SUCCESS)
                lbLog->Items->Add("Watcher start failed: 0x" + IntToHex(Res, 8));
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::btStopClick(TObject *Sender)
{
        int Res = wclBluetoothAudioWatcher->Stop();
        if (Res != WCL_E_SUCCESS)
                lbLog->Items->Add("Watcher stop failed: 0x" + IntToHex(Res, 8));
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::FormDestroy(TObject *Sender)
{
        wclBluetoothAudioWatcher->Stop();
        wclBluetoothAudioReceiver->Close();        
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::wclBluetoothAudioWatcherDeviceAdded(
      TObject *Sender, const String Id, const String Name)
{
        TListItem* Item = lvDevices->Items->Add();
        Item->Caption = Id;
        Item->SubItems->Add(Name);

        lbLog->Items->Add("Device added: " + Id);
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::wclBluetoothAudioWatcherDeviceRemoved(
      TObject *Sender, const String Id)
{
        if (lvDevices->Items->Count > 0)
        {
                for (int i = 0; i < lvDevices->Items->Count; i++)
                {
                        if (lvDevices->Items->Item[i]->Caption == Id)
                        {
                                lvDevices->Items->Delete(i);
                                break;
                        }
                }
        }

        lbLog->Items->Add("Device removed: " + Id);
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::wclBluetoothAudioWatcherStarted(TObject *Sender)
{
        lbLog->Items->Add("Watcher started");
        lvDevices->Items->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::wclBluetoothAudioWatcherStopped(TObject *Sender)
{
        lbLog->Items->Add("Watcher stopped");        
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::wclBluetoothAudioReceiverClosed(TObject *Sender)
{
        lbLog->Items->Add("Audio receiver closed");
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::wclBluetoothAudioReceiverListen(TObject *Sender)
{
        lbLog->Items->Add("Audio receiver listening");
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::wclBluetoothAudioReceiverConnected(
      TObject *Sender)
{
        lbLog->Items->Add("Remote device connected");
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::wclBluetoothAudioReceiverDisconnected(
      TObject *Sender)
{
        lbLog->Items->Add("Remote device disconnected");
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::btOpenClick(TObject *Sender)
{
        if (lvDevices->Selected == NULL)
                ShowMessage("Select device");
        else
        {
                int Res = wclBluetoothAudioReceiver->Listen(
                        lvDevices->Selected->Caption);
                if (Res != WCL_E_SUCCESS)
                        lbLog->Items->Add("Open failed: 0x" + IntToHex(Res, 8));
        }
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::btCloseClick(TObject *Sender)
{
        int Res = wclBluetoothAudioReceiver->Close();
        if (Res != WCL_E_SUCCESS)
                lbLog->Items->Add("Close failed: 0x" + IntToHex(Res, 8));
}
//---------------------------------------------------------------------------
void __fastcall TfmMain::btConnectClick(TObject *Sender)
{
        int Res = wclBluetoothAudioReceiver->Connect();
        if (Res != WCL_E_SUCCESS)
                lbLog->Items->Add("Connect failed: 0x" + IntToHex(Res, 8));
}
//---------------------------------------------------------------------------

