#include "SEISettingsPage.h"


#include "SEISettingsPage.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>




CSEISettingsPage::CSEISettingsPage(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 SEI",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)));

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

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

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


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


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

void CSEISettingsPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{
    if(BtnPtr == mCancelButton)
    {
        mProgramHandle->CloseSEISettingsPage();
        hide();
    }
    else if(BtnPtr == mApplyButton)
    {
            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);

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

            mProgramHandle->ApplySEISettings(IPAddress);//TODO
    }
}

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

int CSEISettingsPage::SetActualSettings(QHostAddress CCHostAdd)
{
    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();

    return RET_OK;
}


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

void CSEISettingsPage::ModbusSEIDisconnected()
{
    mSEIConnectionLED->setPixmap(QPixmap("./Images/red-led-on-md.png").scaled(25,25));
    mModbusSEIConnectionState->setPlainText("Connection NetTrac: DÉCONNECTÉE");
}

void CSEISettingsPage::ModbusSEILinkUP()
{
    mSEIModbusLinkLED->setPixmap(QPixmap("./Images/green-led-on-md.png").scaled(25,25));
    mModbusSEILinkState->setPlainText("Lien Modbus SEI: Établi");
}

void CSEISettingsPage::ModbusSEILinkDown()
{
    mSEIModbusLinkLED->setPixmap(QPixmap("./Images/red-led-on-md.png").scaled(25,25));
    mModbusSEILinkState->setPlainText("Lien Modbus SEI: Non établi");
}

void CSEISettingsPage::SpinBoxValueChanged(int value)
{
    Q_UNUSED(value)
    ShowApplyButton();
}

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

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