/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012 - 2013                                                                        *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Zone de texte qui affiche les statistiques de passage d'un train dans la ZT2.
	
*/

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

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






#include "ZT2StatsZone.h"

CZT2StatsZone::CZT2StatsZone(QGraphicsItem *Parent)
{
	setParentItem(Parent);
    mTrainCompoText = mTrainTypeText = 0;
    mTrainType = TRAIN_TYPE_UNKNOWN;
}

unsigned int CZT2StatsZone::Init(bool ModbusEnabled)
{
    mModbusEnabled = ModbusEnabled;


    QFont font;
    font.setPointSize(15);
    int TextPos = 22;

    mBogieCountText = new QGraphicsTextItem(this);
    mBogieCountText->setPos(0,TextPos);
    mBogieCountText->setFont(font);

    TextPos += 22;
    mS1CountText = new QGraphicsTextItem(this);
    mS1CountText->setPos(0,TextPos);
    mS1CountText->setFont(font);

    TextPos += 22;
    mActualRankText = new QGraphicsTextItem(this);
    mActualRankText->setPos(0,TextPos);
    mActualRankText->setFont(font);

    if(ModbusEnabled)
    {
        TextPos += 22;
        mTrainCompoText = new QGraphicsTextItem(this);
        mTrainCompoText->setPos(0,TextPos);
        mTrainCompoText->setFont(font);

        TextPos += 22;
        mTrainTypeText = new QGraphicsTextItem(this);
        mTrainTypeText->setPos(0,TextPos);
        mTrainTypeText->setFont(font);
    }

    TextPos += 22;
    mLastActivationDateTime = new QGraphicsTextItem(this);
    mLastActivationDateTime->setPos(0,TextPos);
    mLastActivationDateTime->setFont(font);
    mLastActivationDateTime->setPlainText("Dernier passage : ");



    Reset();

    return RET_OK;
}

unsigned int CZT2StatsZone::SetLastActivationDateTime()
{
	QString txt = "Dernier passage: ";
	txt += QDateTime::currentDateTime().toString("yyyy/MM/dd - hh:mm:ss");
	mLastActivationDateTime->setPlainText(txt);
	return RET_OK;
}

unsigned int CZT2StatsZone::UpdateStats(CZT2ThreadData *DataPtr)
{
	QString temp;

	if(mBogieCount != DataPtr->mBogie)
	{
		mBogieCount = DataPtr->mBogie;
		temp.clear();
		temp.sprintf("Compte Bogie: %d",mBogieCount);
		mBogieCountText->setPlainText(temp);
	}

	if(mS1Count != DataPtr->mS1Count)
	{
		mS1Count = DataPtr->mS1Count;
		temp.clear();
		temp.sprintf("Compte S1: %d",mS1Count);
		mS1CountText->setPlainText(temp);
	}

	if(mActualRank != DataPtr->mRank)
	{
		mActualRank = DataPtr->mRank;
		temp.clear();
		temp.sprintf("Rang: %d",mActualRank);
		mActualRankText->setPlainText(temp);
	}


	return RET_OK;


}

unsigned int CZT2StatsZone::Reset()
{
    mBogieCount = mS1Count = mActualRank = 0;

    mBogieCountText->setPlainText("Compte Bogie: 0");
	mS1CountText->setPlainText("Compte S1: 0");
	mActualRankText->setPlainText("Rang: 0");
    if(mTrainTypeText != 0)
    {
        mTrainTypeText->setPlainText("Type de train: Inconnu");
    }
    if(mTrainCompoText != 0)
    {
        mTrainCompoText->setPlainText("Composition du train: Inconnue");
    }


	return RET_OK;
}

unsigned int CZT2StatsZone::SetTrainParameters(int TrainType, QList<qint16> TrainComposition)
{
  //  if(mTrainType != (unsigned)TrainType)
    {
        mTrainType = TrainType;
        mTrainTypeText->setPlainText(QString("Type de train : %1").arg(CZTData::GetTrainTypeString(mTrainType)));
    }

    if(TrainComposition.size() != 3)
    {
        //Invalid composition
        mTrainCompoText->setPlainText("Composition: Inconnue");
    }
    else if((TrainComposition[0] == 0) &&
            (TrainComposition[1] == 0) &&
            (TrainComposition[2] == 0))
    {
        //Composition invalide...
        mTrainCompoText->setPlainText("Composition: Inconnue");
    }
    else
    {
        mTrainCompoText->setPlainText(QString("Composition : %1-%2-%3").arg((int)TrainComposition[0],3,10,QChar('0')).arg(TrainComposition[1],3,10,QChar('0')).arg(TrainComposition[2],3,10,QChar('0')));
    }

    return RET_OK;
}
