/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012 - 2013                                                                        *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Page qui demande à l'utlisateur d'entrer un mot de passe.

	
*/

/* ************************************************************************** */
/* Revision:
### YYYMMDD JFM
	Verision d'origine.
	
### YYYYMMDD Description du besoin ou du bug
	Description du changement.
 */

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

#include "PasswordPrompt.h"
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsProxyWidget>
#include "ZTLog.h"
#include "EngLog.h"
#include <QDebug>


CPasswordPrompt::CPasswordPrompt(QGraphicsItem *Parent)
{
	setParentItem(Parent);
	setGeometry(0,0,1024,768);

	QGraphicsRectItem *BckgndRect = new QGraphicsRectItem(this);
	BckgndRect->setRect(0,0,1024,768);
	BckgndRect->setBrush(QBrush(QColor(Qt::lightGray)));

	mAcceptBtn = new CTextButtonWidget("OK");
	mAcceptBtn->setParentItem(this);
	mAcceptBtn->setPos(400,300);
	connect(mAcceptBtn,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonPressed(CTextButtonWidget*)));

	mCancelBtn = new CTextButtonWidget("Annuler");
	mCancelBtn->setParentItem(this);
	mCancelBtn->setPos(530,300);
	connect(mCancelBtn,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonPressed(CTextButtonWidget*)));

	mLineEditWidget = new QLineEdit();
	mLineEditWidget->setEchoMode(QLineEdit::Password);
	QGraphicsProxyWidget *Proxy = new QGraphicsProxyWidget(this);
	Proxy->setWidget(mLineEditWidget);
	Proxy->setPos(400,250);

	QGraphicsTextItem *Label =  new QGraphicsTextItem(this);
	Label->setPlainText("Veuillez entrer le mot de passe:");
	Label->setPos(400,220);

    mPassword.clear();
    mPassword = DEFAULT_PASSWORD;
    LoadPasswordFile();

	hide();
}

void CPasswordPrompt::RequestPassword()
{
	mLineEditWidget->setFocus();
	show();
}

void CPasswordPrompt::ButtonPressed(CTextButtonWidget *Btn)
{
	if(Btn == mAcceptBtn)
	{
        if(mLineEditWidget->text() == mPassword)
		{
			mLineEditWidget->clear();
			emit PasswordValid();
		}
		else
		{
			CZTLog::instance()->AddLogString("Tentative de mot de passe invalide.",true);
            CEngLog::instance()->AddLogString(QString().sprintf("Tentative de mot de passe invalide %s.",mLineEditWidget->text().toLatin1().data()),1);

			mLineEditWidget->clear();
		}
	}
	else if(Btn == mCancelBtn)
	{
		mLineEditWidget->clear();
		emit Canceled();
	}
}

bool CPasswordPrompt::LoadPasswordFile()
{
    QFile *ZTPasswordFile = new QFile("./Configuration/ZT.pwd");
    if (!ZTPasswordFile->open(QIODevice::ReadOnly | QIODevice::Text))
    {
        CEngLog::instance()->AddLogString("Fichier de mot de passe invalide.  Utilisation du mot de passe par défaut.");
        delete ZTPasswordFile;
        return false;
    }

    QString string;

    string.clear();
    string = ZTPasswordFile->readLine(1024);
    if(string.isEmpty())
    {
        CEngLog::instance()->AddLogString("Fichier de mot de passe invalide.  Utilisation du mot de passe par défaut.");
        ZTPasswordFile->close();
        delete ZTPasswordFile;
        return false;
    }

    string.remove("\n");    //remove trailing "\n" characters inserted by QFile.readline
    mPassword.clear();
    mPassword = string;

    ZTPasswordFile->close();
    delete ZTPasswordFile;

    return true;

}

void CPasswordPrompt::keyPressEvent(QKeyEvent *event)
{
	if(event->key() == Qt::Key_Return)
	{
		ButtonPressed(mAcceptBtn);
	}
}

void CPasswordPrompt::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	event->accept();
}

void CPasswordPrompt::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	event->accept();
}
