#include "IspindelGUI.h"
#include "ui_IspindelGUI.h"
#include "Ispindel.h"
#include "IspindelData.h"
#include "GlobalDefine.h"

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


    mIspindelPlot = new QCustomPlot(ui->mIspindelPlot);
    mIspindelPlot->resize(ui->mIspindelPlot->size());

    // create graph and assign data to it:
    mIspindelPlot->addGraph();
    mIspindelPlot->addGraph(mIspindelPlot->xAxis,mIspindelPlot->yAxis2);


    // give the axes some labels:
    mIspindelPlot->xAxis->setLabel("Time");
    mIspindelPlot->yAxis->setLabel("Gravity");
    mIspindelPlot->yAxis2->setLabel("Temprature (C)");
    mIspindelPlot->yAxis2->setVisible(true);

    double now = QDateTime::currentDateTime().toSecsSinceEpoch();
    QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
    dateTicker->setDateTimeFormat("hh:mm:ss\ndd MMM");
    mIspindelPlot->xAxis->setTicker(dateTicker);
    mIspindelPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    QList<QCPAxis*> xAxis, yAxis;
    xAxis.append(mIspindelPlot->xAxis);
    yAxis.append(mIspindelPlot->yAxis);
    yAxis.append(mIspindelPlot->yAxis2);
    mIspindelPlot->axisRect()->setRangeDragAxes(xAxis,yAxis);
    mIspindelPlot->axisRect()->setRangeZoomAxes(xAxis,yAxis);
//    mIspindelPlot->yAxis2->axisRect()->setRangeZoomAxes(0,mIspindelPlot->yAxis2);


    QDateTime Now = QDateTime::currentDateTime().toLocalTime();

    QDateTime midnight = Now;
    midnight.setTime(QTime(0,0,0));
    QDateTime eod = Now;
    eod.setTime(QTime(23,59,0));

    //mIspindelPlot->xAxis->setRange(0/*QCPAxisTickerDateTime::dateTimeToKey(midnight)*/,QCPAxisTickerDateTime::dateTimeToKey(eod));
//    mIspindelPlot->xAxis->setRange(now, now+(2*3600));
    mIspindelPlot->xAxis->setRange(midnight.toSecsSinceEpoch(), eod.toSecsSinceEpoch());

    mIspindelPlot->yAxis->setRange(1.000,1.01);
    mIspindelPlot->yAxis2->setRange(15,25);

//    mIspindelPlot->graph(0)->addData(now,1.005);
//    mIspindelPlot->graph(1)->addData(now,20);

    mIspindelPlot->replot();

    ui->mSamplesTable->setColumnCount(4);
    ui->mSamplesTable->setHorizontalHeaderLabels(QStringList() << "Sample" << "Date" << "Gravity" << "Temperature");

    connect(ui->mSetOGBtn,SIGNAL(clicked(bool)),this,SLOT(SetOGButtonClicked(bool)));
    connect(ui->mDelSelectedSampleBtn,SIGNAL(clicked(bool)),this,SLOT(DeleteSampleBtnClicked(bool)));

}

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

void CIspindelGUI::SetLastIspindelFrameData(QString Data, QString ABVText)
{
    ui->mLastFrameDataLbl->setText(Data);
    ui->mABVLabel->setText(ABVText);
}

int CIspindelGUI::UpdateIspindelPlot(QList<CIspindelData *> *Data)
{
    if(Data->size() == 0)
        return RET_ERROR;

    QVector<double> x,y,ty;
    for(int i = 0; i < Data->size(); i++)
    {
        x.append(Data->at(i)->mSampleDateTime.toSecsSinceEpoch());
        y.append(Data->at(i)->mGravity);
        ty.append(Data->at(i)->mTemperature);
    }

    if(x.size() == 0 || y.size() == 0)
        return RET_ERROR;

    mIspindelPlot->graph(0)->data().clear();
    mIspindelPlot->graph(0)->setData(x,y);
    mIspindelPlot->xAxis->setRange(x.first(),x.last());
    mIspindelPlot->yAxis->setRange(y.first(),y.last());


    mIspindelPlot->graph(1)->setPen(QColor(Qt::red));
    mIspindelPlot->graph(1)->setName("Température");
    mIspindelPlot->graph(1)->setData(x,ty);
    mIspindelPlot->yAxis2->setRange(10,30);

    mIspindelPlot->replot();

    ui->mSamplesTable->setRowCount(Data->size());
    QTableWidgetItem *TableItem;
    for(int i= 0; i < Data->size(); i++)
    {
        TableItem = new QTableWidgetItem(QString("%1").arg(i));
        ui->mSamplesTable->setItem(i,0,TableItem);
        TableItem = new QTableWidgetItem(Data->at(i)->mSampleDateTime.toString("yyyy-MM-dd - hh:mm:ss"));
        ui->mSamplesTable->setItem(i,1,TableItem);
        TableItem = new QTableWidgetItem(QString("%1").arg(Data->at(i)->mGravity));
        ui->mSamplesTable->setItem(i,2,TableItem);
        TableItem = new QTableWidgetItem(QString("%1").arg(Data->at(i)->mTemperature));
        ui->mSamplesTable->setItem(i,3,TableItem);
    }
    ui->mSamplesTable->resizeColumnsToContents();

    return RET_OK;
}

int CIspindelGUI::NewIspindelFrameReceived(CIspindelData *Data)
{
    mIspindelPlot->graph(0)->addData(Data->mSampleDateTime.toSecsSinceEpoch(),Data->mGravity);
    mIspindelPlot->graph(1)->addData(Data->mSampleDateTime.toSecsSinceEpoch(),Data->mTemperature);

    QTableWidgetItem *TableItem;
    int RowIndex = ui->mSamplesTable->rowCount();
    ui->mSamplesTable->insertRow(RowIndex);
    TableItem = new QTableWidgetItem(QString("%1").arg(RowIndex));
    ui->mSamplesTable->setItem(RowIndex,0,TableItem);
    TableItem = new QTableWidgetItem(Data->mSampleDateTime.toString("yyyy-MM-dd - hh:mm:ss"));
    ui->mSamplesTable->setItem(RowIndex,1,TableItem);
    TableItem = new QTableWidgetItem(QString("%1").arg(Data->mGravity));
    ui->mSamplesTable->setItem(RowIndex,2,TableItem);
    TableItem = new QTableWidgetItem(QString("%1").arg(Data->mTemperature));
    ui->mSamplesTable->setItem(RowIndex,3,TableItem);



    mIspindelPlot->replot();

    return RET_OK;
}

void CIspindelGUI::SetOGButtonClicked(bool)
{
    QList<QTableWidgetItem*> SelectedItemsList;
    SelectedItemsList = ui->mSamplesTable->selectedItems();
    if(SelectedItemsList.size() == 0)
        return;
    int SampleIndex = SelectedItemsList.at(0)->row();

    mProgramHandle->SetOGFromItem(SampleIndex);


}

void CIspindelGUI::DeleteSampleBtnClicked(bool)
{
    QList<QTableWidgetItem*> SelectedItemsList;
    SelectedItemsList = ui->mSamplesTable->selectedItems();
    if(SelectedItemsList.size() == 0)
        return;
    int SampleIndex = SelectedItemsList.at(0)->row();

    mProgramHandle->DeleteSample(SampleIndex);


}

