/*******************************************************************************
*                                                                              *
* 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 "GeneralSettingsPage.h"
#include <QPainter>
#include "GlobalDefine.h"
#include <QDialog>
#include <QMainWindow>
#include "Zonetest.h"
#include <QDateTime>
#include <QFont>
#include <QGraphicsProxyWidget>
//#include <sys/time.h>
#include "ZTLog.h"




CGeneralSettingsPage::CGeneralSettingsPage(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 généraux",this);
	QFont font;
	font.setPointSize(18);
	Title->setFont(font);
	Title->setPos(40,10);

	Title = new QGraphicsTextItem("Ajuster date & heure",this);
	Title->setFont(font);
    Title->setPos(270,80);

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

	QGraphicsProxyWidget *Proxy = new QGraphicsProxyWidget(this);
	mCalendarWidget = new QCalendarWidget();
	Proxy->setWidget(mCalendarWidget);
    Proxy->setPos(100,150);

	mTimeEdit = new QTimeEdit();
	Proxy = new QGraphicsProxyWidget(this);
	Proxy->setWidget(mTimeEdit);
    Proxy->setPos(475,150);
	mTimeEdit->setTime(QTime::currentTime());
	mTimeEdit->setDisplayFormat("hh:mm");

    mUseNetworkTimeChkBx = new QCheckBox("Utiliser l'heure réseau");
    Proxy = new QGraphicsProxyWidget(this);
    Proxy->setWidget(mUseNetworkTimeChkBx);
    Proxy->setPos(450,220);
    connect(mUseNetworkTimeChkBx,SIGNAL(stateChanged(int)),this,SLOT(NetworkTimeChkBxClicked(int)));

	mApplyDateTimeBtn = new CTextButtonWidget("Changer date & heure");
	mApplyDateTimeBtn->setParentItem(this);
    mApplyDateTimeBtn->setPos(450,250);
	connect(mApplyDateTimeBtn,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

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


    Title = new QGraphicsTextItem("Seuil Pneu de Guidage",this);
    Title->setFont(font);
    Title->setPos(270,400);
    Frame = new QGraphicsRectItem(80,405,600,175,this);
    Frame->setPen(FramePen);

    mPGTresholdValueSpinBox = new QSpinBox();
    Proxy = new QGraphicsProxyWidget(this);
    Proxy->setWidget(mPGTresholdValueSpinBox);
    Proxy->setPos(125,500);
    mPGTresholdValueSpinBox->resize(70,30);
    mPGTresholdValueSpinBox->setMinimum(1);
    mPGTresholdValueSpinBox->setMaximum(2000);
    mPGTresholdValueSpinBox->setValue(255);

    mPGValueSetBtn = new CTextButtonWidget("Changer Seuil",0,30);
    mPGValueSetBtn->setParentItem(this);
    mPGValueSetBtn->setPos(200,500);
    connect(mPGValueSetBtn,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

    mActualPGTresholdValueText = new QGraphicsTextItem(this);
    mActualPGTresholdValueText->setPos(125,450);
    font.setPointSize(14);
    mActualPGTresholdValueText->setFont(font);
    mActualPGTresholdValueText->setPlainText("Valeur actuelle : 255");

}

void CGeneralSettingsPage::UseNetworkTime(bool UseNetworkTime, bool DisableSignal)
{
    if(DisableSignal == true)
    {
        disconnect(mUseNetworkTimeChkBx,SIGNAL(stateChanged(int)),this,SLOT(NetworkTimeChkBxClicked(int)));
    }

    if(UseNetworkTime == true)
    {
        mUseNetworkTimeChkBx->setChecked(Qt::Checked);
        mApplyDateTimeBtn->hide();
    }
    else
    {
        mUseNetworkTimeChkBx->setChecked(Qt::Unchecked);
        mApplyDateTimeBtn->show();
    }

    if(DisableSignal == true)
    {
        connect(mUseNetworkTimeChkBx,SIGNAL(stateChanged(int)),this,SLOT(NetworkTimeChkBxClicked(int)));
    }

}

void CGeneralSettingsPage::resizeEvent(QGraphicsSceneResizeEvent *event)
{
	Q_UNUSED(event)


	mBackgroundRect->setRect(boundingRect());
}

void CGeneralSettingsPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{
    if(BtnPtr == mCancelButton)
	{
		mProgramHandle->CloseGeneralSettingsPage();
		hide();
	}
	else if(BtnPtr == mApplyDateTimeBtn)
	{
        QDateTime DateTime;
        DateTime.setTime(mTimeEdit->time());
        DateTime.setDate(mCalendarWidget->selectedDate());
        CZTLog::instance()->AddLogString("Changement manuel de la date et l'heure",true);
        mProgramHandle->SetSystemDateTime(&DateTime);
	}
    else if(BtnPtr == mPGValueSetBtn)
    {
        CZTLog::instance()->AddLogString(QString().sprintf("Seuil PG changé. Ancien: %d, Nouveau: %d",mProgramHandle->GetPGTreshold(),mPGTresholdValueSpinBox->value()),true);
        mProgramHandle->SetPGTreshold(mPGTresholdValueSpinBox->value());
        mActualPGTresholdValueText->setPlainText(QString().sprintf("Valeur seuil PG actuelle %d",mProgramHandle->GetPGTreshold()));
    }
}

void CGeneralSettingsPage::showEvent(QShowEvent *event)
{
	Q_UNUSED(event)
	mTimeEdit->setTime(QTime::currentTime());
    mActualPGTresholdValueText->setPlainText(QString().sprintf("Valeur seuil PG actuelle %d",mProgramHandle->GetPGTreshold()));
    mPGTresholdValueSpinBox->setValue(mProgramHandle->GetPGTreshold());
}

void CGeneralSettingsPage::NetworkTimeChkBxClicked(int state)
{
    if(state == Qt::Checked)
    {
        mApplyDateTimeBtn->hide();
        mProgramHandle->UseNetworkTime(true);
    }
    else
    {
        mApplyDateTimeBtn->show();
        mProgramHandle->UseNetworkTime(false);
    }
}

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