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();
        }

        /// <summary>
        /// Connect to the specified BluetoothDevice.
        /// </summary>
        /// <param name="device">The BluetoothDevice to connect to.</param>
        /// <param name="context">The Android context required for the connection.</param>
        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);
        }

        /// <summary>
        /// Disconnect the current connection.
        /// </summary>
        public void Disconnect()
        {
            _bluetoothGatt?.Disconnect();
            _bluetoothGatt?.Close();
            _bluetoothGatt = null;
        }

        /// <summary>
        /// Custom GATT callback to handle Bluetooth events.
        /// </summary>
        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())}");
            }
        }
    }
}
