#include "historydbform.h"
#include "ui_historydbform.h"

#include <QStandardItemModel>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>

#define COL_timestamp           0
#define COL_filename            1
#define COL_where               2
#define COL_srio_conn           3
#define COL_flash_address       4
#define COL_spi_port            5
#define COL_status              6
#define COL_error               7
#define COL_error_description   8

HistoryDBForm::HistoryDBForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::HistoryDBForm)
{
    ui->setupUi(this);

    connect(ui->buttonBox->button(QDialogButtonBox::Reset), SIGNAL(clicked()), SLOT(on_reset_clicked()));
}

HistoryDBForm::~HistoryDBForm()
{
    delete ui;
}

void HistoryDBForm::setDB(dbHistory *_db)
{
    HistoryDB = _db;

    showInfoDB();

    setWindowTitle(QString("History - file: %1").arg(HistoryDB->getFileDB()));
}

void HistoryDBForm::showInfoDB()
{
    int countItem = HistoryDB->getNumEntry();

    QStandardItemModel *model = new QStandardItemModel(countItem,9,this);

    model->setHorizontalHeaderItem(COL_timestamp,           new QStandardItem(QString("timestamp")));
    model->setHorizontalHeaderItem(COL_filename,            new QStandardItem(QString("filename")));
    model->setHorizontalHeaderItem(COL_where,               new QStandardItem(QString("where")));
    model->setHorizontalHeaderItem(COL_srio_conn,           new QStandardItem(QString("srio_conn")));
    model->setHorizontalHeaderItem(COL_flash_address,       new QStandardItem(QString("flash_address")));
    model->setHorizontalHeaderItem(COL_spi_port,            new QStandardItem(QString("spi_port")));
    model->setHorizontalHeaderItem(COL_status,              new QStandardItem(QString("status")));
    model->setHorizontalHeaderItem(COL_error,               new QStandardItem(QString("error")));
    model->setHorizontalHeaderItem(COL_error_description,   new QStandardItem(QString("error_description")));

    ui->twHistory->setModel(model);

    db_item_t itemDB;

    for (int i = 0; i<countItem; i++)
    {
        //extract for every ini files the description about testcase
        itemDB = HistoryDB->getEntry(i);

        QStandardItem *itemTimeStamp = new QStandardItem(itemDB.timestamp);
        model->setItem(i,COL_timestamp,itemTimeStamp);

        QStandardItem *itemName = new QStandardItem(itemDB.name);
        model->setItem(i,COL_filename,itemName);

        QStandardItem *itemWhere = new QStandardItem(itemDB.where);
        model->setItem(i,COL_where,itemWhere);

        QStandardItem *itemSRIO = new QStandardItem(itemDB.srio_conn);
        model->setItem(i,COL_srio_conn,itemSRIO);

        QStandardItem *itemFlash = new QStandardItem(itemDB.flash_address);
        model->setItem(i,COL_flash_address,itemFlash);

        QString spi_port_str = HistoryDB->getSPIString(itemDB.spi);
        QStandardItem *itemSPI = new QStandardItem(spi_port_str);
        model->setItem(i,COL_spi_port,itemSPI);

        QString status_str = HistoryDB->getStatusString(itemDB.status);
        QStandardItem *itemStatus = new QStandardItem(status_str);
        model->setItem(i,COL_status,itemStatus);

        QString error_str = HistoryDB->getErrorString(itemDB.error);
        QStandardItem *itemError = new QStandardItem(error_str);
        model->setItem(i,COL_error,itemError);

        QStandardItem *error_desc = new QStandardItem(itemDB.errorStr);
        model->setItem(i,COL_error_description,error_desc);

        if (itemDB.error!=db_error_no_error)
        {
            //se c'è un errore
            itemError->setBackground(Qt::red);
            error_desc->setBackground(Qt::red);
            itemStatus->setBackground(Qt::red);
            itemError->setForeground(Qt::white);
            error_desc->setForeground(Qt::white);
            itemStatus->setForeground(Qt::white);
        }
        else
        if (itemDB.status==db_status_programmed)
        {
            //se è completo senza problemi
            itemError->setBackground(Qt::green);
            error_desc->setBackground(Qt::green);
            itemStatus->setBackground(Qt::green);
            itemError->setForeground(Qt::white);
            error_desc->setForeground(Qt::white);
            itemStatus->setForeground(Qt::white);
        }
        else
        if (itemDB.status==db_status_started)
        {
            //se è completo senza problemi
            itemError->setBackground(Qt::lightGray);
            error_desc->setBackground(Qt::lightGray);
            itemStatus->setBackground(Qt::lightGray);
            itemError->setForeground(Qt::black);
            error_desc->setForeground(Qt::black);
            itemStatus->setForeground(Qt::black);
        }
    }

    ui->twHistory->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

    ui->twHistory->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);

    ui->twHistory->setSelectionBehavior(QAbstractItemView::SelectRows);

    ui->twHistory->show();
}

void HistoryDBForm::on_reset_clicked()
{
    if (QMessageBox::Yes == QMessageBox(QMessageBox::Question, "Request confirm", "Reset history?", QMessageBox::Yes|QMessageBox::No).exec())
    {
        HistoryDB->reset();
        close();
    }
}

