/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Page affichant la liste des passages de train.

*/

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

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

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


#include "RemoteZTCtrlPage.h"
#include <QGraphicsProxyWidget>
#include "OutilZT.h"
#include <QString>
#include <QTextCodec>
#include <QHeaderView>
#include <QTextStream>
#include "TCPProtocol.h"


CRemoteZTCtrlPage::~CRemoteZTCtrlPage()
{
//    delete mListParsingTimer;
}

CRemoteZTCtrlPage::CRemoteZTCtrlPage(QGraphicsWidget *Parent)
{
//    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
	setParentItem(Parent);
	mProgramHandle = 0;


    mDeleteLogFile = new CTextButtonWidget("Effacer fichier LOG");
    mDeleteLogFile->setParentItem(this);
    mDeleteLogFile->setPos(10,10);
    connect(mDeleteLogFile,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

    mSetZTFunctionsBtn = new CTextButtonWidget("Envoyer Config");
    mSetZTFunctionsBtn->setParentItem(this);
    mSetZTFunctionsBtn->setPos(10,50);
    connect(mSetZTFunctionsBtn,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

    QFont font;
    QPointF ArrayTopLeft;
    ArrayTopLeft.setX(250);
    ArrayTopLeft.setY(100);
    qreal XButtonSpacing = 90;
    qreal YLineSpacing = 40;
    QString ONPixmapFilePath = "./Images/Slider_Green.png";
    QString OFFPixmapFilePath = "./Images/Slider_Red.png";
    QSizeF SlideButtonSize(50,30);
    font.setPointSize(12);


    QGraphicsTextItem *Label = new QGraphicsTextItem("Analyse",this);
    Label->setFont(font);
    Label->setPos(ArrayTopLeft.x() - 10,ArrayTopLeft.y() - YLineSpacing);

    Label = new QGraphicsTextItem("TK",this);
    Label->setFont(font);
    Label->setPos(ArrayTopLeft.x() + XButtonSpacing +5 ,ArrayTopLeft.y() - YLineSpacing);

    int i = 0;
    Label = new QGraphicsTextItem("Frotteur Négatif:",this);
    Label->setFont(font);
    Label->setPos(35,ArrayTopLeft.y() + (i*YLineSpacing));
    mFNFuncToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mFNFuncToggleBtn->resize(SlideButtonSize);
    mFNFuncToggleBtn->setPos(ArrayTopLeft.x(),ArrayTopLeft.y() + (i*YLineSpacing));
    mFNTKToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mFNTKToggleBtn->resize(SlideButtonSize);
    mFNTKToggleBtn->setPos(ArrayTopLeft.x() + XButtonSpacing,ArrayTopLeft.y() + (i*YLineSpacing));

    i++;
    Label = new QGraphicsTextItem("Pneu de guidage:",this);
    Label->setFont(font);
    Label->setPos(35,ArrayTopLeft.y() + (i*YLineSpacing));
    mPGFuncToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mPGFuncToggleBtn->resize(SlideButtonSize);
    mPGFuncToggleBtn->setPos(ArrayTopLeft.x(),ArrayTopLeft.y() + (i*YLineSpacing));
    mPGTKToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mPGTKToggleBtn->resize(SlideButtonSize);
    mPGTKToggleBtn->setPos(ArrayTopLeft.x() + XButtonSpacing,ArrayTopLeft.y() + (i*YLineSpacing));

    i++;
    Label = new QGraphicsTextItem("Pneu porteur:",this);
    Label->setFont(font);
    Label->setPos(35,ArrayTopLeft.y() + (i*YLineSpacing));
    mPPFuncToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mPPFuncToggleBtn->resize(SlideButtonSize);
    mPPFuncToggleBtn->setPos(ArrayTopLeft.x(),ArrayTopLeft.y() + (i*YLineSpacing));
    mPPTKToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mPPTKToggleBtn->resize(SlideButtonSize);
    mPPTKToggleBtn->setPos(ArrayTopLeft.x() + XButtonSpacing,ArrayTopLeft.y() + (i*YLineSpacing));

    i++;
    Label = new QGraphicsTextItem("Pneu porteur ZT2:",this);
    Label->setFont(font);
    Label->setPos(35,ArrayTopLeft.y() + (i*YLineSpacing));
    mPP2FuncToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mPP2FuncToggleBtn->resize(SlideButtonSize);
    mPP2FuncToggleBtn->setPos(ArrayTopLeft.x(),ArrayTopLeft.y() + (i*YLineSpacing));
    mPP2TKToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mPP2TKToggleBtn->resize(SlideButtonSize);
    mPP2TKToggleBtn->setPos(ArrayTopLeft.x() + XButtonSpacing,ArrayTopLeft.y() + (i*YLineSpacing));

    i++;
    Label = new QGraphicsTextItem("ZT1:",this);
    Label->setFont(font);
    Label->setPos(35,ArrayTopLeft.y() + (i*YLineSpacing));
    mZT1FuncToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mZT1FuncToggleBtn->resize(SlideButtonSize);
    mZT1FuncToggleBtn->setPos(ArrayTopLeft.x(),ArrayTopLeft.y() + (i*YLineSpacing));
    connect(mZT1FuncToggleBtn,SIGNAL(ButtonToggled(CToggleButtonWidget*)),this,SLOT(ToggleButtonToggled(CToggleButtonWidget*)));
    mZT1TKToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mZT1TKToggleBtn->resize(SlideButtonSize);
    mZT1TKToggleBtn->setPos(ArrayTopLeft.x() + XButtonSpacing,ArrayTopLeft.y() + (i*YLineSpacing));
    connect(mZT1TKToggleBtn,SIGNAL(ButtonToggled(CToggleButtonWidget*)),this,SLOT(ToggleButtonToggled(CToggleButtonWidget*)));

    i++;
    Label = new QGraphicsTextItem("ZT2:",this);
    Label->setFont(font);
    Label->setPos(35,ArrayTopLeft.y() + (i*YLineSpacing));
    mZT2FuncToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mZT2FuncToggleBtn->resize(SlideButtonSize);
    mZT2FuncToggleBtn->setPos(ArrayTopLeft.x(),ArrayTopLeft.y() + (i*YLineSpacing));
    connect(mZT2FuncToggleBtn,SIGNAL(ButtonToggled(CToggleButtonWidget*)),this,SLOT(ToggleButtonToggled(CToggleButtonWidget*)));
    mZT2TKToggleBtn = new CToggleButtonWidget(ONPixmapFilePath,OFFPixmapFilePath,this);
    mZT2TKToggleBtn->resize(SlideButtonSize);
    mZT2TKToggleBtn->setPos(ArrayTopLeft.x() + XButtonSpacing,ArrayTopLeft.y() + (i*YLineSpacing));
    connect(mZT2TKToggleBtn,SIGNAL(ButtonToggled(CToggleButtonWidget*)),this,SLOT(ToggleButtonToggled(CToggleButtonWidget*)));

}

void CRemoteZTCtrlPage::ToggleButtonToggled(CToggleButtonWidget *ToggleButton)
{

    if(ToggleButton == mZT1FuncToggleBtn)
    {
        if(mZT1FuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_OFF)
        {
            mFNFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
            mFNFuncToggleBtn->SetButtonEnabled(false);
            mPGFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
            mPGFuncToggleBtn->SetButtonEnabled(false);
            mPPFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
            mPPFuncToggleBtn->SetButtonEnabled(false);
        }
        else
        {
            mFNFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
            mFNFuncToggleBtn->SetButtonEnabled(true);
            mPGFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
            mPGFuncToggleBtn->SetButtonEnabled(true);
            mPPFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
            mPPFuncToggleBtn->SetButtonEnabled(true);
        }
    }
    if(ToggleButton == mZT1TKToggleBtn)
    {
        if(mZT1TKToggleBtn->GetButtonState() == TOGGLE_BUTTON_OFF)
        {
            mFNTKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
            mFNTKToggleBtn->SetButtonEnabled(false);
            mPGTKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
            mPGTKToggleBtn->SetButtonEnabled(false);
            mPPTKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
            mPPTKToggleBtn->SetButtonEnabled(false);
        }
        else
        {
            mFNTKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
            mFNTKToggleBtn->SetButtonEnabled(true);
            mPGTKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
            mPGTKToggleBtn->SetButtonEnabled(true);
            mPPTKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
            mPPTKToggleBtn->SetButtonEnabled(true);
        }
    }

    if(ToggleButton == mZT2FuncToggleBtn)
    {
        if(mZT2FuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_OFF)
        {
            mPP2FuncToggleBtn->SetButtonEnabled(false);
            mPP2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
        }
        else
        {
            mPP2FuncToggleBtn->SetButtonEnabled(true);
            mPP2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
       }
    }
    if(ToggleButton == mZT2TKToggleBtn)
    {
        if(mZT2TKToggleBtn->GetButtonState() == TOGGLE_BUTTON_OFF)
        {
            mPP2TKToggleBtn->SetButtonEnabled(false);
            mPP2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
        }
        else
        {
            mPP2TKToggleBtn->SetButtonEnabled(true);
            mPP2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
       }
    }
}

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

void CRemoteZTCtrlPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{
    if(BtnPtr == mDeleteLogFile)
    {
        mProgramHandle->DeleteRemoteZTLogRequest();
    }
    else if(BtnPtr == mSetZTFunctionsBtn)
    {
        CTCPZTFunctionsStatus FunctionsConfig;

        //update FN flags
           if(mFNFuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
               FunctionsConfig.mFNAnalysisActive = true;
           else
               FunctionsConfig.mFNAnalysisActive = false;

           if(mFNTKToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
               FunctionsConfig.mFNTKActive = true;
           else
               FunctionsConfig.mFNTKActive = false;

           //update PG flags
           if(mPGFuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
               FunctionsConfig.mPGAnalysisActive = true;
           else
               FunctionsConfig.mPGAnalysisActive = false;

           if(mPGTKToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
               FunctionsConfig.mPGTKActive = true;
           else
               FunctionsConfig.mPGTKActive = false;

           //update PP flags
           if(mPPFuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
               FunctionsConfig.mPP1AnalysisActive = true;
           else
               FunctionsConfig.mPP1AnalysisActive = false;

           if(mPPTKToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
               FunctionsConfig.mPP1TKActive = true;
           else
               FunctionsConfig.mPP1TKActive = false;

           //update PP2 flags
           if(mPP2FuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
               FunctionsConfig.mPP2AnalysisActive = true;
           else
               FunctionsConfig.mPP2AnalysisActive = false;

           if(mPP2TKToggleBtn->GetButtonState())
               FunctionsConfig.mPP2TKActive = true;
           else
               FunctionsConfig.mPP2TKActive = false;

           //update ZT1 flags
           if(mZT1FuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
               FunctionsConfig.mZT1AnalysisActive = true;
           else
               FunctionsConfig.mZT1AnalysisActive = false;

           if(mZT1TKToggleBtn->GetButtonState())
               FunctionsConfig.mZT1TKActive = true;
           else
               FunctionsConfig.mZT1TKActive = false;


           //update ZT2 flags
           if(mZT2FuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
               FunctionsConfig.mZT2AnalysisActive = true;
           else
               FunctionsConfig.mZT2AnalysisActive = false;

           if(mZT2TKToggleBtn->GetButtonState())
               FunctionsConfig.mZT2TKActive = true;
           else
               FunctionsConfig.mZT2TKActive = false;

           mProgramHandle->SetZTFunctionsStatus(FunctionsConfig);
    }
}

void CRemoteZTCtrlPage::UpdateDisplay(const CTCPZTStatus *RemoteZTStatus)
{
    //update FN buttons
    if(RemoteZTStatus->mFNAnalysisActive == true)
        mFNFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mFNFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
    if(RemoteZTStatus->mFNTKActive == true)
        mFNTKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mFNTKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

    //update PG buttons
    if(RemoteZTStatus->mPGAnalysisActive == true)
        mPGFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mPGFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
    if(RemoteZTStatus->mPGTKActive == true)
        mPGTKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mPGTKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

    //update PP buttons
    if(RemoteZTStatus->mPP1AnalysisActive == true)
        mPPFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mPPFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
    if(RemoteZTStatus->mPP1TKActive == true)
        mPPTKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mPPTKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

    //update PP2 buttons
    if(RemoteZTStatus->mPP2AnalysisActive == true)
        mPP2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mPP2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
    if(RemoteZTStatus->mPP2TKActive == true)
        mPP2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mPP2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

    //update ZT1 buttons
    if(RemoteZTStatus->mZT1AnalysisActive == true)
        mZT1FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mZT1FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
    if(RemoteZTStatus->mZT1TKActive == true)
        mZT1TKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mZT1TKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

    //update ZT2 buttons
    if(RemoteZTStatus->mZT2AnalysisActive == true)
        mZT2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mZT2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
    if(RemoteZTStatus->mZT2TKActive == true)
        mZT2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mZT2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
}

void CRemoteZTCtrlPage::NetworkZTConnected(int ID)
{

}
void CRemoteZTCtrlPage::NetworkZTDisconnected(int ID)
{

}

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