/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
    Page offrant différentes option pour l'entretien de la ZT

*/

/* ************************************************************************** */
/* Revision:
### 20131021 JFM
    Verision d'origine.

### YYYMMDD Description du besoin ou du bug
    Description du changement.
 */

/* ************************************************************************** */


#include "ModbusSettingsPage.h"
#include <QPainter>
#include "GlobalDefine.h"
#include <QDialog>
#include <QMainWindow>
#include "Zonetest.h"
#include <QDateTime>
#include <QFont>
#include <QGraphicsProxyWidget>
#include <QMessageBox>
#include <QNetworkInterface>
//#include <sys/time.h>
#include "ZTLog.h"
#include <QDebug>




CModbusSettingsPage::CModbusSettingsPage(QGraphicsWidget *Parent)
{
	Q_UNUSED(Parent)


	mProgramHandle = 0;

    mBackgroundRect = new QGraphicsRectItem(boundingRect(), this);
	QBrush BackgroundBrush(QColor(245, 245, 255));
	mBackgroundRect->setBrush(BackgroundBrush);

    QGraphicsTextItem *Title = new QGraphicsTextItem("Paramètres Modbus de la Commande Centralisée",this);
	QFont font;
	font.setPointSize(18);
	Title->setFont(font);
	Title->setPos(40,10);

    mCancelButton = new CTextButtonWidget("Annuler");
	mCancelButton->setParentItem(this);
    mCancelButton->setPos(550,550);
	connect(mCancelButton,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

    mApplyButton = new CTextButtonWidget("Appliquer");
    mApplyButton->setParentItem(this);
    mApplyButton->setPos(650,550);
    mApplyButton->hide();
    connect(mApplyButton,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));


    QGraphicsProxyWidget *Proxy = new QGraphicsProxyWidget(this);
    QGraphicsTextItem *Dot = new QGraphicsTextItem(".",this);
    Dot->setFont(font);


    int IpSettingsPosx = 100, IpSettingsPosy = 200;
    Title = new QGraphicsTextItem("Adresse IP",this);
    font.setPointSize(16);
    Title->setFont(font);
    Title->setPos(IpSettingsPosx,IpSettingsPosy-35);

    mIPChunk1 = new QSpinBox();
    Proxy->setWidget(mIPChunk1);
    Proxy->setPos(IpSettingsPosx,IpSettingsPosy);
    mIPChunk1->resize(70,30);
    mIPChunk1->setMinimum(0);
    mIPChunk1->setMaximum(255);
    IpSettingsPosx += 90;
    Dot->setPos(IpSettingsPosx-17,IpSettingsPosy-5);
    connect(mIPChunk1,SIGNAL(valueChanged(int)),this,SLOT(SpinBoxValueChanged(int)));

    Proxy = new QGraphicsProxyWidget(this);
    Dot = new QGraphicsTextItem(".",this);
    Dot->setFont(font);
    mIPChunk2 = new QSpinBox();
    Proxy->setWidget(mIPChunk2);
    Proxy->setPos(IpSettingsPosx,IpSettingsPosy);
    mIPChunk2->resize(70,30);
    mIPChunk2->setMinimum(0);
    mIPChunk2->setMaximum(255);
    IpSettingsPosx += 90;
    Dot->setPos(IpSettingsPosx-17,IpSettingsPosy-5);
    connect(mIPChunk2,SIGNAL(valueChanged(int)),this,SLOT(SpinBoxValueChanged(int)));

    Proxy = new QGraphicsProxyWidget(this);
    Dot = new QGraphicsTextItem(".",this);
    Dot->setFont(font);
    mIPChunk3 = new QSpinBox();
    Proxy->setWidget(mIPChunk3);
    Proxy->setPos(IpSettingsPosx,IpSettingsPosy);
    mIPChunk3->resize(70,30);
    mIPChunk3->setMinimum(0);
    mIPChunk3->setMaximum(255);
    IpSettingsPosx += 90;
    Dot->setPos(IpSettingsPosx-17,IpSettingsPosy - 5);
    connect(mIPChunk3,SIGNAL(valueChanged(int)),this,SLOT(SpinBoxValueChanged(int)));

    Proxy = new QGraphicsProxyWidget(this);
    mIPChunk4 = new QSpinBox();
    Proxy->setWidget(mIPChunk4);
    mIPChunk4->setMinimum(0);
    mIPChunk4->setMaximum(255);
    Proxy->setPos(IpSettingsPosx,IpSettingsPosy);
    mIPChunk4->resize(70,30);
    connect(mIPChunk4,SIGNAL(valueChanged(int)),this,SLOT(SpinBoxValueChanged(int)));

    Proxy = new QGraphicsProxyWidget(this);
    mUseModbusChkBox = new QCheckBox("Activer la communication Modbus avec la Commande Centralisée");
    Proxy->setWidget(mUseModbusChkBox);
    Proxy->setPos(100,100);
    connect(mUseModbusChkBox,SIGNAL(stateChanged(int)),this,SLOT(CheckBoxStatedChanged(int)));



    IpSettingsPosx = 100, IpSettingsPosy = 250;
    mMACAddress = new QGraphicsTextItem("Adresse MAC: xx:xx:xx:xx",this);
    mMACAddress->setFont(font);
    mMACAddress->setPos(IpSettingsPosx,IpSettingsPosy);


    IpSettingsPosx = 100;
    IpSettingsPosy += 50;
    font.setPointSize(14);
    mModbusCCLinkState = new QGraphicsTextItem("Lien Modbus CC: Non établi",this);
    mModbusCCLinkState->setFont(font);
    mModbusCCLinkState->setPos(IpSettingsPosx+30,IpSettingsPosy);

    mCCModbusLinkLED = new QGraphicsPixmapItem(QPixmap("./Images/red-led-on-md.png").scaled(25,25),this);
    mCCModbusLinkLED->setPos(IpSettingsPosx,IpSettingsPosy+5);

    IpSettingsPosy += 30;
    mModbusCCConnectionState = new QGraphicsTextItem("Connection HPC: DÉCONNECTÉE",this);
    mModbusCCConnectionState->setFont(font);
    mModbusCCConnectionState->setPos(IpSettingsPosx+30,IpSettingsPosy);
    mCCConnectionLED = new QGraphicsPixmapItem(QPixmap("./Images/red-led-on-md.png").scaled(25,25),this);
    mCCConnectionLED->setPos(IpSettingsPosx,IpSettingsPosy+5);


    QGraphicsRectItem *Frame = new QGraphicsRectItem(80,70,600,400,this);
	QPen FramePen;
	FramePen.setWidth(3);
	Frame->setPen(FramePen);
}


