/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012 - 2013                                                                        *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Élément graphique qui affiche une règle qui indique le décompte des rangs
	du train dans le temps.  Ce qui permet de savoir à quel rang un événement
	est survenu lors de la consultation d'un passage de train.


*/

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

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

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

#include "RankRulerWidget.h"
#include <QPainter>
#include <QTextStream>
#include <QStyleOptionGraphicsItem>

CRankRulerWidget::~CRankRulerWidget()
{
	delete mRulerPixmap;
}

CRankRulerWidget::CRankRulerWidget(qreal RulerPixelWidth, QGraphicsItem *Parent)
{
    setParentItem(Parent);
    mPixelWidth = RulerPixelWidth;
    mLabel = new QGraphicsTextItem(this);

    mRulerPixmap = new QPixmap(boundingRect().width(),boundingRect().height());
}

void CRankRulerWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
{
	Q_UNUSED(event)

    delete mRulerPixmap;
    mRulerPixmap = new QPixmap(boundingRect().width(),boundingRect().height());
    Render();
}

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

    painter->setClipRect(option->exposedRect);
    painter->drawPixmap(0,0,*mRulerPixmap);

//    if(mRankItems.isEmpty())
//        return;
//	QFont font;
//	font.setPixelSize(12);
//	painter->setFont(font);
//    qreal pitch = mPixelWidth/(mEndTime - mStartTime);  //pixel/ns

//    int StartIndex = 0,EndIndex = 0;
//    bool StartFound = false;

//    for(int i = 0; i < mRankItems.size(); i++)
//    {
//        if(StartFound == false)
//        {
//            if(mStartTime <= mRankItems.at(i).mStartTime)
//            {
//                StartIndex = i;
//                StartFound = true;
//            }
//        }

//        if(mRankItems.at(i).mEndTime <= mEndTime)
//        {
//            EndIndex = i;
//        }
//    }

//    if(StartIndex > 0)
//    {
//        if(mStartTime < mRankItems.at(StartIndex).mStartTime)
//        {
//            QRectF LabelRect;
//            qreal StartPixel = 0;//mStartTime * pitch;
//            qreal EndPixel = (mRankItems.at(StartIndex).mStartTime - mStartTime) * pitch;
//            qreal Height = boundingRect().height();

//            LabelRect.setRect(StartPixel,0,(EndPixel-StartPixel),Height);
//            painter->drawLine(StartPixel,0,StartPixel,Height);
//            painter->drawLine(EndPixel,0,EndPixel,Height);
//            painter->drawText(LabelRect,mRankItems.at(StartIndex-1).mLabel,Qt::AlignHCenter | Qt::AlignVCenter);

//        }
//    }

//    for(int i = StartIndex; i <= EndIndex; i++)
//    {
//        QRectF LabelRect;
//        qreal StartPixel = (mRankItems.at(i).mStartTime - mStartTime) * pitch;

//        qreal EndPixel;
//        if(mRankItems.at(i).mEndTime > mEndTime)
//            EndPixel = (mEndTime - mStartTime) * pitch;
//        else
//            EndPixel = (mRankItems.at(i).mEndTime - mStartTime) * pitch;

//        qreal Height = boundingRect().height();

//        LabelRect.setRect(StartPixel,0,(EndPixel-StartPixel),Height);
//        painter->drawLine(StartPixel,0,StartPixel,Height);
//        painter->drawLine(EndPixel,0,EndPixel,Height);
//        painter->drawText(LabelRect,mRankItems.at(i).mLabel,Qt::AlignHCenter | Qt::AlignVCenter);
//    }
//    if(EndIndex < mRankItems.size()-1)
//    {
//        if(mEndTime > mRankItems.at(EndIndex).mStartTime)
//        {
//            QRectF LabelRect;
//            qreal StartPixel =  (mRankItems.at(EndIndex).mEndTime - mStartTime) * pitch;
//            qreal EndPixel = (mEndTime - mStartTime) * pitch;
//            qreal Height = boundingRect().height();

//            LabelRect.setRect(StartPixel,0,(EndPixel-StartPixel),Height);
//            painter->drawLine(StartPixel,0,StartPixel,Height);
//            painter->drawLine(EndPixel,0,EndPixel,Height);
////            if(EndIndex == StartIndex)
////                painter->drawText(LabelRect,mRankItems.at(EndIndex).mLabel,Qt::AlignHCenter | Qt::AlignVCenter);
////            else
//                painter->drawText(LabelRect,mRankItems.at(EndIndex+1).mLabel,Qt::AlignHCenter | Qt::AlignVCenter);

//        }
//    }
}

