#include "tag.h"


Tag::Tag(QObject *parent) : QObject(parent)
{
    // obtain the location data source
    locationInfo = QGeoPositionInfoSource::createDefaultSource(this);

    // select positioning method
    locationInfo->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods);

    // when the position has changed the setGPSLocation slot is called
    connect(locationInfo, SIGNAL(positionUpdated(QGeoPositionInfo)),this, SLOT(setGPSCoordinates(QGeoPositionInfo)));

    // start listening for position updates
    //locationInfo->startUpdates();

    emit tagChanged();
    //bluetooth

    // Create a discovery agent and connect to its signals
    //QBluetoothDeviceDiscoveryAgent *discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
                this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));

    // Start a discovery
    discoveryAgent->setLowEnergyDiscoveryTimeout(0);
    discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}

void Tag::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
    qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
    if(device.address().toString() == itsName)//"C7:53:A8:74:75:AE"
    {
        updateGPSCoordinates();
        discoveryAgent->stop();
        disconnect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
                    this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
        delete discoveryAgent;
        discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
        connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
                    this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));

        // Start a discovery
        discoveryAgent->setLowEnergyDiscoveryTimeout(0);
        discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);

    }
}

void Tag::setName(QString newName)
{
    itsName = newName;
}

void Tag::setGPSCoordinates(QGeoPositionInfo geoPositionInfo)
{
    if (geoPositionInfo.isValid())
    {
        // get the current location coordinates
        QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();

        // transform coordinates to lat/lon
        setCoordinates(geoCoordinate.latitude(),geoCoordinate.longitude());
        qDebug() << "SLOT stopping updates"+QString::number(geoCoordinate.latitude()) + "  "+ QString::number(geoCoordinate.longitude());
        locationInfo->stopUpdates();
    }

}

void Tag::updateGPSCoordinates()
{
    locationInfo->startUpdates();
    qDebug() << "requestUpdate : ";

}

double Tag::getLatitude()
{
    return itsLatitude;

}

double Tag::getLongtitude()
{
    return itsLongtitude;

}

int Tag::getTagNum()
{
    return itsTagNum;
}

void Tag::setTag(int TagNum)
{
    if(itsTagNum != TagNum)
    {
        itsTagNum = TagNum;
        emit tagChanged();
        qDebug() << "Set Tag : " << QString::number(itsTagNum) ;
    }
}

QString Tag::getStrDate()
{
    QString sDate = itsDate.toString("dd.MM.yyyy  hh:mm:ss");
    return sDate;
}

void Tag::setCoordinates(double newLatitude, double newLongtitude)
{
    if(itsLatitude != newLatitude || itsLongtitude != newLongtitude)
    {
        emit coordinatesChanged();
        itsDate = QDateTime::currentDateTime();
    }
    itsLatitude = newLatitude;
    itsLongtitude = newLongtitude;
    qDebug() << "Set Coordinates : saving new coordinates" << QString::number(itsLatitude) << QString::number(itsLongtitude);
}




