#include "xlruchartconfig.h"
#include <QFileInfo>
#include <QApplication>

#define REPORT_ENABLED_DEF  true

// XlruChartConfig pointer used to ensure a single instance of the class.
XlruChartConfig* XlruChartConfig::s_instance = 0;

/* This function is called to create an instance of the class.
 *   Calling the constructor publicly is not allowed. The constructor
 *   is private and is only called by this Instance function.
 */
XlruChartConfig* XlruChartConfig::instance()
{
    // Only allow one instance of class to be generated.
    if (!s_instance)
        s_instance = new XlruChartConfig;
    return s_instance;
}

XlruChartConfig::XlruChartConfig()
{
    list_graph_config.clear();

    num_graphs = 0;

    valid_config = false;

    report_enabled = REPORT_ENABLED_DEF;
}


bool XlruChartConfig::getReportEnabled()
{
    return report_enabled;
}

void XlruChartConfig::setReportEnabled(bool _enabled)
{
    report_enabled = _enabled;
}

bool XlruChartConfig::loadSettings()
{
    bool result =false;

    //QString pathFileIni = QFileInfo(".").absolutePath() + "/" + fileCfg;
    QString pathFileIni = "./" + fileCfg;

    if (!QFile::exists(pathFileIni))
    {
        Q_INIT_RESOURCE(grifoxlrumonitor);

        QFile::copy(":/GrifoXLruMonitor.ini", pathFileIni);

        QFile(pathFileIni).setPermissions(QFile::ReadOther | QFile::WriteOther);

        //QFile(pathFileIni).remove();
    }

    QSettings settings( pathFileIni, QSettings::IniFormat );

    if ( settings.status() == QSettings::NoError )
    {
        settings.beginGroup("config");

        num_graphs = settings.value("num_graphs").toInt();
        report_enabled = settings.value("report").toBool();
        temp_default_limit = settings.value("temp_limit").toInt();
        curr_default_limit = settings.value("curr_limit").toInt();
        volt_default_limit = settings.value("volt_limit").toInt();
        remove_limit = settings.value("remove_limit").toBool();

        settings.endGroup();
        QString section = "";
        QString sensor_field = "";

        for (int i = 0; i<num_graphs; i++)
        {
            section = QString("graph%1").arg(i);
            settings.beginGroup(section);
            graph_config_t* new_graph = new graph_config_t();
            new_graph->name = settings.value("name", "").toString();
            new_graph->num_series = settings.value("num_sensors").toInt();
            list_graph_config.append(new_graph);
            for (int j = 0; j<new_graph->num_series; j++)
            {
                sensor_config_t* new_sensor = new sensor_config_t();
                sensor_field = QString("sensor%1_limit").arg(j);
                new_sensor->limit = settings.value(sensor_field).toInt();
                if ((new_sensor->limit==0) && (temp_default_limit!=0))
                    new_sensor->limit = temp_default_limit;

                sensor_field = QString("sensor%1_name").arg(j);
                new_sensor->name = settings.value(sensor_field).toString();
                sensor_field = QString("sensor%1_label").arg(j);

                QString tmp = settings.value(sensor_field).toString();
                if (tmp!="")
                    new_sensor->label_sensor = tmp;
                else
                    new_sensor->label_sensor = new_sensor->name;

                sensor_field = QString("sensor%1_type").arg(j);
                new_sensor->type = type_series_t(settings.value(sensor_field).toInt());
                sensor_field = QString("sensor%1_fpga_ch").arg(j);
                new_sensor->fpga_ch = settings.value(sensor_field).toInt();
                sensor_field = QString("sensor%1_fpga_address").arg(j);
                new_sensor->fpga_address= settings.value(sensor_field).toString();
                new_graph->list_sensors_config.append(new_sensor);
           }

           settings.endGroup();
      }

      settings.sync();

      if(num_graphs!=0)
          result = true;
   }

    //result = false;

    valid_config = result;

    return result;
}

/*bool XlruChartConfig::loadSettings()
{
    bool result =false;

    QString pathFileIni = QFileInfo(".").absolutePath() + "/" + fileCfg;

    QSettings settings( pathFileIni, QSettings::IniFormat );

    if (QFile::exists(pathFileIni))
    {

        if ( settings.status() == QSettings::NoError )
        {
            settings.beginGroup("config");

            num_graphs = settings.value("num_graphs").toInt();

            report_enabled = settings.value("report").toBool();

            temp_default_limit = settings.value("temp_limit").toInt();

            curr_default_limit = settings.value("curr_limit").toInt();

            volt_default_limit = settings.value("volt_limit").toInt();

            settings.endGroup();

            QString section = "";
            QString sensor_field = "";

            for (int i = 0; i<num_graphs; i++)
            {
                section = QString("graph%1").arg(i);
                settings.beginGroup(section);

                graph_config_t* new_graph = new graph_config_t();

                new_graph->name = settings.value("name", "").toString();
                new_graph->num_series = settings.value("num_sensors").toInt();

                list_graph_config.append(new_graph);

                for (int j = 0; j<new_graph->num_series; j++)
                {
                    sensor_config_t* new_sensor = new sensor_config_t();

                    sensor_field = QString("sensor%1_limit").arg(j);

                    new_sensor->limit = settings.value(sensor_field).toInt();
                    if ((new_sensor->limit==0) && (temp_default_limit!=0))
                        new_sensor->limit = temp_default_limit;

                    sensor_field = QString("sensor%1_name").arg(j);
                    new_sensor->name = settings.value(sensor_field).toString();

                    sensor_field = QString("sensor%1_sensor").arg(j);
                    new_sensor->sensor = settings.value(sensor_field).toString();

                    sensor_field = QString("sensor%1_type").arg(j);
                    new_sensor->type = settings.value(sensor_field).toString();

                    new_graph->list_sensors_config.append(new_sensor);
                }

                settings.endGroup();
            }

            settings.sync();

            if((settings.status() ==QSettings::NoError) && (num_graphs!=0))
                result = true;
        }
    }

    //result = false;

    valid_config = result;

    return result;
}*/

int XlruChartConfig::getVoltDefaultLimit()
{
    return volt_default_limit;
}

int XlruChartConfig::getTempDefaultLimit()
{
    return temp_default_limit;
}

int XlruChartConfig::getCurrDefaultLimit()
{
    return curr_default_limit;
}

//controlla se devo creare il grafico perchè ho almeno un sensore valido.
bool XlruChartConfig::checkFpga(int idx, QStringList list_fpga)
{
    graph_config_t *graph_config = list_graph_config[idx];

    for (int i= 0; i<graph_config->list_sensors_config.size(); i++)
    {
        //se esiste un sensor non di tipo fpga allora devo creare il grafico
        if (graph_config->list_sensors_config[i]->fpga_address=="")
            return true;
        else
            //se esiste nella lista delle fpga quella indicata nel sensore.
            if (list_fpga.indexOf(graph_config->list_sensors_config[i]->fpga_address)!=-1)
                return true;
    }

    return false;
}

bool XlruChartConfig::getRemoveLimit()
{
    return remove_limit;
}

void XlruChartConfig::setRemoveLimit(bool _enabled)
{
    remove_limit = _enabled;
}
