#include "xlrusensorstable.h"

#define tab_col_00_width 90
#define tab_col_01_width 50
#define tab_col_02_width 50
#define tab_col_03_width 50
#define tab_col_04_width 50
#define tab_row_height   20

XlruSensorsTableClass::XlruSensorsTableClass(QTableWidget *_tab_sensor)
{
    tab_sensor = _tab_sensor;

    tab_sensor->setColumnCount(4);
    tab_sensor->setRowCount(0);
    tab_sensor->setColumnWidth(0, tab_col_00_width);
    tab_sensor->setColumnWidth(1, tab_col_01_width);
    tab_sensor->setColumnWidth(2, tab_col_02_width);
    tab_sensor->setColumnWidth(3, tab_col_03_width);
    //tab_sensor->setRowHeight(0,tab_row_height);
    //tab_sensor->setRowHeight(1,tab_row_height);
    //tab_sensor->setRowHeight(2,tab_row_height);

    QStringList m_TableHeader;

    m_TableHeader<<"sensor"<<"min"<<"actual"<<"max";
    tab_sensor->setHorizontalHeaderLabels(m_TableHeader);
    tab_sensor->setEditTriggers(QAbstractItemView::NoEditTriggers);
    tab_sensor->setSelectionBehavior(QAbstractItemView::SelectRows);
    tab_sensor->setSelectionMode(QAbstractItemView::SingleSelection);
    tab_sensor->setShowGrid(true);
    tab_sensor->setStyleSheet("QTableView {selection-background-color: silver;}");
}

XlruSensorsTableClass::~XlruSensorsTableClass()
{

}

//Insert a value into correct sensor and update info into tableview
void XlruSensorsTableClass::update_sensor(QString name, float value)
{
    //found sensor row for table
    int idx = findSensorName(name);

    if (idx==-1)
        return;

    //update instant value for sensor on correct row
    sensor_table *se = list_sensors[idx];

    se->value = value;

    if ((se->max==0.0f) && (se->min==0.0f))
    {
        se->max = value;
        se->min = value;
        tab_sensor->setItem(idx, 3, new QTableWidgetItem(QString::number(value, 'f', 2)));
        tab_sensor->setItem(idx, 1, new QTableWidgetItem(QString::number(value, 'f', 2)));
    }


    tab_sensor->setItem(idx, 2, new QTableWidgetItem(QString::number(value, 'f', 2)));

    //check if max/min and update
    if (se->max<value)
    {
        se->max = value;
        tab_sensor->setItem(idx, 3, new QTableWidgetItem(QString::number(value, 'f', 2)));
    }

    if (se->min>value)
    {
        se->min = value;
        tab_sensor->setItem(idx, 1, new QTableWidgetItem(QString::number(value, 'f', 2)));
    }
}

//reset min/max with last instant value to re-check the intervall
void XlruSensorsTableClass::resetMinMax()
{
    int idx = -1;
    foreach (sensor_table *s, list_sensors)
    {
        s->max = 0.0f;
        s->min = 0.0f;
        idx++;

        tab_sensor->setItem(idx,1, new QTableWidgetItem(QString::number(0.0f, 'f', 2)));
        tab_sensor->setItem(idx,3, new QTableWidgetItem(QString::number(0.0f, 'f', 2)));
    }
}

//add label for sensor configured
void XlruSensorsTableClass::initializeSensor(QStringList SensorsName)
{
    if (SensorsName.count()<1)
        return;

    for (int i = 0; i<SensorsName.count(); i++)
    {
        tab_sensor->setItem(i,0, new QTableWidgetItem(SensorsName.value(i)));

        sensor_table *newSensor;
        newSensor->name = SensorsName.value(i);
        newSensor->max = 0.0f;
        newSensor->min = 0.0f;
        newSensor->value = 0.0f;
        list_sensors.append(newSensor);
    }
}

int XlruSensorsTableClass::findSensorName(QString _name)
{
    int idx = 0;
    foreach (sensor_table *s, list_sensors)
    {
        if (s->name == _name)
           return idx;

        idx++;
    }
    return -1;
}

void XlruSensorsTableClass::addNewSensor(QString name)
{
    int idx = findSensorName(name);
    if (idx!=-1)
    {
        //sensor just present
        return;
    }

    sensor_table *newSensor = new sensor_table();
    newSensor->name = name;
    newSensor->max = 0.0f;
    newSensor->min = 0.0f;
    newSensor->value = 0.0f;
    list_sensors.append(newSensor);

    int count = list_sensors.count();

    tab_sensor->setRowCount(count);

    tab_sensor->setItem(count-1,0, new QTableWidgetItem(newSensor->name));

}
