package com.google.heartrate.wearos.app.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGattServer;
import android.bluetooth.BluetoothGattServerCallback;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.content.Context;
import android.content.pm.PackageManager;

import com.google.heartrate.wearos.app.gatt.GattException;

import static android.content.Context.BLUETOOTH_SERVICE;

/**
 * Provides bluetooth related objects and checks.
 */
public class BluetoothUtils {
    private static final String TAG = BluetoothUtils.class.getSimpleName();

    /**
     * Check if bluetooth is supported on current device.
     * @param context context of application
     */
    public static void assertBluetoothIsSupported(Context context) throws GattException {
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(BLUETOOTH_SERVICE);
        if (bluetoothManager == null) {
            throw new GattException("Bluetooth is not supported. No BluetoothManager found.");
        }

        BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
        if (bluetoothAdapter == null) {
            throw new GattException("Bluetooth is not supported. No BluetoothAdapter found.");
        }

        if (!bluetoothAdapter.isEnabled()) {
            throw new GattException("Bluetooth adapter is currently disabled");
        }

        if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            throw new GattException("Bluetooth LE  is not supported. No FEATURE_BLUETOOTH_LE found.");
        }
    }

    /**
     * Get bluetooth manager.
     * @param context context of application
     * @return bluetooth manager
     */
    public static BluetoothManager getBluetoothManager(Context context) throws GattException {
        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        if (bluetoothManager == null) {
            throw new GattException("Bluetooth is not supported. No BluetoothManager found.");
        }
        return bluetoothManager;
    }

    /**
     * Get bluetooth adapter.
     * @param context context of application
     * @return bluetooth adapter
     */
    public static BluetoothAdapter getBluetoothAdapter(Context context) throws GattException {
        BluetoothManager bluetoothManager = getBluetoothManager(context);
        BluetoothAdapter adapter = bluetoothManager.getAdapter();
        if (adapter == null) {
            throw new GattException("Bluetooth is not supported. No BluetoothAdapter found.");
        }

        return adapter;
    }

    /**
     * Get bluetooth adapter.
     * @param context context of application
     * @return bluetooth adapter
     */
    public static BluetoothLeAdvertiser getBluetoothLeAdvertiser(Context context) throws GattException {
        BluetoothAdapter adapter = getBluetoothAdapter(context);
        BluetoothLeAdvertiser advertiser = adapter.getBluetoothLeAdvertiser();
        if (advertiser == null) {
            throw new GattException("Bluetooth advertising is not supported. Can not get BluetoothLeAdvertiser.");
        }

        return advertiser;
    }

    public static BluetoothGattServer getBluetoothGattServer(Context context, BluetoothGattServerCallback callback) throws GattException {
        BluetoothGattServer server = getBluetoothManager(context)
                .openGattServer(context, callback);

        if (server == null) {
            throw new GattException("Unable to create GATT server");
        }

        return server;
    }
}

@@ -659,16 +659,19 @@
   * 
-Check if
+Assert that
  blu
@@ -690,17 +690,17 @@
 pported 
-o
+i
 n curren
@@ -709,16 +709,23 @@
 device.%0A
+     *%0A
      * @
@@ -734,35 +734,24 @@
 ram context 
-context of 
 application%0A
@@ -745,24 +745,91 @@
  application
+ context%0A     * @throws GattException if bluetooth is not supported
 %0A     */%0A   
@@ -1015,32 +1015,33 @@
 TOOTH_SERVICE);%0A
+%0A
         if (blue
@@ -1237,32 +1237,33 @@
 r.getAdapter();%0A
+%0A
         if (blue
@@ -1513,16 +1513,17 @@
 disabled
+.
 %22);%0A    
@@ -1781,24 +1781,31 @@
 th manager.%0A
+     *%0A
      * @para
@@ -1810,35 +1810,24 @@
 ram context 
-context of 
 application%0A
@@ -1817,32 +1817,40 @@
 text application
+ context
 %0A     * @return 
@@ -1867,16 +1867,75 @@
 manager%0A
+     * @throws GattException if bluetooth is not supported%0A
      */%0A
@@ -2145,16 +2145,17 @@
 RVICE);%0A
+%0A
         
@@ -2287,24 +2287,25 @@
 ;%0A        %7D%0A
+%0A
         retu
@@ -2362,32 +2362,39 @@
 etooth adapter.%0A
+     *%0A
      * @param co
@@ -2395,35 +2395,24 @@
 ram context 
-context of 
 application%0A
@@ -2402,32 +2402,40 @@
 text application
+ context
 %0A     * @return 
@@ -2444,32 +2444,91 @@
 uetooth adapter%0A
+     * @throws GattException if bluetooth is not supported%0A
      */%0A    publ
@@ -2750,24 +2750,25 @@
 tAdapter();%0A
+%0A
         if (
@@ -2950,30 +2950,40 @@
 bluetooth ad
-apter.
+vertiser.%0A     *
 %0A     * @par
@@ -2997,19 +2997,8 @@
 ext 
-context of 
 appl
@@ -3004,16 +3004,24 @@
 lication
+ context
 %0A     * 
@@ -3036,29 +3036,103 @@
 bluetooth ad
-apter
+vertiser%0A     * @throws GattException if bluetooth advertising is not supported
 %0A     */%0A   
@@ -3377,16 +3377,17 @@
 iser();%0A
+%0A
         
@@ -3568,24 +3568,260 @@
 ser;%0A    %7D%0A%0A
+    /**%0A     * Get bluetooth gatt server.%0A     *%0A     * @param context application context%0A     * @param callback server callback%0A     * @return bluetooth gatt server%0A     * @throws GattException if unable to create gatt server%0A     */%0A
     public s
@@ -3968,23 +3968,30 @@
 ooth
-GattServer serv
+Manager bluetoothManag
 er =
@@ -4019,16 +4019,17 @@
 context)
+;
 %0A       
@@ -4029,24 +4029,61 @@
         
-        
+BluetoothGattServer server = bluetoothManager
 .openGat
@@ -4206,16 +4206,17 @@
 T server
+.
 %22);%0A    

Fix comments