/*******************************************************************************
*                                                                              *
* 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 "NetworkCtrlPage.h"
#include <QGraphicsProxyWidget>
#include "OutilZT.h"
#include <QString>
#include <QTextCodec>
#include <QHeaderView>




CNetworkCtrlPage::~CNetworkCtrlPage()
{
    delete mRadioButtonsGroup;
}

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

    mNetworkConnectButton = new CToggleTextButtonWidget("Déconnecter"," Connecter ",QPixmap("./Images/red_cross.png"),QPixmap("./Images/green_check.png"),25,125);
    mNetworkConnectButton->setParentItem(this);
//    mNetworkConnectButton->setPos(10,350);
    mNetworkConnectButton->setPos(10,25);
    connect(mNetworkConnectButton,SIGNAL(ToggleBtnActivate(CToggleTextButtonWidget*)),this,SLOT(ConnectButtonPressed()));
    connect(mNetworkConnectButton,SIGNAL(ToggleBtnDeActivate(CToggleTextButtonWidget*)),this,SLOT(ConnectButtonReleased()));

    mZTStatusRefreshButton = new CTextButtonWidget("Rafraîchir");
    mZTStatusRefreshButton->setParentItem(this);
    mZTStatusRefreshButton->setPos(180,75);
    connect(mZTStatusRefreshButton,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

    mZTLogDownloadButton = new CTextButtonWidget("Voir ZTLog");
    mZTLogDownloadButton->setParentItem(this);
    mZTLogDownloadButton->setPos(50,75);
    connect(mZTLogDownloadButton,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

    mTrainLogsDownloadButton = new CTextButtonWidget("Téléch. Passages");
    mTrainLogsDownloadButton->setParentItem(this);
    mTrainLogsDownloadButton->setPos(50,125);
    connect(mTrainLogsDownloadButton,SIGNAL(TxtButtonClicked(CTextButtonWidget*)),this,SLOT(ButtonClicked(CTextButtonWidget*)));

    mRadioButtonsGroup = new QButtonGroup();
    mViewRemoteControlRadioBtn = new QRadioButton("Voir contrôle");
    QGraphicsProxyWidget *RadioButtonsProxyWidget = new QGraphicsProxyWidget(this);
    RadioButtonsProxyWidget->setWidget(mViewRemoteControlRadioBtn);
    RadioButtonsProxyWidget->setPos(180,0);
    RadioButtonsProxyWidget->resize(150,15);
    connect(mViewRemoteControlRadioBtn,SIGNAL(toggled(bool)),this,SLOT(RemoteViewRadioButtonToggled(bool)));
    mViewRemoteTrainLogsRadioBtn = new QRadioButton("Voir passages");
    RadioButtonsProxyWidget = new QGraphicsProxyWidget(this);
    RadioButtonsProxyWidget->setWidget(mViewRemoteTrainLogsRadioBtn);
    RadioButtonsProxyWidget->setPos(180,20);
    RadioButtonsProxyWidget->resize(150,15);
    connect(mViewRemoteTrainLogsRadioBtn,SIGNAL(toggled(bool)),this,SLOT(RemoteViewRadioButtonToggled(bool)));
    mViewRemoteLiveZtRadioBtn = new QRadioButton("Voir temps réel");
    RadioButtonsProxyWidget = new QGraphicsProxyWidget(this);
    RadioButtonsProxyWidget->setWidget(mViewRemoteLiveZtRadioBtn);
    RadioButtonsProxyWidget->setPos(180,40);
    RadioButtonsProxyWidget->resize(150,15);
    connect(mViewRemoteLiveZtRadioBtn,SIGNAL(toggled(bool)),this,SLOT(RemoteViewRadioButtonToggled(bool)));
    mViewRemoteTrainLogsRadioBtn->setChecked(true);
    mRadioButtonsGroup->addButton(mViewRemoteControlRadioBtn);
    mRadioButtonsGroup->addButton(mViewRemoteTrainLogsRadioBtn);
    mRadioButtonsGroup->addButton(mViewRemoteLiveZtRadioBtn);


    mTxRxWidget = new CTxRxWidget();
    mTxRxWidget->setParentItem(this);
    mTxRxWidget->setPos(180,100);
}

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

void CNetworkCtrlPage::ButtonClicked(CTextButtonWidget *BtnPtr)
{
    if(BtnPtr == mZTStatusRefreshButton)
    {
        mProgramHandle->RefreshRemoteZTStatusRequest();
    }
    else if(BtnPtr == mZTLogDownloadButton)
    {
        mProgramHandle->DownloadZTLogRequest();
    }
    else if(BtnPtr == mTrainLogsDownloadButton)
    {
        mProgramHandle->DownloadTrainLogsRequest();
    }
}

void CNetworkCtrlPage::ConnectButtonPressed()
{
    mNetworkConnectButton->ForceDeactivation();
    mNetworkConnectButton->OverrideText("Annuler...");

    if(mIsConnecting == false)
    {
        if(mProgramHandle->ConnectToZTRequest() == RET_OK)
        {
            mIsConnecting = true;
        }
    }
    else
    {
        mProgramHandle->DisconnectZTRequest();
        mIsConnecting = false;
    }

}

void CNetworkCtrlPage::ConnectButtonReleased()
{
    mProgramHandle->DisconnectZTRequest();

}

void CNetworkCtrlPage::NetworkZTConnected(int ID)
{
    mNetworkConnectButton->ForceActivation();
    mIsConnecting = false;
}
void CNetworkCtrlPage::NetworkZTConnectionFailed(int ID)
{
    mNetworkConnectButton->ForceDeactivation();
    mIsConnecting = false;
}

void CNetworkCtrlPage::NetworkRx()
{
    mTxRxWidget->Receive();
}

void CNetworkCtrlPage::NetworkTx()
{
    mTxRxWidget->Transmit();
}

void CNetworkCtrlPage::DisableNetworkButtons()
{
    mZTStatusRefreshButton->DisableButton();
    mZTLogDownloadButton->DisableButton();
    mTrainLogsDownloadButton->DisableButton();
}

void CNetworkCtrlPage::EnableNetworkButtons()
{
    mZTStatusRefreshButton->EnableButton();
    mZTLogDownloadButton->EnableButton();
    mTrainLogsDownloadButton->EnableButton();
}

void CNetworkCtrlPage::RemoteViewRadioButtonToggled(bool state)
{

    if(mViewRemoteControlRadioBtn->isChecked())
    {
        if(mProgramHandle != 0)
            mProgramHandle->ShowRemoteControlPage();
    }
    else if(mViewRemoteTrainLogsRadioBtn->isChecked())
    {
        if(mProgramHandle != 0)
            mProgramHandle->ShowRemoteTrainLogsViewPages();
    }
    else if(mViewRemoteLiveZtRadioBtn->isChecked())
    {
        if(mProgramHandle != 0)
            mProgramHandle->ShowRemoteLiveZTPage();

    }
}

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