using Android.Bluetooth.LE;

namespace InABox.Avalonia.Platform.Android;

public class BluetoothScanManager : ScanCallback
{
    private readonly Action<ScanResult> _onDeviceFound;
    private readonly Action _onScanStopped;

    public BluetoothScanManager(Action<ScanResult> onDeviceFound, Action onScanStopped)
    {
        _onDeviceFound = onDeviceFound;
        _onScanStopped = onScanStopped;
    }

    public override void OnScanResult(ScanCallbackType callbackType, ScanResult result)
    {
        base.OnScanResult(callbackType, result);
        _onDeviceFound?.Invoke(result);
    }

    public override void OnScanFailed(ScanFailure errorCode)
    {
        base.OnScanFailed(errorCode);
        _onScanStopped?.Invoke();
        throw new Exception($"Scan failed with error code: {errorCode}");
    }
}