#include "customplotitem.h"
#include "qcustomplot.h"
#include <QDebug>

CustomPlotItem::CustomPlotItem( QQuickItem* parent ) : QQuickPaintedItem( parent )
    , m_CustomPlot( nullptr )
{
    setFlag( QQuickItem::ItemHasContents, true );
    setAcceptedMouseButtons( Qt::AllButtons );

    connect( this, &QQuickPaintedItem::widthChanged, this, &CustomPlotItem::updateCustomPlotSize );
    connect( this, &QQuickPaintedItem::heightChanged, this, &CustomPlotItem::updateCustomPlotSize );
    initCustomPlot();


}

void CustomPlotItem::paint(QPainter *painter)
{
    if(m_CustomPlot){
        QPixmap picture (boundingRect().size().toSize());
        QCPPainter qcpPainter (&picture);

        m_CustomPlot->toPainter(&qcpPainter);

        painter->drawPixmap(QPoint(), picture);
    }
}


CustomPlotItem::~CustomPlotItem()
{
    delete m_CustomPlot;
    m_CustomPlot = nullptr;

}

/*
 * Initialiyation of QCustomPlot
 * */
void CustomPlotItem::initCustomPlot()
{
    m_CustomPlot = new QCustomPlot();

    updateCustomPlotSize();

    //m_CustomPlot->axisRect()->setupFullAxesBox();
    m_CustomPlot->xAxis->setRange(-1,10.25);
    m_CustomPlot->xAxis->setVisible(false);
    m_CustomPlot->xAxis2->setVisible(false);
    m_CustomPlot->xAxis->grid()->setSubGridVisible(false);
    m_CustomPlot->yAxis->setRange(-1,12);
    m_CustomPlot->yAxis->setVisible(false);
    m_CustomPlot->yAxis2->setVisible(false);
    m_CustomPlot->yAxis->grid()->setSubGridVisible(false);

//pudorys
    drawLine(0,0,9.25,0,0);
    drawLine(9.25,0,9.25,11,1);
    drawLine(3.75,11,9.25,11,2);
    drawLine(3.75,9,3.75,11,3);
    drawLine(0,9,3.75,9,4);
    drawLine(0,0,0,9,5);
    drawLine(0,3.75,4.25,3.75,6);
    drawLine(5.5,3.75,9.25,3.75,7);
    drawLine(5.5,7.75,9.25,7.75,8); //nosny a obvodovy
    drawLine(5.5,0,5.5,2.25,9);
    drawLine(5.5,3.05,5.5,3.75,10);
    drawLine(5.5,3.75,5.5,4.75,11);
    drawLine(5.5,5.65,5.5,7.9,12);
    drawLine(5.5,8.8,5.5,9,13);
    drawLine(5.2,9,9.25,9,14);
    drawLine(3.75,9,4.3,9,15);
    drawLine(0,6.2,0.5,6.2,16);
    drawLine(1.4,6.2,4.25,6.2,17);
    drawLine(1.8,6.2,1.8,9,18);
    drawLine(4.25,3.75,4.25,4.25,19);
    drawLine(4.25,5.15,4.25,7,20);
    drawLine(4.25,7.9,4.25,9,21);

    m_CustomPlot->replot();
    update();
    //////////////////
}


/*
 * [x0,y0] start point, [x1,y1] end point
 * */
void CustomPlotItem::drawLine(double x0, double y0, double x1, double y1, int graphNumber)
{
    m_CustomPlot->addGraph();
    QVector<double> x(100), y(100);
    if(y0 == y1) //horizontal
    {
        for (int i=0; i<100; ++i)
        {
        x[i] = i/(100/(x1-x0)) + x0;
        y[i] = y0;
        }
    }
    if(x0 == x1)    //vertical
    {
        for (int i=0; i<100; ++i)
        {
        x[i] = x0;
        y[i] = i/(100/(y1-y0)) + y0;
        }
    }
    QPen pen;
    pen.setColor(Qt::black);
    if(graphNumber < 9)
        pen.setWidth(5);
    else
        pen.setWidth(2);

    m_CustomPlot->graph(graphNumber)->setPen(pen);
    m_CustomPlot->graph(graphNumber)->setData(x, y);
}

/*
 * displays the circle with different color choosen by recieved time
 * x and y coordinates, radius, sequence
 * */
void CustomPlotItem::showCircle(double x, double y, double radius, int sequence)
{
    // generate the curve data points:
    const int pointCount = 500;
    QVector<QCPCurveData> dataCircle(pointCount);
    for (int i=0; i<pointCount; ++i)
    {
      double phi = i/(double)(pointCount-1)*2*M_PI;
      dataCircle[i] = QCPCurveData(i, x + radius*qCos(phi), y + radius*qSin(phi));
    }
    // pass the data to the curves; we know t (i in loop above) is ascending, so set alreadySorted=true (saves an extra internal sort):

    if(sequence == 1)
    {
        int plotabbles = m_CustomPlot->QCustomPlot::plottableCount();
        if(plotabbles > 22)
        {
            while(plotabbles > 22)
                m_CustomPlot->QCustomPlot::removePlottable(--plotabbles);
        }
        QCPCurve *circle0 =  new QCPCurve(m_CustomPlot->xAxis, m_CustomPlot->yAxis);
        circle0->data()->set(dataCircle, true);
        circle0->setPen(QPen(QColor(0, 128, 255, 20)));
        circle0->setBrush(QBrush(QColor(0, 128, 255, 120)));
    }
    if(sequence == 2)
    {
        QCPCurve *circle1 =  new QCPCurve(m_CustomPlot->xAxis, m_CustomPlot->yAxis);
        circle1->data()->set(dataCircle, true);
        circle1->setPen(QPen(QColor(0, 128, 255, 20)));
        circle1->setBrush(QBrush(QColor(0, 128, 255, 80)));
    }
    if(sequence == 3)
    {
        QCPCurve *circle2 =  new QCPCurve(m_CustomPlot->xAxis, m_CustomPlot->yAxis);
        circle2->data()->set(dataCircle, true);
        circle2->setPen(QPen(QColor(0, 128, 255, 20)));
        circle2->setBrush(QBrush(QColor(0, 128, 255, 40)));
    }
    m_CustomPlot->replot();
    update();
}


void CustomPlotItem::updateCustomPlotSize()
{
    if (m_CustomPlot)
    {
        m_CustomPlot->setGeometry(0, 0, (int)width(), (int)height());
        m_CustomPlot->setViewport(QRect(0, 0, (int)width(), (int)height()));
    }
}

void CustomPlotItem::onCustomReplot()
{
    qDebug() << Q_FUNC_INFO;
    update();
}
