#include "timeLineWidget.h"
#include "timeLineItem.h"

#include <QGraphicsScene>
#include <QColor>
#include <QDateTime>
#include <QWheelEvent>

class TimeLineWidgetPrivate
{
public:
    TimeLineWidgetPrivate();

    QGraphicsScene* m_pScene;
    QColor          m_background_color;

    bool            m_bMousePress;
    QPoint          m_ptMousePress;
    QDateTime       m_timeMousePress;

    TimeLine::TimeLineItem*   m_timeLineItem;
};
TimeLineWidgetPrivate::TimeLineWidgetPrivate()
{
    m_bMousePress = false;
}

TimeLineWidget::TimeLineWidget(QWidget *parent)
    : QGraphicsView(parent)
    , d_ptr(new TimeLineWidgetPrivate)
{
    d_ptr->m_pScene = new QGraphicsScene;
    setScene(d_ptr->m_pScene);
    d_ptr->m_timeLineItem = new TimeLine::TimeLineItem();
    d_ptr->m_pScene->addItem(d_ptr->m_timeLineItem);

    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setFrameShape(NoFrame);
}

TimeLineWidget::~TimeLineWidget()
{
    delete d_ptr;
}

void TimeLineWidget::resizeEvent(QResizeEvent* e)
{
    d_ptr->m_pScene->setSceneRect(-width() / 2, -height() / 2, width(), height());
    d_ptr->m_timeLineItem->updateBoundingRect(childrenRect());
}
void TimeLineWidget::mousePressEvent(QMouseEvent* e)
{
    if (Qt::LeftButton == e->button())
    {
        d_ptr->m_bMousePress = true;
        d_ptr->m_ptMousePress = e->pos();
        d_ptr->m_timeMousePress = d_ptr->m_timeLineItem->time();
        setCursor(Qt::ClosedHandCursor);
    }
    else
        QGraphicsView::mousePressEvent(e);
}
void TimeLineWidget::mouseMoveEvent(QMouseEvent* e)
{
    if(d_ptr->m_bMousePress)
    {
        int offsetX = e->pos().x() - d_ptr->m_ptMousePress.x();
        qint64 offsetValue = d_ptr->m_timeLineItem->getOffsetTimeValue(offsetX);
        offsetValue = qAbs(offsetValue);
        if(offsetX < 0)
            offsetValue = -offsetValue;

        QDateTime dateTime = d_ptr->m_timeMousePress.addMSecs(-offsetValue);
        d_ptr->m_timeLineItem->setTime(dateTime);
        d_ptr->m_ptMousePress = e->pos();
        d_ptr->m_timeMousePress = d_ptr->m_timeLineItem->time();

        setDisplayState(historical);
        emit viewHistoricalData(dateTime);
    }
    else
        QGraphicsView::mouseMoveEvent(e);
}
void TimeLineWidget::mouseReleaseEvent(QMouseEvent* e)
{
    if (Qt::LeftButton == e->button())
    {
        d_ptr->m_bMousePress = false;
        setCursor(Qt::ArrowCursor);
    }
    else
        QGraphicsView::mousePressEvent(e);
}
void TimeLineWidget::wheelEvent(QWheelEvent* e)
{
    if (e->angleDelta().y() > 0)
        d_ptr->m_timeLineItem->zoomIn();
    else
        d_ptr->m_timeLineItem->zoomOut();

    emit timeScaleUnitChanged(d_ptr->m_timeLineItem->timeUnit());
}

void TimeLineWidget::updateAll()
{
    d_ptr->m_pScene->update();
}

void TimeLineWidget::setBackground(QColor color)
{
    d_ptr->m_background_color = color;
    setBackgroundBrush(QBrush(color, Qt::SolidPattern));
}

void TimeLineWidget::setTimelineColor(QColor color)
{
    d_ptr->m_timeLineItem->setLineColor(color);
}

void TimeLineWidget::setTimeScaleSize(int size)
{
    d_ptr->m_timeLineItem->setScaleSize(size);
}

void TimeLineWidget::setTimeScaleUnit(TimeUnit unit)
{
    d_ptr->m_timeLineItem->setTimeUnit(unit);
}

void TimeLineWidget::setDateTime(const QDateTime& dateTime)
{
    d_ptr->m_timeLineItem->setTime(dateTime);
}

void TimeLineWidget::setDisplayState(DateTimeWidgetState state)
{
    d_ptr->m_timeLineItem->setState(state);
}

void TimeLineWidget::syncTimeUnit()
{
    emit timeScaleUnitChanged(d_ptr->m_timeLineItem->timeUnit());
}