void CRankRulerWidget::Render()
{

    if(mRankItems.isEmpty())
        return;

    mRulerPixmap->fill(Qt::white);
    QPainter *painter = new QPainter(mRulerPixmap);

    QFont font;
    font.setPixelSize(12);
    painter->setFont(font);
    qreal pitch = mPixelWidth/(mEndTime - mStartTime);  //pixel/ns



    int StartIndex = 0,EndIndex = 0;
    bool StartFound = false;

    for(int i = 0; i < mRankItems.size(); i++)
    {
        if(StartFound == false)
        {
            if(mStartTime <= mRankItems.at(i).mStartTime)
            {
                StartIndex = i;
                StartFound = true;
            }
        }

        if(mRankItems.at(i).mEndTime <= mEndTime)
        {
            EndIndex = i;
        }
    }

    if(StartIndex > 0)
    {
        if(mStartTime < mRankItems.at(StartIndex).mStartTime)
        {
            QRectF LabelRect;
            qreal StartPixel = 0;//mStartTime * pitch;
            qreal EndPixel = (mRankItems.at(StartIndex).mStartTime - mStartTime) * pitch;
            qreal Height = boundingRect().height();

            LabelRect.setRect(StartPixel,0,(EndPixel-StartPixel),Height);
            painter->drawLine(StartPixel,0,StartPixel,Height);
            painter->drawLine(EndPixel,0,EndPixel,Height);
            painter->drawText(LabelRect,mRankItems.at(StartIndex-1).mLabel,Qt::AlignHCenter | Qt::AlignVCenter);

        }
    }

    for(int i = StartIndex; i <= EndIndex; i++)
    {
        QRectF LabelRect;
        qreal StartPixel = (mRankItems.at(i).mStartTime - mStartTime) * pitch;

        qreal EndPixel;
        if(mRankItems.at(i).mEndTime > mEndTime)
            EndPixel = (mEndTime - mStartTime) * pitch;
        else
            EndPixel = (mRankItems.at(i).mEndTime - mStartTime) * pitch;

        qreal Height = boundingRect().height();

        LabelRect.setRect(StartPixel,0,(EndPixel-StartPixel),Height);
        painter->drawLine(StartPixel,0,StartPixel,Height);
        painter->drawLine(EndPixel,0,EndPixel,Height);
        painter->drawText(LabelRect,mRankItems.at(i).mLabel,Qt::AlignHCenter | Qt::AlignVCenter);
    }
    if(EndIndex < mRankItems.size()-1)
    {
        if(mEndTime > mRankItems.at(EndIndex).mStartTime)
        {
            QRectF LabelRect;
            qreal StartPixel =  (mRankItems.at(EndIndex).mEndTime - mStartTime) * pitch;
            qreal EndPixel = (mEndTime - mStartTime) * pitch;
            qreal Height = boundingRect().height();

            LabelRect.setRect(StartPixel,0,(EndPixel-StartPixel),Height);
            painter->drawLine(StartPixel,0,StartPixel,Height);
            painter->drawLine(EndPixel,0,EndPixel,Height);
//            if(EndIndex == StartIndex)
//                painter->drawText(LabelRect,mRankItems.at(EndIndex).mLabel,Qt::AlignHCenter | Qt::AlignVCenter);
//            else
                painter->drawText(LabelRect,mRankItems.at(EndIndex+1).mLabel,Qt::AlignHCenter | Qt::AlignVCenter);

        }
    }

    delete painter;
}


unsigned int CRankRulerWidget::SetRange(quint64 StartTime, quint64 EndTime)
{
    if(StartTime > EndTime)
        return false;

    if(mRankItems.isEmpty())
        return false;


    mStartTime = StartTime;
    mEndTime = EndTime;
    mTimeOffset = mRankItems.first().mStartTime;

    Render();

    update();

    return RET_OK;
}

unsigned int CRankRulerWidget::AddRankItem(QString Rank, quint64 StartTime, quint64 EndTime)
{
    CRankWidgetItem NewItem;
    NewItem.mLabel = Rank;
    NewItem.mStartTime = StartTime;
    NewItem.mEndTime = EndTime;

    mRankItems.append(NewItem);
    return RET_OK;
}

void CRankRulerWidget::SetLabel(QString Label, int Offset)
{
    QFont font;
    font.setBold(true);
    mLabel->setFont(font);
    mLabel->setPlainText(Label);
    mLabel->setPos(-Offset,boundingRect().height()/2 - mLabel->boundingRect().height()/2);
}

void CRankRulerWidget::ClearRuler()
{
	mRankItems.clear();
}
