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

import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.content.Context;
import android.os.ParcelUuid;
import android.util.Log;

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

import java.util.Set;
import java.util.UUID;

/**
 * {@link BluetoothAdvertiser} is the wrapper over {@link BluetoothLeAdvertiser} class.
 * <p>
 * Advertising allows devices to broadcast information defining their intentions.
 * Peripheral uses advertising to allow centrals to find it and, subsequently, to establish a connection with it.
 * <p>
 * A peripheral can use {@link BluetoothAdvertiser} to advertise its existence and show it's complete or partial
 * list of GATT services it offers.
 *
 */
public class BluetoothAdvertiser {
    private static final String TAG = BluetoothAdvertiser.class.getSimpleName();

    /** {@link BluetoothAdvertiser} for advertising process. */
    private final BluetoothLeAdvertiser bluetoothLeAdvertiser;

    /** Advertising callback to receive async responses from {@link BluetoothAdvertiser} . */
    final AdvertiseCallback advertiseCallback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            Log.i(TAG, "BluetoothLE Advertise Started.");
        }

        @Override
        public void onStartFailure(int errorCode) {
            Log.e(TAG, String.format("BluetoothLE Advertise Failed: %d",  + errorCode));
        }
    };

    public BluetoothAdvertiser(Context context) throws GattException {
        bluetoothLeAdvertiser = BluetoothUtils.getBluetoothLeAdvertiser(context);
    }

    /**
     * Start advertising centrals about peripheral existence.
     *
     * @param serviceUuids list of GATT services peripheral offers
     */
    public void start(Set<UUID> serviceUuids) {
        Log.d(TAG, "Starting advertising");

        AdvertiseSettings.Builder advertiseSettings = new AdvertiseSettings.Builder()
                /* Balanced between advertising frequency and power consumption */
                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED)
                .setTimeout(0)
                /* Medium transmission (TX) power level */
                .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM);

        AdvertiseData.Builder advertiseData = new AdvertiseData.Builder()
                .setIncludeDeviceName(true)
                .setIncludeTxPowerLevel(false);

        for (UUID serviceUuid : serviceUuids) {
            advertiseData.addServiceUuid(new ParcelUuid(serviceUuid));
        }

        bluetoothLeAdvertiser.startAdvertising(
                advertiseSettings.build(),
                advertiseData.build(),
                advertiseCallback);
    }

    /**
     * Stop advertising centrals about peripheral existence.
     */
    public void stop() {
        Log.d(TAG, "Stopping advertising");

        bluetoothLeAdvertiser.stopAdvertising(advertiseCallback);
    }
}

@@ -687,18 +687,14 @@
 %0A * 
-Peripheral
+Server
  use
@@ -717,22 +717,21 @@
  allow c
+li
 ent
-ral
 s to fin
@@ -801,26 +801,22 @@
 p%3E%0A * A 
-peripheral
+server
  can use
@@ -1887,39 +1887,41 @@
 advertising 
-central
+to client
 s about 
 peripheral e
@@ -1904,34 +1904,30 @@
 ients about 
-peripheral
+server
  existence.%0A
@@ -1982,26 +1982,22 @@
 ervices 
-peripheral
+server
  offers%0A
@@ -2025,16 +2025,35 @@
 id start
+AdvertisingServices
 (Set%3CUUI
@@ -3038,23 +3038,25 @@
 ing 
-central
+to client
 s about 
 peri
@@ -3055,18 +3055,14 @@
 out 
-peripheral
+server
  exi
@@ -3095,18 +3095,38 @@
  void st
-op
+artAdvertisingServices
 () %7B%0A   

Fix start/stop method names and comment.