#include "ModbusPage.h"
#include "ui_CModbusPage.h"
#include <QFont>
#include <QPalette>
#include "OutilModbus.h"
#include <QMessageBox>
#include <QFont>

CModbusPage::CModbusPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CModbusPage)
{
    mProgramHandle = 0;
    ui->setupUi(this);
    QFont font;
    font.setPointSize(18);
    ui->mSimCCTitleLabel->setFont(font);
    ui->mSimCCTitleLabel->setAlignment(Qt::AlignCenter);
    QPalette pal = QPalette(ui->mSimCCTitleLabel->palette());
    pal.setColor(QPalette::WindowText,QColor(Qt::darkBlue));
    ui->mSimCCTitleLabel->setPalette(pal);

    ui->mCCEventsList->show();

    pal.setColor(QPalette::WindowText,Qt::darkGreen);

    ui->mPPI1ZT1AlarmLabel->setPalette(pal);
    ui->mPPEZT1AlarmLabel->setPalette(pal);
    ui->mFNZT1AlarmLabel->setPalette(pal);
    ui->mPGZT1AlarmLabel->setPalette(pal);
    ui->mV00AlarmLabel->setPalette(pal);
    ui->mPEQ1AlarmLabel->setPalette(pal);

    ui->mPPIZT2AlarmLabel->setPalette(pal);
    ui->mPPEZT2AlarmLabel->setPalette(pal);
    ui->mV02AlarmLabel->setPalette(pal);
    ui->mPEQ2AlarmLabel->setPalette(pal);

    connect(ui->mConnectBtn,SIGNAL(clicked()),this,SLOT(ConnectToSlaveBtnClicked()));
    connect(ui->mSendModbusReadBtn,SIGNAL(clicked()),this,SLOT(ReadModbusBtnClicked()));
    connect(ui->mAN1Btn,SIGNAL(clicked()),this,SLOT(SendAN1BtnClicked()));
    connect(ui->mAN2Btn,SIGNAL(clicked()),this,SLOT(SendAN2BtnClicked()));
    connect(ui->mDateTimeBtn,SIGNAL(clicked()),this,SLOT(SendDateTimeBtnClicked()));
    connect(ui->mSetTrainParamBtn,SIGNAL(clicked()),this,SLOT(SendTrainInfoBtnClicked()));

    connect(ui->mAutoRequestChkBx,SIGNAL(stateChanged(int)),this,SLOT(AutoSendRequestsCheckboxClicked(int)));
    connect(ui->mStayOnTopChkBx,SIGNAL(stateChanged(int)),this,SLOT(StayOnTopBtnClicked(int)));

    ui->mCCModbusTable->hide();
    ui->mWatchdogChkBx->setCheckState(Qt::Checked);
    connect(ui->mWatchdogChkBx,SIGNAL(stateChanged(int)),this,SLOT(WatchdogChkBxChanged(int)));

    QList<QTreeWidgetItem *> Items;
    ui->mCCModbusDataTree->setColumnCount(3);
    ui->mCCModbusDataTree->setHeaderItem(new QTreeWidgetItem(QStringList()<<"Flag/Alarme"<<"Valeur"<<"Rang"));

    //Items.append(new QTreeWidgetItem(QStringList(QString().fromUtf8("Pneu De Guidage ZT1 #2") << QString().fromUtf8("Pneu De Guidage ZT1 #2") << QString().fromUtf8("Pneu De Guidage ZT1 #2"))));
    Items.append(new QTreeWidgetItem(QStringList()<<"ZT1"));
    Items.append(new QTreeWidgetItem(QStringList()<<"ZT2"));
    Items.append(new QTreeWidgetItem(QStringList()<<"ZT"));

    QTreeWidgetItem* Item;
  //  Item = new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Alarmes"));
  //  Items.at(0)->addChild(Item);

    Item = Items.at(0);

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT1 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT1 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu De Guidage ZT1 #4")<<"0"<<"---"));

    Item ->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Frotteur Négatif ZT1 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Itinéraire")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Alarme V00 ZT1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Panne Équipement ZT1")<<"0"<<"---"));

    Item = Items.at(1);

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Intérieur ZT2 #4")<<"0"<<"---"));

   // Items.at(1)->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("  ")));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #1")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #3")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Pneu Porteur Extérieur ZT2 #4")<<"0"<<"---"));

    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Alarme V02 ZT2")<<"0"<<"---"));
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Panne Équipement ZT2")<<"0"<<"---"));

    Item = Items.at(2);
    Item->addChild(new QTreeWidgetItem(QStringList()<<QString().fromUtf8("Watchdog ZT")<<"0"<<"---"));


    ui->mCCModbusDataTree->insertTopLevelItems(0,Items);
    ui->mCCModbusDataTree->resizeColumnToContents(1);

    ui->mCCModbusDataTree->expandAll();
    ui->mCCModbusDataTree->resizeColumnToContents(0);

   // ui->mCCModbusDataTree->topLevelItem(0)->addChild(new QTreeWidgetItem(QStringList() << "TEST" << "TEST"));


    mLedONPixmap = QPixmap("./images/green-led-on-md.png").scaled(20,20);
    mLedOFFPixmap = QPixmap("./images/green-led-off-md.png").scaled(20,20);
    ui->mActivityBitmap->setPixmap(mLedOFFPixmap);

    ui->mZT1TrainTypeBox->addItem("MR63");
    ui->mZT1TrainTypeBox->addItem("MR73");
    ui->mZT1TrainTypeBox->addItem("MPM10");
    ui->mZT1TrainTypeBox->addItem("Travaux");
    ui->mZT1TrainTypeBox->addItem("97");
    ui->mZT1TrainTypeBox->addItem("98");
    ui->mZT1TrainTypeBox->addItem("99");

    ui->mZT2TrainTypeBox->addItem("MR63");
    ui->mZT2TrainTypeBox->addItem("MR73");
    ui->mZT2TrainTypeBox->addItem("MPM10");
    ui->mZT2TrainTypeBox->addItem("Travaux");
    ui->mZT2TrainTypeBox->addItem("97");
    ui->mZT2TrainTypeBox->addItem("98");
    ui->mZT2TrainTypeBox->addItem("99");

    ui->mZT1Compo1->setValue(100);
    ui->mZT1Compo2->setValue(200);
    ui->mZT1Compo3->setValue(300);

    ui->mZT2Compo1->setValue(100);
    ui->mZT2Compo2->setValue(200);
    ui->mZT2Compo3->setValue(300);
}

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

