/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
    Description:
	Page de sélection des options (sélection des fonctions de détection, ingénierie,
	mode entretien, etc.).

*/

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

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

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


#include "OptionsPage.h"
#include <QPainter>
#include "GlobalDefine.h"
#include <QDialog>
#include <QMainWindow>
#include "OutilZT.h"
#include <QDateTime>
#include <QFont>
#include <QFileDialog>
#include <QTextEdit>
#include "OutilZT.h"
#include <QGraphicsProxyWidget>
#include "OutilZTVersion.h"


COptionsPage::~COptionsPage()
{
}

COptionsPage::COptionsPage(QGraphicsWidget *Parent)
{
	Q_UNUSED(Parent)

	mProgramHandle = 0;
    mSettings = 0;



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

    QGraphicsTextItem *Title = new QGraphicsTextItem("Options",this);
	QFont font;
	font.setPointSize(18);
	Title->setFont(font);
    Title->setPos(250,10);

    QGraphicsTextItem *SoftVersion = new QGraphicsTextItem(this);
    font.setPointSize(14);
    SoftVersion->setFont(font);
    QString VersionString = "OutilZT version: ";
    VersionString += SOFTWARE_VERSION;
    SoftVersion->setPlainText(VersionString);
    SoftVersion->setPos(25,210);

    QPixmap *FilePixmap = new QPixmap("./Images/open-file-icon.png");
    mLogDataDirDialogButton = new CTextButtonWidget("Sélectionner",0,50,150,15,FilePixmap);
    mLogDataDirDialogButton->setParentItem(this);
    mLogDataDirDialogButton->setPos(25,100);
    connect(mLogDataDirDialogButton, SIGNAL(TxtButtonClicked(CTextButtonWidget*)), this, SLOT(ButtonClicked(CTextButtonWidget*)));
    mLogDataDirDialogButton->hide();

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

    font.setPointSize(12);
    mLogsPathText = new QGraphicsTextItem(this);
    mLogsPathText->setPlainText("Répertoire de données: ");
    mLogsPathText->setFont(font);
    mLogsPathText->setPos(25,65);

    QGraphicsTextItem *StationName;
    QLineEdit *StationIP;
    QGraphicsProxyWidget *Proxy;
    int xpos = 375;
    int ypos = 150;
    for(int i = 0; i < NB_ZT_ID; i++)
    {
        StationName = new QGraphicsTextItem(this);
        StationName->setFont(font);
        StationName->setPlainText(COutilZT::GetStationName(i));
        StationName->adjustSize();

        StationName->setPos(xpos-StationName->boundingRect().width(), ypos + (i*30));
        StationName->hide();

        StationIP = new QLineEdit("100.100.100.100");
        Proxy = new QGraphicsProxyWidget(this);
        Proxy->setWidget(StationIP);
     //   StationIP->setInputMask("000.000.000.000;");
        Proxy->setPos(xpos+10,ypos +(i*30)+3);
        StationIP->resize(100,20);
        StationIP->setEnabled(false);
        StationIP->hide();

        mIpAddressList.append(StationIP);
    }

    mApplyNetworkSettingsBtn = new CTextButtonWidget("Appliquer");
    mApplyNetworkSettingsBtn->setParentItem(this);
    mApplyNetworkSettingsBtn->setPos(50,260);
    connect(mApplyNetworkSettingsBtn,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));



}

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

void COptionsPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{
    if(BtnPtr == mCancelButton)
    {
        hide();
    }
    else if(BtnPtr == mLogDataDirDialogButton)
    {
        QString DataFilePath;
        DataFilePath.clear();
        QFileDialog FileDlg;
        FileDlg.setFileMode(QFileDialog::DirectoryOnly);
//        DataFilePath = FileDlg.getOpenFileName(0,"Répertoire de données",QString(),QString(),0,QFileDialog::ShowDirsOnly);
        DataFilePath = FileDlg.getExistingDirectory(0,"Répertoire de données");

        if(DataFilePath.isEmpty())
            return;

        qDebug("open file %s",qPrintable(DataFilePath));
        mProgramHandle->DataSourceDirChanged(DataFilePath);
        QString tmp = "Répertoire de données: ";
        tmp += mProgramHandle->GetLogDataPath();
        mLogsPathText->setPlainText(tmp);

    }
    else if(BtnPtr == mApplyNetworkSettingsBtn)
    {
        if(mSettings == 0)
            return;
        for(int i = 0; i < mSettings->mNetworkCfgList.size(); i++)
        {
            mSettings->mNetworkCfgList[i].mIPAdress = mIpAddressList.at(i)->text();
        }
        mProgramHandle->NetworkDataChanged();
    }
}

void COptionsPage::SetLogsDataPath(QString Path)
{
    mLogsPathText->setPlainText(Path);
}

unsigned int COptionsPage::SetSettings(CSettingsData *SettingsData)
{
    mSettings = SettingsData;
    mLogsPathText->setPlainText(SettingsData->mDataSourceDirectoryPath);

    if(SettingsData->mNetworkCfgList.size() != mIpAddressList.size())
        return RET_ERROR;

    for(int i = 0; i < mIpAddressList.size(); i++)
    {
        mIpAddressList.at(i)->setText(SettingsData->mNetworkCfgList.at(i).mIPAdress);
    }

    return RET_OK;
}

void COptionsPage::EnableEngineeringMode()
{
    mLogDataDirDialogButton->show();

    for(int i = 0; i < mIpAddressList.size(); i++)
    {
        mIpAddressList.at(i)->setEnabled(true);
    }
}

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