#include "anchor.h"


Anchor::Anchor(QObject *parent) : QObject(parent)
//Anchor::Anchor(QObject *parent) : Client(parent)
{
    socket = new QTcpSocket(this);

    timer = new QTimer;
    timer2 = new QTimer;
    timer->start(120000);   //reconnect every 2 minutes

    connect(timer, SIGNAL(timeout()), SLOT(connectToExist()));
    connect(socket, SIGNAL(readyRead()), SLOT(readTcpData()) );

    socket->connectToHost(itsHost, itsPort);
    if(socket->waitForConnected(500))
    {
        qDebug("Connected!");
        setConnectionStatus(1);
    }
    else
    {
        qDebug("not connected");
        timer2->start(300000);  //try to reconnect every 5 minutes if connection unpossible
        setConnectionStatus(0);
        connect(timer2, SIGNAL(timeout()), SLOT(reconnect()));
    }
    //writeTcpData("G01");
    /* test connection greeting
    QString d = "ahoj";
    socket->write(d.toUtf8());
    socket->flush();
    */

}

void Anchor::test()
{
    qDebug() << "test()..onActiveFocusChanged : " << QString::number(itsRssi) ;

}

qint64 Anchor::getMsec()
{
    QDate d1(2017, 4, 14);
    qint64 seconds = itsDate.daysTo(d1) * 24*60;//secodns from 14/4/2017
    seconds += itsTime.msecsSinceStartOfDay() / 1000;//seconds from date and time
    return seconds;
}


QString Anchor::getStrRSSI()
{
    QString sNumber = QString::number(itsRssi);
    //qDebug() << "getString() : " << QString::number(itsRssi) ;
    sNumber = "- " + sNumber;
    return sNumber;

}

QString Anchor::getStrDate()
{
    QString sDate = itsDate.toString("dd.MM.yyyy");
    QString sTime = itsTime.toString("  hh:mm:ss");
    return (sDate + sTime);

}

int Anchor::getRssi()
{
    return itsRssi;
}

double Anchor::getX()
{
    return itsXPossition;
}

double Anchor::getY()
{
    return itsYPossition;
}

double Anchor::getRadius()
{
    return itsRadius;
}

bool Anchor::getConnectionStatus()
{
    return itsConnectionStatus;
}

void Anchor::setConnectionStatus(int status)
{
    if(status != 0)
        itsConnectionStatus = true;
    else
        itsConnectionStatus = false;
}

void Anchor::setRssi(int newRssi)       //setters+signals
{
    if(itsRssi != newRssi)
        emit rssiChanged();
    itsRssi = newRssi;
    itsRadius = ((itsRssi-58.255)/2.0925)/3; //rovnice pro spoÄŤĂ­tĂˇnĂ­ vzdĂˇlenosti na zĂˇkladÄ› RSSI..../3 index pro pouĹľitĂ­ vevnitĹ™
    //Radius nemÄ›nnĂ˝ protoĹľe urÄŤovĂˇnĂ­ vzdĂˇlenosti podle RSSI nefunguje
}

void Anchor::setRssi()
{
    emit rssiChanged();
}

void Anchor::setTrilat(int newTrilat)
{
    itsTrilat = newTrilat;
}

int Anchor::getTrilat(int number)
{
    if(number == 1)
        return ((itsTrilat/100) % 10);
    if(number == 2)
        return ((itsTrilat/10) % 10);
    if(number == 3)
        return (itsTrilat % 10);
}

void Anchor::setDateTime(QDate newDate,QTime newTime)
{
    if(itsTime != newTime)
        emit dateChanged();
    itsDate = newDate;
    itsTime = newTime;
}

void Anchor::setDate()
{
    emit dateChanged();
}

void Anchor::setX(double newXPossition)
{
    if(itsXPossition != newXPossition)
        emit XChanged(newXPossition);
    itsXPossition = newXPossition;
}

void Anchor::setY(double newYPossition)
{
    if(itsYPossition != newYPossition)
        emit YChanged(newYPossition);
    itsYPossition = newYPossition;
}

void Anchor::setXY(double newXPossition, double newYPossition)
{
    if(itsYPossition != newYPossition)
        emit YChanged(newYPossition);
    itsYPossition = newYPossition;

    if(itsXPossition != newXPossition)
        emit XChanged(newXPossition);
    itsXPossition = newXPossition;

}


void Anchor::writeTcpData(QString data)
{
    if(socket->state() == QAbstractSocket::ConnectedState)
    {
        //socket->write(IntToArray(data.size())); //write size of data
        socket->write(data.toUtf8()); //write the data itself
        socket->waitForBytesWritten();
    }
    else
        reconnect();



}

QByteArray Anchor::readTcpData()
{
    QByteArray data = socket->readAll();
    int rssi = (data[21]-'0')*10 + (data[22]-'0');
    setRssi(rssi);
    setTrilat((data[23]-'0')*100 + (data[24]-'0')*10 +(data[25]-'0'));

    QByteArray date = data.mid(2,10);
    QByteArray time = data.mid(13,8);

    QString sDate = QString::fromUtf8(date);
    QString sTime = QString::fromUtf8(time);
    QDate Date = QDate::fromString(sDate,"yyyy-MM-dd");
    QTime Time = QTime::fromString(sTime,"hh:mm:ss");

    setDateTime(Date,Time);

    return data;
}

void Anchor::connectToExist()
{
    socket->disconnectFromHost();
    socket->connectToHost(itsHost, itsPort);
}

void Anchor::reconnect()
{
    delete socket;
    socket = new QTcpSocket(this);
    socket->connectToHost(itsHost, itsPort);
    if(socket->waitForConnected(500))
    {
        qDebug("Connected!");
        setConnectionStatus(1);
    }
        else
    {
        qDebug("reconnect: not connected");
        timer2->stop();
        timer2->start(300000);
        setConnectionStatus(0);
    }

}