void CModbusPage::ConnectToSlaveBtnClicked()
{
    if(ui->mConnectBtn->text() == QString::fromUtf8("Déconnecter"))
    {
        if(mProgramHandle->DisconnectCCFromZTButtonPressed() == 0)
        {
            MasterDisconnected();
        }
    }
    else
    {
        bool OK = false;
        QString IP = ui->mZTIPAddressLineEdit->text();
        if(IP.isEmpty())
        {
            QMessageBox::warning(this,"Adresse IP invalide","Veuillez spécifier une adresse IP");
            return;
        }
        int Port = ui->mZTPortLineEdit->text().toInt(&OK,10);
        if(OK == false)
        {
            QMessageBox::warning(this,"Numéro de port invalide","Port invalide, vérifier le numéro de port");
            return;
        }

        if(mProgramHandle->ConnectCCToZTButtonPressed(IP,Port) == 0)
        {
            QMessageBox::warning(this,"Paramètres invalides","Paramètres IP ou Port invalides.");
        }
    }
}

void CModbusPage::MasterConnected()
{
    ui->mConnectBtn->setText(QString::fromUtf8("Déconnecter"));
}

void CModbusPage::MasterDisconnected()
{
    ui->mConnectBtn->setText("Connecter");
    ui->mAutoRequestChkBx->setChecked(false);
}

void CModbusPage::ReadModbusBtnClicked()
{
    mProgramHandle->SendMasterReadCommand();
}

