#include "xlrureportclass.h"


#include <QDebug>
#include <QFile>

XlruReportClass::XlruReportClass():
    file(0)
{
    file_path = "";
    file_name = "";
    header_saved = false;
}

XlruReportClass::~XlruReportClass()
{
    closeFile();
}

QString XlruReportClass::newNameFile(QString _header)
{
    QDateTime dateTime;
    dateTime = QDateTime::currentDateTime();
    return QString("%1_%2_%3").arg(dateTime.toString("yyyyMMddhhmmss")).arg(_header).arg("xlru_monitor.csv");
}

QString XlruReportClass::generateNewFile(QString _header)
{
    file_name = newNameFile(_header);
    //file_path = QString("%1//%2").arg(QFileInfo(".").absolutePath()).arg(file_name);
    file_path = QString("./%1").arg(file_name);

    if (saveString(file_name))
    {
        closeFile();
        return file_path;
    }
    else
        return "";
}

bool XlruReportClass::saveString(QString _value)
{  
    if (!file.isOpen())
    {
        file.setFileName(file_path);
        if ( !file.open(QFile::WriteOnly | QFile::Text | QIODevice::Append) )
        {
            qDebug() << "File not exists";
            return false;
        }
    }

    // Create a thread to retrieve data from a file
    QTextStream out(&file);
    out << _value << "\n";
    //file.close();
    return true;
}

bool XlruReportClass::addSensorCol(QString _col)
{
    int idx = findSensor(_col);
    if (idx == -1)
    {
        sensor_report newSensor;
        newSensor.name = _col;
        newSensor.present = true;
        newSensor.value = 0.0;
        list_sensors.append(newSensor);
        return true;
    }
    else
    {
        return false;
    }
}

//add value for sensor
bool XlruReportClass::addValueCol(QString _col, double value)
{
    int idx = findSensor(_col);
    if (idx != -1)
    {
        list_sensors[idx].value = value;
        list_sensors[idx].present = true;
        return true;
    }
    else
    {
        return false;
    }
}

bool XlruReportClass::saveValue()
{
    bool save = false;
    if ((!file.isOpen()) && (!header_saved))
        saveHeader();

    QString string_to_save = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");

    for (int i = 0; i< list_sensors.count(); i++)
    {
        if (list_sensors[i].present)
        {
            save = true;
            string_to_save = string_to_save + ";" + QString::number(list_sensors[i].value, 'f', 2);
        }
    }

    if (save)
    {
        string_to_save = string_to_save+";";

        //reset value
        foreach (sensor_report se, list_sensors)
        {
            se.value = 0.0;
        }

        return saveString(string_to_save);
    }
    return -1;
}

bool XlruReportClass::saveHeader()
{
    bool save = false;

    QString string_to_save = "DateTime";

    for (int i = 0; i< list_sensors.count(); i++)
    {
        if (list_sensors[i].present)
        {
            save = true;
            string_to_save = string_to_save + ";" + list_sensors[i].name;
        }
    }
    if (save)
    {
        string_to_save = string_to_save+";";

        header_saved = true;

        return saveString(string_to_save);
    }
    return -1;
}

bool XlruReportClass::closeFile()
{
    if (file.isOpen())
    {
        file.close();
        return true;
    }
    return false;
}

int XlruReportClass::findSensor(QString _col)
{
    for (int i = 0; i<list_sensors.count(); i++)
    {
        if (list_sensors[i].name==_col)
            return i;
    }
    return -1;
}



