/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Page d'activation ou de désactivation des fonctions de détection.
	Des boutons curseurs permettent de mettre à ON ou à OFF une fonction, soit
	pour son analyse ou pour sa TK ou les deux.

*/

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

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

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


#include "FunctionSelectionPage.h"
#include "GlobalDefine.h"
#include "Zonetest.h"
#include <QFont>



CFunctionSelectionPage::CFunctionSelectionPage(QGraphicsWidget *Parent)
{
	Q_UNUSED(Parent)
	mProgramHandle = 0;
	mZTSimulatorPtr = 0;



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

	QGraphicsTextItem *Title = new QGraphicsTextItem("Configuration des fonctions de détection",this);
	QFont font;
	font.setPointSize(15);
	Title->setFont(font);
	Title->setPos(40,10);

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

	mAcceptButton = new CTextButtonWidget("Appliquer");
	mAcceptButton->setParentItem(this);
    mAcceptButton->setPos(145,360);
	connect(mAcceptButton,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));


	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->hide(); //JFM 2024-09-05 Enlèvement de l'inhibition d'analyse...


	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));
    mFNFuncToggleBtn->hide();//JFM 2024-09-05 Enlèvement de l'inhibition d'analyse...
	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));
    mPGFuncToggleBtn->hide();//JFM 2024-09-05 Enlèvement de l'inhibition d'analyse...
	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));
    mPPFuncToggleBtn->hide();//JFM 2024-09-05 Enlèvement de l'inhibition d'analyse...
	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));
    mPP2FuncToggleBtn->hide();//JFM 2024-09-05 Enlèvement de l'inhibition d'analyse...
	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));
    mZT1FuncToggleBtn->hide();//JFM 2024-09-05 Enlèvement de l'inhibition d'analyse...
    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));
    mZT2FuncToggleBtn->hide();//JFM 2024-09-05 Enlèvement de l'inhibition d'analyse...
    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 CFunctionSelectionPage::resizeEvent(QGraphicsSceneResizeEvent *event)
{
	Q_UNUSED(event)
	mBackgroundRect->setRect(boundingRect());
}

void CFunctionSelectionPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{
	if(BtnPtr == mAcceptButton)
	{
		UpdateConfig();
		mProgramHandle->DetectionFunctionsConfigChanged(&mZTConfig);
		hide();
	}
	else if(BtnPtr == mCancelButton)
	{
		UpdateDisplay();	//revert any changes made to the buttons
		hide();
	}
}

unsigned int CFunctionSelectionPage::SetConfig(CZTDetectionFunctionConfig *Config)
{
	mZTConfig = *Config;
	UpdateDisplay();
	return RET_OK;
}

void CFunctionSelectionPage::UpdateDisplay()
{
	//update FN buttons
	if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_FN].AnalysisActive == true)
		mFNFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
	else
		mFNFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
	if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_FN].TKActive == true)
		mFNTKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
	else
		mFNTKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

	//update PG buttons
	if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_PG].AnalysisActive == true)
		mPGFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
	else
		mPGFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
	if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_PG].TKActive == true)
		mPGTKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
	else
		mPGTKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

	//update PP buttons
	if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP].AnalysisActive == true)
		mPPFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
	else
		mPPFuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
	if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP].TKActive == true)
		mPPTKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
	else
		mPPTKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

	//update PP2 buttons
	if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP2].AnalysisActive == true)
		mPP2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
	else
		mPP2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
	if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP2].TKActive == true)
		mPP2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
	else
		mPP2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

    //update ZT1 buttons
    if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT1].AnalysisActive == true)
        mZT1FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mZT1FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
    if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT1].TKActive == true)
        mZT1TKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mZT1TKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

    //update ZT2 buttons
    if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT2].AnalysisActive == true)
        mZT2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mZT2FuncToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);
    if(mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT2].TKActive == true)
        mZT2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_ON);
    else
        mZT2TKToggleBtn->SetButtonState(TOGGLE_BUTTON_OFF);

}

void CFunctionSelectionPage::UpdateConfig()
{
    //update FN flags
	if(mFNFuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_FN].AnalysisActive = true;
	else
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_FN].AnalysisActive = false;

	if(mFNTKToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_FN].TKActive = true;
	else
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_FN].TKActive = false;

    //update PG flags
	if(mPGFuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PG].AnalysisActive = true;
	else
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PG].AnalysisActive = false;

	if(mPGTKToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PG].TKActive = true;
	else
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PG].TKActive = false;

    //update PP flags
	if(mPPFuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP].AnalysisActive = true;
	else
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP].AnalysisActive = false;

	if(mPPTKToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP].TKActive = true;
	else
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP].TKActive = false;

    //update PP2 flags
	if(mPP2FuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP2].AnalysisActive = true;
	else
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP2].AnalysisActive = false;

	if(mPP2TKToggleBtn->GetButtonState())
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP2].TKActive = true;
	else
		mZTConfig.mZTDetectionConfig[DETECTION_FCT_PP2].TKActive = false;

    //update ZT1 flags
    if(mZT1FuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
        mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT1].AnalysisActive = true;
    else
        mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT1].AnalysisActive = false;

    if(mZT1TKToggleBtn->GetButtonState())
        mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT1].TKActive = true;
    else
        mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT1].TKActive = false;


    //update ZT2 flags
    if(mZT2FuncToggleBtn->GetButtonState() == TOGGLE_BUTTON_ON)
        mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT2].AnalysisActive = true;
    else
        mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT2].AnalysisActive = false;

    if(mZT2TKToggleBtn->GetButtonState())
        mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT2].TKActive = true;
    else
        mZTConfig.mZTDetectionConfig[DETECTION_FCT_ZT2].TKActive = false;
}

void CFunctionSelectionPage::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);
       }
    }
}

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