void CModbusPage::UpdateZTInfo(CZTInfo *DataStruct)
{

    QTreeWidgetItem* Item;
    QString TempStr;

    //ZT1
    Item = ui->mCCModbusDataTree->topLevelItem(0);

    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag1));
    Item->child(1)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag2));
    Item->child(2)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag3));
    Item->child(3)->setText(1,QString("%1").arg(DataStruct->mZT1PPIAlarmFlag4));

    Item->child(4)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag1));
    Item->child(5)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag2));
    Item->child(6)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag3));
    Item->child(7)->setText(1,QString("%1").arg(DataStruct->mZT1PPEAlarmFlag4));

    Item->child(8)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag1));
    Item->child(9)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag2));
    Item->child(10)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag3));
    Item->child(11)->setText(1,QString("%1").arg(DataStruct->mZT1PGAlarmFlag4));

    Item->child(12)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag1));
    Item->child(13)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag2));
    Item->child(14)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag3));
    Item->child(15)->setText(1,QString("%1").arg(DataStruct->mZT1FNAlarmFlag4));

    if(DataStruct->mITI10_12Flag == 0)
    {
        TempStr = "Principal";
    }
    else
    {
        TempStr = "Secondaire";
    }
    Item->child(16)->setText(1,TempStr);

    Item->child(17)->setText(1,QString("%1").arg(DataStruct->mZT1V00AlarmFlag));
    Item->child(18)->setText(1,QString("%1").arg(DataStruct->mZT1PEQ1AlarmFlag));

    for(int i = 0; i < 16; i++)
    {
        Item->child(i)->setText(2,QString("%1").arg(DataStruct->mZT1Ranks.at(i)));
    }

    //ZT2
    Item = ui->mCCModbusDataTree->topLevelItem(1);

    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag1));
    Item->child(1)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag2));
    Item->child(2)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag3));
    Item->child(3)->setText(1,QString("%1").arg(DataStruct->mZT2PPIAlarmFlag4));

    Item->child(4)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag1));
    Item->child(5)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag2));
    Item->child(6)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag3));
    Item->child(7)->setText(1,QString("%1").arg(DataStruct->mZT2PPEAlarmFlag4));

    Item->child(8)->setText(1,QString("%1").arg(DataStruct->mZT2V02AlarmFlag));
    Item->child(9)->setText(1,QString("%1").arg(DataStruct->mZT2PEQ2AlarmFlag));

    for(int i = 0; i < 8; i++)
    {
        Item->child(i)->setText(2,QString("%1").arg(DataStruct->mZT2Ranks.at(i)));
    }

    //Watchdog
    Item = ui->mCCModbusDataTree->topLevelItem(2);
    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mWatchdogZT));



//     QPalette OffPal = QPalette(ui->mSimCCTitleLabel->palette());
//     QPalette OnPal = QPalette(ui->mSimCCTitleLabel->palette());

//     OffPal.setColor(QPalette::WindowText,Qt::darkGreen);
//     OnPal.setColor(QPalette::WindowText,Qt::red);

//     if(DataStruct->mZT1PPIAlarm == 1)
//     {
//        ui->mPPI1ZT1AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mPPI1ZT1AlarmLabel->setPalette(OffPal);
//     }

//     if(DataStruct->mZT1PPEAlarm == 1)
//     {
//        ui->mPPEZT1AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mPPEZT1AlarmLabel->setPalette(OffPal);
//     }

//     if(DataStruct->mZT1FNAlarm == 1)
//     {
//        ui->mFNZT1AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mFNZT1AlarmLabel->setPalette(OffPal);
//     }

//     if(DataStruct->mZT1PGAlarm == 1)
//     {
//        ui->mPGZT1AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mPGZT1AlarmLabel->setPalette(OffPal);
//     }

//     if(DataStruct->mZT1V00Alarm == 1)
//     {
//        ui->mV00AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mV00AlarmLabel->setPalette(OffPal);
//     }

//     if(DataStruct->ZT1PEQ1Alarm == 1)
//     {
//        ui->mPEQ1AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mPEQ1AlarmLabel->setPalette(OffPal);
//     }

//     if(DataStruct->mZT2PPIAlarm == 1)
//     {
//        ui->mPPIZT2AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mPPIZT2AlarmLabel->setPalette(OffPal);
//     }

//     if(DataStruct->mZT2PPEAlarm == 1)
//     {
//        ui->mPPEZT2AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mPPEZT2AlarmLabel->setPalette(OffPal);
//     }

//     if(DataStruct->mZT2V02Alarm == 1)
//     {
//        ui->mV02AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mV02AlarmLabel->setPalette(OffPal);
//     }

//     if(DataStruct->mZT2PEQ2Alarm == 1)
//     {
//        ui->mPEQ2AlarmLabel->setPalette(OnPal);
//     }
//     else
//     {
//         ui->mPEQ2AlarmLabel->setPalette(OffPal);
//     }

//     ui->mZT1RangAlarmeLabel->setText(QString("%1").arg(DataStruct->mZT1RangPP_PG));
//     ui->mZT1RangFNLabel->setText(QString("%1").arg(DataStruct->mZT1RangFN));
//     ui->mZT2RangAlarmeLabel->setText(QString("%1").arg(DataStruct->mZT2RangPP));
}

