/*******************************************************************************
*                                                                              *
* 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 ZT1
	
*/

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

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

#include "ZT1StatsZone.h"


CZT1StatsZone::CZT1StatsZone(QGraphicsItem *Parent)
{
	setParentItem(Parent);

    mTrainCompositionText = 0;

    mModbusEnabled = false;



}

unsigned int CZT1StatsZone::Init(bool ModbusEnabled)
{
    mModbusEnabled = ModbusEnabled;
    int textpos = 12;
    QFont font;
    font.setPointSize(15);

    mTrainSpeedText = new QGraphicsTextItem(this);
    mTrainSpeedText->setPos(0,textpos);
//	mTrainSpeedText->setPlainText("Vitesse du train: 0");
    mTrainSpeedText->setFont(font);

    textpos += 22;
    mBogieCountText = new QGraphicsTextItem(this);
    mBogieCountText->setPos(0,textpos);
//	mBogieCountText->setPlainText("Compte Bogie: 0");
    mBogieCountText->setFont(font);

    textpos += 22;
    mS1CountText = new QGraphicsTextItem(this);
    mS1CountText->setPos(0,textpos);
//	mS1CountText->setPlainText("Compte S1: 0");
    mS1CountText->setFont(font);

    textpos += 22;
    mS2CountText = new QGraphicsTextItem(this);
    mS2CountText->setPos(0,textpos);
//	mS2CountText->setPlainText("Compte S2: 0");
    mS2CountText->setFont(font);

    textpos += 22;
    mFNCountText = new QGraphicsTextItem(this);
    mFNCountText->setPos(0,textpos);
//	mFNCountText->setPlainText("Compte FN: 0");
    mFNCountText->setFont(font);

    textpos += 22;
    mActualRankText = new QGraphicsTextItem(this);
    mActualRankText->setPos(0,textpos);
//	mActualRankText->setPlainText("Rang: 0");
    mActualRankText->setFont(font);

    textpos += 22;
    mTrainTypeText = new QGraphicsTextItem(this);
    mTrainTypeText->setPos(0,textpos);
//	mTrainTypeText->setPlainText("Train Détecté: Aucun");
    mTrainTypeText->setFont(font);

    if(mModbusEnabled == true)
    {
        textpos += 22;
        mTrainCompositionText = new QGraphicsTextItem(this);
        mTrainCompositionText->setPos(0,textpos);
        mTrainCompositionText->setFont(font);
    }

    textpos += 22;
    mPIValueText = new QGraphicsTextItem(this);
    mPIValueText->setPos(0,textpos);
//	mTrainTypeText->setPlainText("Train Détecté: Aucun");
    mPIValueText->setFont(font);

    textpos += 22;
    mPEValueText = new QGraphicsTextItem(this);
    mPEValueText->setPos(0,textpos);
    //	mTrainTypeText->setPlainText("Train Détecté: Aucun");
    mPEValueText->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 CZT1StatsZone::UpdateStats(CZT1ThreadData *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(mS2Count != DataPtr->mS2Count)
	{
		mS2Count = DataPtr->mS2Count;
		temp.clear();
		temp.sprintf("Compte S2: %d",mS2Count);
		mS2CountText->setPlainText(temp);
	}

	if(mFNCount != DataPtr->mFNCount)
	{
		mFNCount = DataPtr->mFNCount;
		temp.clear();
		temp.sprintf("Compte FN: %d",mFNCount);
		mFNCountText->setPlainText(temp);
	}

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


	if(mTrainSpeed != DataPtr->mTrainSpeed)
	{
		mTrainSpeed = DataPtr->mTrainSpeed;
		temp.clear();
		temp.sprintf("Vitesse du train: %.2f Mph",mTrainSpeed);
		mTrainSpeedText->setPlainText(temp);
	}

    if(mModbusEnabled == false)
    {
        if(mTrainType != DataPtr->mTrainType)
        {
            mTrainType = DataPtr->mTrainType;
            temp.clear();
            temp = "Train détecté: ";
            temp += CZTData::GetTrainTypeString(mTrainType);
            mTrainTypeText->setPlainText(temp);
        }
    }
    if(mPIValue != DataPtr->mPGIntValue)
    {
        mPIValue = DataPtr->mPGIntValue;
        temp.clear();
        temp = "Train détecté: ";
        temp.sprintf("Pneu Guidage Int.: %i",mPIValue);
        mPIValueText->setPlainText(temp);
    }
    if(mPEValue != DataPtr->mPGExtValue)
    {
        mPEValue = DataPtr->mPGExtValue;
        temp.clear();
        temp = "Train détecté: ";
        temp.sprintf("Pneu Guidage Ext.: %i",mPEValue);
        mPEValueText->setPlainText(temp);
    }


	return RET_OK;


}

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

unsigned int CZT1StatsZone::Reset()
{
	mBogieCount = mS1Count = mS2Count = mFNCount = mActualRank = mActualBogie = mTrainType = 0;
	mTrainSpeed = 0.0;
	mPEValue = 0;
	mPIValue = 0;

	mTrainSpeedText->setPlainText("Vitesse du train: 0");
	mBogieCountText->setPlainText("Compte Bogie: 0");
	mS1CountText->setPlainText("Compte S1: 0");
	mS2CountText->setPlainText("Compte S2: 0");
	mFNCountText->setPlainText("Compte FN: 0");
	mActualRankText->setPlainText("Rang: 0");
    mTrainTypeText->setPlainText("Type de train: Inconnu");
    if(mTrainCompositionText != 0)
    {
        mTrainCompositionText->setPlainText("Composition : Inconnue");
    }
    mPIValueText->setPlainText("Pneu Guidage Int.: ?");
    mPEValueText->setPlainText("Pneu Guidage Ext.: ?");

	return RET_OK;
}

unsigned int CZT1StatsZone::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
        mTrainCompositionText->setPlainText("Composition: Inconnue");
    }
    else if((TrainComposition[0] == 0) &&
            (TrainComposition[1] == 0) &&
            (TrainComposition[2] == 0))
    {
        //Composition invalide...
        mTrainCompositionText->setPlainText("Composition: Inconnue");
    }
    else
    {
        mTrainCompositionText->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;
}
