#include "SEIModbusPage.h"
#include "ui_SEIModbusPage.h"
#include "OutilModbus.h"

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

    QFont font;
    font.setPointSize(18);
    ui->mSEISimTitleLabel->setFont(font);
    ui->mSEISimTitleLabel->setAlignment(Qt::AlignCenter);
    QPalette pal = QPalette(ui->mSEISimTitleLabel->palette());
    pal.setColor(QPalette::WindowText,QColor(Qt::darkBlue));
    ui->mSEISimTitleLabel->setPalette(pal);

    SetConnected(false);

    connect(ui->mStartStopServerBtn,SIGNAL(clicked()),this,SLOT(ServerStartStopPressed()));
    connect(ui->mEnableWDChkBx,SIGNAL(toggled(bool)),this,SLOT(EnableWatchdogChkBxClicked()));
    connect(ui->mAN1ChkBx,SIGNAL(toggled(bool)),this,SLOT(AN1ChkBoxToggled()));
    connect(ui->mAN2ChkBx,SIGNAL(toggled(bool)),this,SLOT(AN2ChkBoxToggled()));



    QList<QTreeWidgetItem *> Items;
    ui->mZTAlarmDataTree->setColumnCount(3);
    ui->mZTAlarmDataTree->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->mZTAlarmDataTree->insertTopLevelItems(0,Items);
    ui->mZTAlarmDataTree->resizeColumnToContents(1);

    ui->mZTAlarmDataTree->expandAll();
    ui->mZTAlarmDataTree->resizeColumnToContents(0);
}

void CSEIModbusPage::UpdateZTInfo(CZTInfo *DataStruct)
{


    QTreeWidgetItem* Item;
    QString TempStr;

    //ZT1
    Item = ui->mZTAlarmDataTree->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->mZTAlarmDataTree->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->mZTAlarmDataTree->topLevelItem(2);
    Item->child(0)->setText(1,QString("%1").arg(DataStruct->mWatchdogZT));


}

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


int CSEIModbusPage::SetConnected(bool Connected)
{
    QFont font;
    font.setPointSize(18);
    font.setBold(true);
    ui->mSEIConnectStatLabel->setFont(font);
    ui->mSEIConnectStatLabel->setAlignment(Qt::AlignCenter);
    QPalette pal = QPalette(ui->mSEIConnectStatLabel->palette());

    if(Connected)
    {
        pal.setColor(QPalette::WindowText,QColor(Qt::green));
        ui->mSEIConnectStatLabel->setPalette(pal);
        ui->mSEIConnectStatLabel->setText("ZT: Connected");
    }
    else
    {
        pal.setColor(QPalette::WindowText,QColor(Qt::red));
        ui->mSEIConnectStatLabel->setPalette(pal);
        ui->mSEIConnectStatLabel->setText("ZT: Disconnected");
    }

    return 1;
}

void CSEIModbusPage::ServerStartStopPressed()
{
    if(mProgramHandle->ToggleSEIServerState() != 0)
    {
        if(mProgramHandle->IsSEIServerOpened() == true)
        {
            ui->mStartStopServerBtn->setText("Arreter Serveur");
        }
        else
        {
            ui->mStartStopServerBtn->setText("Démarrer Serveur");
        }
    }
}

void CSEIModbusPage::EnableWatchdogChkBxClicked()
{
    mProgramHandle->SEIWatchdogEnable(ui->mEnableWDChkBx->checkState() == Qt::Checked);
}

void CSEIModbusPage::AN1ChkBoxToggled()
{
    mProgramHandle->SEIAN1Toggle(ui->mAN1ChkBx->checkState() == Qt::Checked);
}

void CSEIModbusPage::AN2ChkBoxToggled()
{
    mProgramHandle->SEIAN2Toggle(ui->mAN2ChkBx->checkState() == Qt::Checked);
}