void CModbusPage::SendAN1BtnClicked()
{
    mProgramHandle->SendAN1Request();
}

void CModbusPage::SendAN2BtnClicked()
{
    mProgramHandle->SendAN2Request();
}

void CModbusPage::AutoSendRequestsCheckboxClicked(int state)
{
    if(state == Qt::Checked)
    {
        int delay = ui->mZTAutoReqDelay->text().toInt();
        mProgramHandle->SetAutoRequestMode(true,delay);
    }
    else
    {
        mProgramHandle->SetAutoRequestMode(false,0);
    }
}

void CModbusPage::StayOnTopBtnClicked(int state)
{
    if(state == Qt::Checked)
    {
        Qt::WindowFlags flags = window()->windowFlags();
        flags |= Qt::WindowStaysOnTopHint;
        window()->setWindowFlags(flags);
        window()->show();
    }
    else
    {
        Qt::WindowFlags flags = window()->windowFlags();
        flags &= ~Qt::WindowStaysOnTopHint;
        window()->setWindowFlags(flags);
        window()->show();
    }
}

void CModbusPage::ModbusActivity()
{
    static bool IsON = false;
    if(IsON)
    {
        ui->mActivityBitmap->setPixmap(mLedOFFPixmap);
        IsON = false;
    }
    else
    {
        ui->mActivityBitmap->setPixmap(mLedONPixmap);
        IsON = true;
    }
}

void CModbusPage::WatchdogChkBxChanged(int state)
{
    if(state == Qt::Checked)
    {
        mProgramHandle->EnableWatchdog(true);
    }
    else
    {
        mProgramHandle->EnableWatchdog(false);
    }
}

void CModbusPage::SendDateTimeBtnClicked()
{
    mProgramHandle->SendDateTime();
}

void CModbusPage::SendTrainInfoBtnClicked()
{
    QList<qint16> TrainInfo;
    qint16 ZT1Type = 95;
    if(ui->mZT1TrainTypeBox->currentText() == "MR63")
        ZT1Type = 63;
    else if(ui->mZT1TrainTypeBox->currentText() == "MR73")
        ZT1Type = 73;
    else if(ui->mZT1TrainTypeBox->currentText() == "MPM10")
        ZT1Type = 10;
    else if(ui->mZT1TrainTypeBox->currentText() == "Travaux")
        ZT1Type = 0;
    else if(ui->mZT1TrainTypeBox->currentText() == "97")
        ZT1Type = 97;
    else if(ui->mZT1TrainTypeBox->currentText() == "98")
        ZT1Type = 98;
    else if(ui->mZT1TrainTypeBox->currentText() == "99")
        ZT1Type = 99;

    qint16 ZT2Type = 95;
    if(ui->mZT2TrainTypeBox->currentText() == "MR63")
        ZT2Type = 63;
    else if(ui->mZT2TrainTypeBox->currentText() == "MR73")
        ZT2Type = 73;
    else if(ui->mZT2TrainTypeBox->currentText() == "MPM10")
        ZT2Type = 10;
    else if(ui->mZT2TrainTypeBox->currentText() == "Travaux")
        ZT2Type = 0;
    else if(ui->mZT2TrainTypeBox->currentText() == "97")
        ZT2Type = 97;
    else if(ui->mZT2TrainTypeBox->currentText() == "98")
        ZT2Type = 98;
    else if(ui->mZT2TrainTypeBox->currentText() == "99")
        ZT2Type = 99;

    TrainInfo.append(ZT1Type);
    TrainInfo.append(ZT2Type);
    TrainInfo.append(ui->mZT1Compo1->value());
    TrainInfo.append(ui->mZT1Compo2->value());
    TrainInfo.append(ui->mZT1Compo3->value());
    TrainInfo.append(ui->mZT2Compo1->value());
    TrainInfo.append(ui->mZT2Compo2->value());
    TrainInfo.append(ui->mZT2Compo3->value());

    qint16 InhibZT1 = 0, InhibZT2 = 0;
    if(ui->mZT1InhibPassChkBx->checkState() == Qt::Checked)
    {
        InhibZT1 = 1;
    }
    if(ui->mZT2InhibPassChkBx->checkState() == Qt::Checked)
    {
        InhibZT2 = 1;
    }

    mProgramHandle->SendTrainInfo(TrainInfo);
    mProgramHandle->SendInhibitZT(InhibZT1,InhibZT2);
}
