/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012 - 2013                                                                        *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Affiche une barre d'informations.

	Utilisé sur la page principale de la ZT.
	
*/

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

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

#include "StatusBar.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>

CStatusBar::CStatusBar(QGraphicsItem *Parent):
    mCCModbusONOFF("CC",this),
    mSEIModbusONOFF("SEI",this),
    mHistorienModbusONOFF("HIS",this)
{

	setParentItem(Parent);
	resize(SCREEN_RES_WIDTH,25);

	mZT1StatusString = "INACTIVE";
	mZT2StatusString = "INACTIVE";
	mNbTrainsPass = 10;
	mNbTriggers = 2;

    mZT1Status = mZT2Status = SB_ZT_INACTIVE_STATUS;
    mCCModbusState = SB_MODBUS_NOT_PRESENT;
    mSEIModbusState = SB_MODBUS_NOT_PRESENT;
    mHistoModbusState = SB_MODBUS_NOT_PRESENT;

    mCCModbusONOFF.setParentItem(this);
    mCCModbusONOFF.setPos(730,3);
    mCCModbusONOFF.EthOFF();
    mCCModbusONOFF.hide();

    mSEIModbusONOFF.setParentItem(this);
    mSEIModbusONOFF.setPos(765,3);
    mSEIModbusONOFF.EthOFF();
    mSEIModbusONOFF.hide();

    mHistorienModbusONOFF.setParentItem(this);
    mHistorienModbusONOFF.setPos(800,3);
    mHistorienModbusONOFF.EthOFF();
    mHistorienModbusONOFF.hide();

//	QGraphicsRectItem *temprect = new QGraphicsRectItem(this);
//	temprect->setRect(boundingRect());
//	temprect->show();
}

void CStatusBar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

	Q_UNUSED(widget)


    painter->setClipRect(option->exposedRect);

	QPen RectPen(Qt::SolidLine);
	RectPen.setWidth(2);
	painter->setPen(RectPen);
	painter->drawLine(boundingRect().topLeft(),boundingRect().topRight());

	QFont font;
	QString Text;
	QRectF TextRect = boundingRect();
	QFontMetrics FontMetrics = painter->fontMetrics();
	//font.setPixelSize(1);
	font.setPointSize(15);
	painter->setFont(font);

	painter->setPen(Qt::black);
	Text = "ZT1: ";
	painter->drawText(TextRect, Qt::AlignLeft, Text);
	TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);

    switch(mZT1Status)
    {
        case SB_ZT_ACTIVE_STATUS:
        {
            Text = "ACTIVE";
            painter->setPen(Qt::red);
            break;
        }
        case SB_ZT_INACTIVE_STATUS:
        {
            Text = "INACTIVE";
            painter->setPen(Qt::darkGreen);
            break;
        }
        case SB_ZT_NOT_PRESENT_STATUS:
        {
            Text = "ABSENTE";
            painter->setPen(Qt::black);
            break;
        }
		case SB_ZT_DISABLED_STATUS:
		{
			Text = "HORS SERVICE";
			painter->setPen(Qt::darkYellow);
			break;
		}
    }

	font.setBold(true);
	painter->drawText(TextRect,Qt::AlignLeft,Text);
	TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);


	painter->setPen(Qt::black);
	Text = "ZT2: ";
	TextRect.adjust(20,0,0,0);
	painter->drawText(TextRect, Qt::AlignLeft, Text);
	TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);
    switch(mZT2Status)
    {
        case SB_ZT_ACTIVE_STATUS:
        {
            Text = "ACTIVE";
            painter->setPen(Qt::red);
            break;
        }
        case SB_ZT_INACTIVE_STATUS:
        {
            Text = "INACTIVE";
            painter->setPen(Qt::darkGreen);
            break;
        }
        case SB_ZT_NOT_PRESENT_STATUS:
        {
            Text = "ABSENTE";
            painter->setPen(Qt::black);
            break;
        }
		case SB_ZT_DISABLED_STATUS:
		{
			Text = "HORS SERVICE";
			painter->setPen(Qt::darkYellow);
			break;
		}
    }

	font.setBold(true);
	painter->drawText(TextRect,Qt::AlignLeft,Text);
	TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);


	painter->setPen(Qt::black);
	Text.clear();
	Text.sprintf("Passages:%d",mNbTrainsPass);
	TextRect.adjust(20,0,0,0);
	painter->drawText(TextRect, Qt::AlignLeft, Text);
	TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);

	Text.clear();
	Text.sprintf("Déclenchements:%d",mNbTriggers);
	TextRect.adjust(20,0,0,0);
	painter->drawText(TextRect, Qt::AlignLeft, Text);
	TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);

    if(mCCModbusState != SB_MODBUS_NOT_PRESENT || mSEIModbusState != SB_MODBUS_NOT_PRESENT)
    {
        painter->setPen(Qt::black);
        Text.clear();
        Text.sprintf("Modbus:");
        TextRect.adjust(20,0,0,0);
        painter->drawText(TextRect, Qt::AlignLeft, Text);
        TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);

   /*     if(mCCModbusState == SB_MODBUS_CONNECTED)
        {
            painter->setPen(Qt::darkGreen);
            Text.clear();
            Text.sprintf("CC:");
            TextRect.adjust(5,0,0,0);
            painter->drawText(TextRect, Qt::AlignLeft, Text);
            TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);
        }
        else if(mCCModbusState == SB_MODBUS_DISCONNECTED)
        {
            painter->setPen(Qt::red);
            Text.clear();
            Text.sprintf("CC:");
            TextRect.adjust(5,0,0,0);
            painter->drawText(TextRect, Qt::AlignLeft, Text);
            TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);
        }

        if(mSEIModbusState == SB_MODBUS_CONNECTED)
        {
            painter->setPen(Qt::darkGreen);
            Text.clear();
            Text.sprintf("SEI:");
            TextRect.adjust(50,0,0,0);
            painter->drawText(TextRect, Qt::AlignLeft, Text);
            TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);
        }
        else if(mSEIModbusState == SB_MODBUS_DISCONNECTED)
        {
            painter->setPen(Qt::red);
            Text.clear();
            Text.sprintf("SEI:");
            TextRect.adjust(50,0,0,0);
            painter->drawText(TextRect, Qt::AlignLeft, Text);
            TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);
        }*/
    }
    else if(mCCModbusState == SB_MODBUS_NOT_PRESENT)
    {
        painter->setPen(Qt::black);
        Text.clear();
        Text.sprintf("Modbus: N/A");
        TextRect.adjust(20,0,0,0);
        painter->drawText(TextRect, Qt::AlignLeft, Text);
        TextRect.adjust(painter->fontMetrics().width(Text),0,0,0);
    }

}

