#include "BlynkCloudClient.h"
#include <QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>

CBlynkCloudClient::CBlynkCloudClient()
{
    mBlynkSocket = new QNetworkAccessManager;
    connect(mBlynkSocket,SIGNAL(finished(QNetworkReply*)),this,SLOT(BlynkServerFinished(QNetworkReply*)));
    connect(mBlynkSocket,SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),this,SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));

    mDummy = 0;
}

void CBlynkCloudClient::TestBlynk()
{
    //QString URL = QString("http://blynk-cloud.com/NzI2kH0L0sLOO8NWyPyoZPS7IFJ3Gtho/update/V0?value=%1").arg(mDummy++);
    QString URL = QString("http://blynk-cloud.com/NzI2kH0L0sLOO8NWyPyoZPS7IFJ3Gtho/get/V10");
    mBlynkSocket->get(QNetworkRequest(URL));

}

void CBlynkCloudClient::UpdateChaletCurrent(int Current)
{
    QString URL = QString("http://blynk-cloud.com/NzI2kH0L0sLOO8NWyPyoZPS7IFJ3Gtho/update/V1?value=%1").arg(Current);

    mBlynkSocket->get(QNetworkRequest(URL));

}

void CBlynkCloudClient::UpdateChaletVoltage(float Voltage)
{
    QString URL = QString("http://blynk-cloud.com/NzI2kH0L0sLOO8NWyPyoZPS7IFJ3Gtho/update/V0?value=%1").arg(Voltage);

    mBlynkSocket->get(QNetworkRequest(URL));

}

void CBlynkCloudClient::UpdateChaletWifiStatus(int Status)
{
    QString URL = QString("http://blynk-cloud.com/NzI2kH0L0sLOO8NWyPyoZPS7IFJ3Gtho/update/V10?value=%1").arg(Status);

    mBlynkSocket->get(QNetworkRequest(URL));

}

void CBlynkCloudClient::UpdateChaletInverterStatus(int Status)
{
    QString URL = QString("http://blynk-cloud.com/NzI2kH0L0sLOO8NWyPyoZPS7IFJ3Gtho/update/V10?value=%1").arg(Status);

    mBlynkSocket->get(QNetworkRequest(URL));

}

void CBlynkCloudClient::UpdateChaletStatusLed(int Status)
{
    QString URL = QString("http://blynk-cloud.com/NzI2kH0L0sLOO8NWyPyoZPS7IFJ3Gtho/update/V12?value=%1").arg(Status);

    mBlynkSocket->get(QNetworkRequest(URL));
}

int CBlynkCloudClient::GetChaletWifiToggleSwitchStatus()
{
//    http://blynk-cloud.com/NzI2kH0L0sLOO8NWyPyoZPS7IFJ3Gtho/get/V10
    QString URL = QString("http://blynk-cloud.com/NzI2kH0L0sLOO8NWyPyoZPS7IFJ3Gtho/get/V10");

    mBlynkSocket->get(QNetworkRequest(URL));
}

void CBlynkCloudClient::BlynkServerFinished(QNetworkReply *NetworkReply)
{
    if(NetworkReply->isFinished() == false)
        return;

    if(NetworkReply->error() != QNetworkReply::NoError)
    {
    //    qDebug("Network error... %d", NetworkReply->error());
        return;
    }


    QByteArray Reply = NetworkReply->readAll();
    if(Reply.isEmpty())
        return;

    NetworkReply->deleteLater();

    QString ReplyString(Reply);
    ReplyString.remove("[\"");
    ReplyString.remove("\"]");
    QJsonValue Result(ReplyString);
//    QJsonParseError error;
//    QJsonDocument JsonReply = QJsonDocument::fromJson(Reply,&error);
//    QJsonObject JsonObject = JsonReply.object();
//    QStringList Keys = JsonObject.keys();

    int toto = ReplyString.toInt();
    qDebug("Switch = %d",toto);



}

void CBlynkCloudClient::sslErrors(QNetworkReply *reply, QList<QSslError> error)
{

    qDebug("Ssl errors...");
    reply->ignoreSslErrors();
}
