using Android.Bluetooth;
using Android.Content;
using System;
namespace MyLibrary.Bluetooth
{
    public class BLEConnector
    {
        private BluetoothGatt _bluetoothGatt;
        private BluetoothGattCallback _gattCallback;
        public BLEConnector()
        {
            _gattCallback = new CustomGattCallback();
        }
        /// 
        /// Connect to the specified BluetoothDevice.
        /// 
        /// The BluetoothDevice to connect to.
        /// The Android context required for the connection.
        public void Connect(BluetoothDevice device, Context context)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device), "Device cannot be null");
            }
            // Initiate the connection
            _bluetoothGatt = device.ConnectGatt(context, false, _gattCallback);
        }
        /// 
        /// Disconnect the current connection.
        /// 
        public void Disconnect()
        {
            _bluetoothGatt?.Disconnect();
            _bluetoothGatt?.Close();
            _bluetoothGatt = null;
        }
        /// 
        /// Custom GATT callback to handle Bluetooth events.
        /// 
        private class CustomGattCallback : BluetoothGattCallback
        {
            public override void OnConnectionStateChange(BluetoothGatt gatt, GattStatus status, ProfileState newState)
            {
                base.OnConnectionStateChange(gatt, status, newState);
                if (status == GattStatus.Success && newState == ProfileState.Connected)
                {
                    Console.WriteLine("Connected to device!");
                    gatt.DiscoverServices();
                }
                else if (newState == ProfileState.Disconnected)
                {
                    Console.WriteLine("Disconnected from device.");
                }
                else
                {
                    Console.WriteLine($"Connection state changed: {newState}, Status: {status}");
                }
            }
            public override void OnServicesDiscovered(BluetoothGatt gatt, GattStatus status)
            {
                base.OnServicesDiscovered(gatt, status);
                if (status == GattStatus.Success)
                {
                    Console.WriteLine("Services discovered:");
                    foreach (var service in gatt.Services)
                    {
                        Console.WriteLine($"Service: {service.Uuid}");
                    }
                }
                else
                {
                    Console.WriteLine($"Failed to discover services. Status: {status}");
                }
            }
            public override void OnCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
            {
                base.OnCharacteristicRead(gatt, characteristic, status);
                if (status == GattStatus.Success)
                {
                    Console.WriteLine($"Characteristic Read: {characteristic.Uuid}, Value: {BitConverter.ToString(characteristic.GetValue())}");
                }
            }
            public override void OnCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, GattStatus status)
            {
                base.OnCharacteristicWrite(gatt, characteristic, status);
                if (status == GattStatus.Success)
                {
                    Console.WriteLine($"Characteristic Write Successful: {characteristic.Uuid}");
                }
            }
            public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
            {
                base.OnCharacteristicChanged(gatt, characteristic);
                Console.WriteLine($"Characteristic Changed: {characteristic.Uuid}, Value: {BitConverter.ToString(characteristic.GetValue())}");
            }
        }
    }
}