#include "HistorianPage.h"
#include "ui_HistorianPage.h"
#include "HistorienData.h"
#include "ModbusHistorienMaster.h"

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

    QTreeWidgetItem *Item = new QTreeWidgetItem(QStringList()<<"ZT1");
    ui->mHistorianDataTreeWidget->setColumnCount(3);
    ui->mHistorianDataTreeWidget->setHeaderItem(new QTreeWidgetItem(QStringList()<<"Registre"<<"Description"<<"Valeur"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2000/b0")<<"Détection FN en service"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2000/b1")<<"Détection PP  en service"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2000/b2")<<"Détection PG en service"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2000/b3")<<"Détection PP ZT2 en service"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2000/b4")<<"Mode entretien activé"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2000/b5")<<"Santé lien HPC"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2000/b6")<<"Santé lien SEI"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2000/b7")<<"ZT1 en service"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2000/b8")<<"ZT2 en service"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2001")<<"Calibration pneu de guidage"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2002")<<"Date démarrage  ZT année"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2003")<<"Date démarrage ZT mois"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2004")<<"Date démarrage ZT jour"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2005")<<"Heure démarrage ZT heure"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2006")<<"Heure démarrage ZT minutes"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2007")<<"Heure démarrage ZT secondes"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2008")<<"Nb passages total"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2009")<<"Nb déclenchements total"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2010")<<"Nb passages aujourd'hui"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2011")<<"Nb déclenchements aujourd'hui"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2012")<<"Nb décl. FN aujourd'hui"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2013")<<"Nb décl. PPI  aujourd'hui"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2014")<<"Nb décl. PPE  aujourd'hui"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2015")<<"Nb décl. PG ajourd'hui"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2016")<<"Nb décl. PPI ZT2 aujourd'hui"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2017")<<"Nb décl. PPE ZT2 aujourd'hui"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2018")<<"Date dernier passage année"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2019")<<"Date dernier passage mois"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2020")<<"Date dernier passage jour"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2021")<<"Heure dernier passage heure"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2022")<<"Heure dernier passage minutes"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2023")<<"Heure dernier passage secondes"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2024")<<"Zone franchie (ZT1 = 1, ZT2 = 2)"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2025")<<"Nb déclenchement lors du passage"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2026")<<"Rang dernier passage PPI 1"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2027")<<"Rang dernier passage PPI 2"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2028")<<"Rang dernier passage PPI 3"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2029")<<"Rang dernier passage PPI 4"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2030")<<"Rang dernier passage PPE 1"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2032")<<"Rang dernier passage PPE 2"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2032")<<"Rang dernier passage PPE 3"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2033")<<"Rang dernier passage PPE 4"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2034")<<"Rang dernier passage PG 1"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2035")<<"Rang dernier passage PG 2"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2036")<<"Rang dernier passage PG 3"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2037")<<"Rang dernier passage PG 4"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2038")<<"Rang dernier passage FN 1"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2039")<<"Rang dernier passage FN 2"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2040")<<"Rang dernier passage FN 3"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2041")<<"Rang dernier passage FN 4"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2042")<<"Erreur comptage"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2043")<<"Panne équipement"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2044")<<"Type MR (63,73,10)"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2045")<<"Identification du train  - élément 1"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2046")<<"Identification du train  - élément 2"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("2047")<<"Identification du train  - élément 3"<<"---"));



    ui->mHistorianDataTreeWidget->insertTopLevelItem(0,Item);
    ui->mHistorianDataTreeWidget->expandAll();
    ui->mHistorianDataTreeWidget->resizeColumnToContents(0);
    ui->mHistorianDataTreeWidget->resizeColumnToContents(1);
    ui->mHistorianDataTreeWidget->resizeColumnToContents(2);

    connect(ui->mConnectBtn,&QPushButton::clicked,this,&CHistorianPage::ConnectButtonPressed);



    mProgramHandle = 0;
}

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


