#include <exception>
#include <iostream>
#include <vector>
#include <boost/uuid/uuid_io.hpp>
#include "ble/Bluetooth.h"

using namespace std;
using namespace trygvis::bluetooth;

void scan_callback(BluetoothDevice & device) {
    cout << "Inspecting device: " << device.getMac().str() << endl;

    auto &gatt = device.connectGatt();

    gatt.discoverServices();

    vector < BluetoothGattService * > services = gatt.getServices();
    cout << "Device has " << services.size() << " services" << endl;

    for (auto &s: services) {
        const vector<BluetoothGattCharacteristic *> characteristics = s->getCharacteristics();

        cout << "Service: UUID: " << s->getUuid() << ", has " << characteristics.size() << " characteristics" << endl;

        for (auto &c: characteristics) {
            cout << "Characteristic: UUID: " << c->getUuid() << ", properties: " << (int) c->getProperties() << endl;
        }
    }

    gatt.disconnect();
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        cerr << "usage: " << argv[0] << " [mac]" << endl;
        return EXIT_FAILURE;
    }

    BluetoothSystem bluetoothSystem;

    try {
        Mac mac = Mac::parseMac(argv[1]);

        BluetoothAdapter &adapter = getAdapter(0);

        BluetoothDevice &device = adapter.getDevice(mac);

        scan_callback(device);

        return EXIT_SUCCESS;
    } catch (std::runtime_error ex) {
        cout << "std::runtime_error: " << ex.what() << endl;
        return EXIT_FAILURE;
    } catch (std::exception ex) {
        cout << "std::exception: " << ex.what() << endl;
        return EXIT_FAILURE;
    }
}