// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Class is used to discover available devices and the Bluetooth services
#include "bluetoothdiscoverdevices.h"

BluetoothDiscoverDevices::BluetoothDiscoverDevices(QObject *parent) : QObject(parent)
{
    // Initializing the connected and selected device index
    this->connectedDeviceIndex = -1;
    this->selectedDeviceIndex = 0;

    // Making connections to monitor when the device is connected or disconnected
    localDevice = new QBluetoothLocalDevice();

    // Object to discover available bluetooth devices
    discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
    #if QT_VERSION >= QT_VERSION_CHECK(5,8,0)  // This was introduced only in Qt 5.8 and it has issues with Windows Phone 8.1 (problems with file downloads getting stuck).
        discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
    #endif
    connect(this->discoveryAgent, SIGNAL(finished()), this, SIGNAL(deviceDiscoveryFinished()));
}

// Method to get a reference to the object used to discover bluetooth devices
QBluetoothDeviceDiscoveryAgent *BluetoothDiscoverDevices::getBluetoothDeviceDiscoveryAgent() { return this->discoveryAgent; }

// Initializing device and connections
void BluetoothDiscoverDevices::discoverDevices(bool isLowEnergyMode)
{
    // Stopping the discovery agent if it is already running
    if (discoveryAgent->isActive()) { discoveryAgent->stop(); }

    // Starting the device discovery (need to wait for the descovery service to finish)
    if (isLowEnergyMode) {
        #if QT_VERSION >= QT_VERSION_CHECK(5,8,0)  // This was introduced only in Qt 5.8 and it has issues with Windows Phone 8.1 (problems with file downloads getting stuck).
            discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
        #else
            discoveryAgent->start();
        #endif
    }
    else { discoveryAgent->start(); }

    // Signal is emited when the device discovery is started
    emit deviceDiscoveryStarted();
}

// Method to get the connected device index
qint8 BluetoothDiscoverDevices::getConnectedDeviceIndex() { return this->connectedDeviceIndex; }

// Methods to get and set the selected device index
quint8 BluetoothDiscoverDevices::getSelectedDeviceIndex() { return this->selectedDeviceIndex; }
void BluetoothDiscoverDevices::setSelectedDeviceIndex(quint8 newSelectedDeviceIndex) { this->selectedDeviceIndex = newSelectedDeviceIndex; }

// Method to get selected device address
QBluetoothDeviceInfo BluetoothDiscoverDevices::getSelectedDevice() { return this->discoveryAgent->discoveredDevices().at(this->selectedDeviceIndex); }

// Slot is called when a device is connected
void BluetoothDiscoverDevices::setDeviceConnected()
{
    // Selected device has been connected
    this->connectedDeviceIndex = this->selectedDeviceIndex;
    emit connected();
}

// Slot is called when the device is disconnected
void BluetoothDiscoverDevices::setDeviceDisconnected()
{
    this->connectedDeviceIndex = -1;
    emit disconnected();
}