int  CHistorianPage::UpdateDataTable(CHistorienData *Data)
{
    QTreeWidgetItem* Item = ui->mHistorianDataTreeWidget->topLevelItem(0);

    Item->child(0)->setText(2,QString("%1").arg(Data->mFNEnabled));
    Item->child(1)->setText(2,QString("%1").arg(Data->mPPEnabled));
    Item->child(2)->setText(2,QString("%1").arg(Data->mPGEnabled));
    Item->child(3)->setText(2,QString("%1").arg(Data->mPPZT2Enabled));
    Item->child(4)->setText(2,QString("%1").arg(Data->mMaintenanceMode));
    Item->child(5)->setText(2,QString("%1").arg(Data->mHPCHealth));
    Item->child(6)->setText(2,QString("%1").arg(Data->mSEIHealth));
    Item->child(7)->setText(2,QString("%1").arg(Data->mZT1Enabled));
    Item->child(8)->setText(2,QString("%1").arg(Data->mZT2Enabled));
    Item->child(9)->setText(2,QString("%1").arg(Data->mCalibPG));
    Item->child(10)->setText(2,QString("%1").arg(Data->mBootDateYr));
    Item->child(11)->setText(2,QString("%1").arg(Data->mBootDateMonth));
    Item->child(12)->setText(2,QString("%1").arg(Data->mBootDateDay));
    Item->child(13)->setText(2,QString("%1").arg(Data->mBootDateHour));
    Item->child(14)->setText(2,QString("%1").arg(Data->mBootDateMinutes));
    Item->child(15)->setText(2,QString("%1").arg(Data->mBootDateSeconds));
    Item->child(16)->setText(2,QString("%1").arg(Data->mTotalTrains));
    Item->child(17)->setText(2,QString("%1").arg(Data->mTotalDetections));
    Item->child(18)->setText(2,QString("%1").arg(Data->mTrainsToday));
    Item->child(19)->setText(2,QString("%1").arg(Data->mDetectToday));
    Item->child(20)->setText(2,QString("%1").arg(Data->mFNDetectToday));
    Item->child(21)->setText(2,QString("%1").arg(Data->mPPIDetectToday));
    Item->child(22)->setText(2,QString("%1").arg(Data->mPPEDetectToday));
    Item->child(23)->setText(2,QString("%1").arg(Data->mPGDetectToday));
    Item->child(24)->setText(2,QString("%1").arg(Data->mPPIZT2DetectToday));
    Item->child(25)->setText(2,QString("%1").arg(Data->mPPEZT2DetectToday));
    Item->child(26)->setText(2,QString("%1").arg(Data->mLastTrainYear));
    Item->child(27)->setText(2,QString("%1").arg(Data->mLastTrainMonth));
    Item->child(28)->setText(2,QString("%1").arg(Data->mLastTrainDay));
    Item->child(29)->setText(2,QString("%1").arg(Data->mLastTrainHour));
    Item->child(30)->setText(2,QString("%1").arg(Data->mLastTrainMinutes));
    Item->child(31)->setText(2,QString("%1").arg(Data->mLastTrainSeconds));
    Item->child(32)->setText(2,QString("%1").arg(Data->mLastZone));
    Item->child(33)->setText(2,QString("%1").arg(Data->mNbDetectLasTrain));
    Item->child(34)->setText(2,QString("%1").arg(Data->mLastTrainPPI1));
    Item->child(35)->setText(2,QString("%1").arg(Data->mLastTrainPPI2));
    Item->child(36)->setText(2,QString("%1").arg(Data->mLastTrainPPI3));
    Item->child(37)->setText(2,QString("%1").arg(Data->mLastTrainPPI4));
    Item->child(38)->setText(2,QString("%1").arg(Data->mLastTrainPPE1));
    Item->child(39)->setText(2,QString("%1").arg(Data->mLastTrainPPE2));
    Item->child(40)->setText(2,QString("%1").arg(Data->mLastTrainPPE3));
    Item->child(41)->setText(2,QString("%1").arg(Data->mLastTrainPPE4));
    Item->child(42)->setText(2,QString("%1").arg(Data->mLastTrainPG1));
    Item->child(43)->setText(2,QString("%1").arg(Data->mLastTrainPG2));
    Item->child(44)->setText(2,QString("%1").arg(Data->mLastTrainPG3));
    Item->child(45)->setText(2,QString("%1").arg(Data->mLastTrainPG4));
    Item->child(46)->setText(2,QString("%1").arg(Data->mLastTrainFN1));
    Item->child(47)->setText(2,QString("%1").arg(Data->mLastTrainFN2));
    Item->child(48)->setText(2,QString("%1").arg(Data->mLastTrainFN3));
    Item->child(49)->setText(2,QString("%1").arg(Data->mLastTrainFN4));
    Item->child(50)->setText(2,QString("%1").arg(Data->mLastTrainCountErr));
    Item->child(51)->setText(2,QString("%1").arg(Data->mLastTrainPEQ));
    Item->child(52)->setText(2,QString("%1").arg(Data->mLastTrainType));
    Item->child(53)->setText(2,QString("%1").arg(Data->mLastTrainCompo1));
    Item->child(54)->setText(2,QString("%1").arg(Data->mLastTrainCompo2));
    Item->child(55)->setText(2,QString("%1").arg(Data->mLastTrainCompo3));
}

void CHistorianPage::ConnectButtonPressed()
{
    QString IP = ui->mZTIPAddressLineEdit->text();
    int Port = ui->mZTPortLineEdit->text().toInt();

    if(ui->mConnectBtn->text() == "Déconnecter")
    {
        mProgramHandle->DisconnectFromZTRequest();
    }
    else
    {
        mProgramHandle->ConnectToZTRequest(IP,Port);
    }
}

int CHistorianPage::HistorianConnexionStatus(bool Connected)
{
    if(Connected)
    {
        ui->mConnectBtn->setText("Déconnecter");
    }
    else
    {
        ui->mConnectBtn->setText("Connecter");
    }
}
