#include "ZTAlarmGeneratorPage.h"
#include "ui_ZTAlarmGeneratorPage.h"
#include <QStringList>
#include "ZTAlarmGenerator.h"

CZTAlarmGeneratorPage::CZTAlarmGeneratorPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CZTAlarmGeneratorPage)
{
    ui->setupUi(this);
    mProgramHandle = 0;

    QStringList AlarmTypes;
    AlarmTypes << "Erreur de comptage ZT1"
               << "Détection Frotteur Négatif"
               << "Détection Pneu de Guidage"
               << "Détection Pneu Porteur Intérieur ZT1"
               << "Détection Pneu Porteur Extérieur ZT1"
               << "Erreur de comptage ZT2"
               << "Détection Pneu Porteur Intérieur ZT2"
               << "Détection Pneu Porteur Extérieur ZT2"
               << "Panne Équipement ZT1"
               << "Panne Équipement ZT2";

    ui->mAlarmComboBox->addItems(AlarmTypes);
    ui->mAlarmComboBox->setEditable(false);

    AlarmSelectionChanged("Erreur de comptage ZT1");

    ui->mConnectSEIBtn->setText("Connecter");

    connect(ui->mAlarmComboBox,SIGNAL(activated(QString)),this,SLOT(AlarmSelectionChanged(QString)));

    ui->mActiveAlarmsList->clear();

    connect(ui->mSendAlarmBtn,&QPushButton::clicked,this,&CZTAlarmGeneratorPage::SendAlarmsBtnPressed);
    connect(ui->mResetAlarmsBtn,&QPushButton::clicked,this,&CZTAlarmGeneratorPage::ClearAlarmsBtnPressed);
    connect(ui->mConnectSEIBtn,&QPushButton::clicked,this,&CZTAlarmGeneratorPage::ConnectToSEIBtnPressed);


}

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

void CZTAlarmGeneratorPage::ClearAlarmsBtnPressed()
{
    mActiveAlarms.ClearAllAlarms();
    ui->mActiveAlarmsList->clear();
    mProgramHandle->ClearAllAlarms();

}

void CZTAlarmGeneratorPage::ActivateAlarmBtnPressed()
{

}

void CZTAlarmGeneratorPage::ConnectToSEIBtnPressed()
{
    if(ui->mConnectSEIBtn->text() == "Connecter")
    {
        mProgramHandle->ConnectToSEIRequest();
    }
    else
    {
        mProgramHandle->DisconnectFromSEIRequest();
    }
}

void CZTAlarmGeneratorPage::AlarmSelectionChanged(QString text)
{
    qDebug(qPrintable(text.toUtf8()));

    if(text == "Erreur de comptage ZT1" ||
       text == "Panne Équipement ZT1" ||
       text == "Erreur de comptage ZT2" ||
       text == "Panne Équipement ZT2")
    {
        ui->mRankSpinBx->setRange(1,1);
        ui->mRankSpinBx->setValue(1);
        ui->mRankSpinBx->setEnabled(false);
    }
    else if(text == "Détection Frotteur Négatif")
    {
        ui->mRankSpinBx->setEnabled(true);
        ui->mRankSpinBx->setRange(1,36);
    }
    else
    {
        ui->mRankSpinBx->setEnabled(true);
        if(ui->mRankSpinBx->value() > 18)
        {
            ui->mRankSpinBx->setValue(18);
        }
        ui->mRankSpinBx->setRange(1,18);

    }
}