unsigned int CStatusBar::SetNbPass(unsigned int Value)
{
	mNbTrainsPass = Value;
    update(boundingRect());
	return RET_OK;
}

unsigned int CStatusBar::SetNbTriggers(unsigned int Value)
{
	mNbTriggers = Value;
    update(boundingRect());
	return RET_OK;
}

unsigned int CStatusBar::SetZT1Status(unsigned int Status)
{
    mZT1Status = Status;
    update(boundingRect());
	return RET_OK;
}

unsigned int CStatusBar::SetZT2Status(unsigned int Status)
{
    mZT2Status = Status;
    update(boundingRect());
	return RET_OK;
}

unsigned int CStatusBar::SetCCModbusState(unsigned int State)
{
    mCCModbusState = State;
    if(State == SB_MODBUS_CONNECTED)
    {
        mCCModbusONOFF.EthON();
        mCCModbusONOFF.show();
    }
    else if(State == SB_MODBUS_DISCONNECTED)
    {
        mCCModbusONOFF.EthOFF();
        mCCModbusONOFF.show();
    }
    else if(State == SB_MODBUS_NOT_PRESENT)
    {
        mCCModbusONOFF.hide();
    }

    update(boundingRect());

    return RET_OK;
}
unsigned int CStatusBar::SetSEIModbusState(unsigned int State)
{
    mSEIModbusState = State;
    if(State == SB_MODBUS_CONNECTED)
    {
        mSEIModbusONOFF.EthON();
        mSEIModbusONOFF.show();
    }
    else if(State == SB_MODBUS_DISCONNECTED)
    {
        mSEIModbusONOFF.EthOFF();
        mSEIModbusONOFF.show();
    }
    else if(State == SB_MODBUS_NOT_PRESENT)
    {
        mSEIModbusONOFF.hide();
    }

    update(boundingRect());
    return RET_OK;
}

unsigned int CStatusBar::SetHistorienModbusState(unsigned int State)
{
    mHistoModbusState = State;
    if(State == SB_MODBUS_CONNECTED)
    {
        mHistorienModbusONOFF.EthON();
        mHistorienModbusONOFF.show();
    }
    else if(State == SB_MODBUS_DISCONNECTED)
    {
        mHistorienModbusONOFF.EthOFF();
        mHistorienModbusONOFF.show();
    }
    else if(State == SB_MODBUS_NOT_PRESENT)
    {
        mHistorienModbusONOFF.hide();
    }

    update(boundingRect());
    return RET_OK;
}