void CModbusSettingsPage::resizeEvent(QGraphicsSceneResizeEvent *event)
{
	Q_UNUSED(event)
	mBackgroundRect->setRect(boundingRect());
}

void CModbusSettingsPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{
    if(BtnPtr == mCancelButton)
	{
        mProgramHandle->CloseModbusSettingsPage();
		hide();
	}
    else if(BtnPtr == mApplyButton)
    {
        QMessageBox msgBox;
        msgBox.setText("Redémarrage requis");
        msgBox.setInformativeText("Les paramètres modbus ont été changés\nVoulez-vous vraiment redémarrer l'ordinateur ZT?");
        msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Cancel);
        int ret = msgBox.exec();

        if(ret == QMessageBox::Ok)
        {
            qint32 Address = 0;
            Address = (mIPChunk1->value() & 0x000000FF);
            Address <<= 8;
            Address |= (mIPChunk2->value() & 0x000000FF);
            Address <<= 8;
            Address |= (mIPChunk3->value() & 0x000000FF);
            Address <<= 8;
            Address |= (mIPChunk4->value() & 0x000000FF);
            QHostAddress IPAddress(Address);

            bool UseModbus = (mUseModbusChkBox->checkState() == Qt::Checked);

            QString LogEntry = QString("Changement des paramètres Modbus CC -> Modbus Activé : %1, Adresse IP %2").arg(UseModbus).arg(IPAddress.toString().toAscii().data());
            CZTLog::instance()->AddLogString(LogEntry,true);

            mProgramHandle->ModbusSettingsChangedReboot(UseModbus,IPAddress);
          //  mProgramHandle->CloseModbusSettingsPage();
        }
    }
}

void CModbusSettingsPage::showEvent(QShowEvent *event)
{
    Q_UNUSED(event)
}

int CModbusSettingsPage::SetActualSettings(bool UseModbus, QHostAddress CCHostAdd)
{
    mUseModbusChkBox->setChecked(UseModbus);

    qint32 add = CCHostAdd.toIPv4Address();
    mIPChunk4->setValue(add & 0x000000FF);
    add >>= 8;
    mIPChunk3->setValue(add & 0x000000FF);
    add >>= 8;
    mIPChunk2->setValue(add & 0x000000FF);
    add >>= 8;
    mIPChunk1->setValue(add & 0x000000FF);

    mApplyButton->hide();

    QNetworkInterface IfInfo = QNetworkInterface::interfaceFromName("eth0");
    QString MacAddress = IfInfo.hardwareAddress();

    mMACAddress->setPlainText(MacAddress.prepend("Adresse MAC: " ));

    return RET_OK;
}


void CModbusSettingsPage::ModbusCCConnected(qint32 LocalIP, qint32 RemoteIP)
{
    mCCConnectionLED->setPixmap(QPixmap("./Images/green-led-on-md.png").scaled(25,25));
    mModbusCCConnectionState->setPlainText(QString("Connection HPC: Connectée. \nIP: Local[%1] - HPC[%2]").arg(QHostAddress(LocalIP).toString()).arg(QHostAddress(RemoteIP).toString()));
}

void CModbusSettingsPage::ModbusCCDisconnected()
{
    mCCConnectionLED->setPixmap(QPixmap("./Images/red-led-on-md.png").scaled(25,25));
    mModbusCCConnectionState->setPlainText("Connection HPC: DÉCONNECTÉE");
}

void CModbusSettingsPage::ModbusCCLinkUP()
{
    mCCModbusLinkLED->setPixmap(QPixmap("./Images/green-led-on-md.png").scaled(25,25));
    mModbusCCLinkState->setPlainText("Lien Modbus CC: Établi");
}

void CModbusSettingsPage::ModbusCCLinkDown()
{
    mCCModbusLinkLED->setPixmap(QPixmap("./Images/red-led-on-md.png").scaled(25,25));
    mModbusCCLinkState->setPlainText("Lien Modbus CC: Non établi");
}

void CModbusSettingsPage::CheckBoxStatedChanged(int state)
{
    ShowApplyButton();
}

void CModbusSettingsPage::SpinBoxValueChanged(int value)
{
    ShowApplyButton();
}

void CModbusSettingsPage::ShowApplyButton()
{
    mApplyButton->show();
}

//Grab the mouse if the user clicks outside buttons
void CModbusSettingsPage::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	Q_UNUSED(event)
}
void CModbusSettingsPage::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	Q_UNUSED(event)
}