void CZTAlarmGeneratorPage::SendAlarmsBtnPressed()
{
    QString AlarmText = ui->mAlarmComboBox->currentText();
    if(AlarmText == "Erreur de comptage ZT1")
    {
        if(mActiveAlarms.mZT1AxleCountAlarm == true)
        {
            //Alarm already active... do nothing
            return;
        }

        mActiveAlarms.mZT1AxleCountAlarm = true;
        ui->mActiveAlarmsList->addItem("Erreur de comptage ZT1");
    }
    if(AlarmText == "Détection Frotteur Négatif")
    {
        if(mActiveAlarms.mZT1FNAlarmRanks.size() == 4)
        {
            return;
        }

        mActiveAlarms.mZT1FNAlarmRanks.append(ui->mRankSpinBx->value());
        ui->mActiveAlarmsList->addItem(QString("%2 au rang %1").arg(ui->mRankSpinBx->value()).arg(AlarmText));
    }
    if(AlarmText == "Détection Pneu de Guidage")
    {
        if(mActiveAlarms.mZT1PGAlarmRanks.size() == 4)
        {
            return;
        }

        mActiveAlarms.mZT1PGAlarmRanks.append(ui->mRankSpinBx->value());
        ui->mActiveAlarmsList->addItem(QString("%2 au rang %1").arg(ui->mRankSpinBx->value()).arg(AlarmText));
    }
    if(AlarmText == "Détection Pneu Porteur Intérieur ZT1")
    {
        if(mActiveAlarms.mZT1PPIAlarmRanks.size() == 4)
        {
            return;
        }

        mActiveAlarms.mZT1PPIAlarmRanks.append(ui->mRankSpinBx->value());
        ui->mActiveAlarmsList->addItem(QString("%2 au rang %1").arg(ui->mRankSpinBx->value()).arg(AlarmText));

    }
    if(AlarmText == "Détection Pneu Porteur Extérieur ZT1")
    {
        if(mActiveAlarms.mZT1PPEAlarmRanks.size() == 4)
        {
            return;
        }

        mActiveAlarms.mZT1PPEAlarmRanks.append(ui->mRankSpinBx->value());
        ui->mActiveAlarmsList->addItem(QString("%2 au rang %1").arg(ui->mRankSpinBx->value()).arg(AlarmText));

    }
    if(AlarmText == "Erreur de comptage ZT2")
    {
        if(mActiveAlarms.mZT2AxleCountAlarm == true)
        {
            return;
        }

        mActiveAlarms.mZT2AxleCountAlarm = true;
        ui->mActiveAlarmsList->addItem(QString("%1").arg(AlarmText));
    }
    if(AlarmText == "Détection Pneu Porteur Intérieur ZT2")
    {
        if(mActiveAlarms.mZT2PPIAlarmRanks.size() == 4)
        {
            return;
        }

        mActiveAlarms.mZT2PPIAlarmRanks.append(ui->mRankSpinBx->value());
        ui->mActiveAlarmsList->addItem(QString("%2 au rang %1").arg(ui->mRankSpinBx->value()).arg(AlarmText));
    }
    if(AlarmText == "Détection Pneu Porteur Extérieur ZT2")
    {
        if(mActiveAlarms.mZT2PPEAlarmRanks.size() == 4)
        {
            return;
        }

        mActiveAlarms.mZT2PPEAlarmRanks.append(ui->mRankSpinBx->value());
        ui->mActiveAlarmsList->addItem(QString("%2 au rang %1").arg(ui->mRankSpinBx->value()).arg(AlarmText));
    }
    if(AlarmText == "Panne Équipement ZT1")
    {
        if(mActiveAlarms.mZT1EquipmentFailureAlarm == true)
        {
            return;
        }

        mActiveAlarms.mZT1EquipmentFailureAlarm = true;
        ui->mActiveAlarmsList->addItem(QString("%1").arg(AlarmText));
    }
    if(AlarmText == "Panne Équipement ZT2")
    {
        if(mActiveAlarms.mZT2EquipmentFailureAlarm == true)
        {
            return;
        }

        mActiveAlarms.mZT2EquipmentFailureAlarm = true;
        ui->mActiveAlarmsList->addItem(QString("%1").arg(AlarmText));
    }

    mProgramHandle->UpdateActiveAlarms(&mActiveAlarms);
}

void CZTAlarmGeneratorPage::ModbusMasterConnected()
{
     ui->mSEIConnectStatusLbl->setText("SEI: Connecté");
     ui->mConnectSEIBtn->setText("Déconnecter");
}

void CZTAlarmGeneratorPage::ModbusMasterDisconnected()
{
    ui->mSEIConnectStatusLbl->setText("SEI: Déconnecté");
    ui->mConnectSEIBtn->setText("Connecter");
}

void CZTAlarmGeneratorPage::UpdateSEIState(bool AN1Flag, bool AN2Flag, quint16 Watchdog)
{
    if(AN1Flag)
    {
        mActiveAlarms.ClearZT1Alarms();
        mProgramHandle->UpdateActiveAlarms(&mActiveAlarms);
    }
    if(AN2Flag)
    {
        mActiveAlarms.ClearZT2Alarms();
        mProgramHandle->UpdateActiveAlarms(&mActiveAlarms);
    }

    if(Watchdog == 0)
    {
        ui->mSEIWatchdogValueLbl->setText("Watchdog SEI: 0");
    }
    else
    {
        ui->mSEIWatchdogValueLbl->setText("Watchdog SEI: 1");
    }
}
