#include <QDebug>
#include "mycustomwidget.h"
#include <QPainter>
#include <QPaintEvent>
#include <QApplication>
#include "global_defines.h"
#include <QElapsedTimer>

#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif

#include <math.h>

MyCustomWidget::MyCustomWidget(QWidget *parent) :
    QWidget(parent),
    m_buffer(size()),
    m_backgroundColor(Qt::white),
    m_hoehe(0.5),
    m_laenge(2),
    m_anzahl(5),
    m_time(0),
    x(0),
    y(0),
    m_timerId(0),
    m_measureTimer(),
    m_maxTime(0),
    m_numOld(0),
    m_isActiveDraw(false),
    m_scaleFactorX(0),
    m_scaleFactorY(0),
    m_pointData(),
    m_countPoins(0)
{
    createRects();
    draw();
    //calcInit();
    //calc();
    //m_timerId = startTimer(100);
}

void MyCustomWidget::replot(WidgetData &data)
{
    QElapsedTimer timer;
    timer.start();
    m_pointData.clear();
    m_pointData.resize(data.point.size());
    for(unsigned int i=0; i<data.point.size(); ++i){
        m_pointData[i] = QPointF(m_baseRect.left() + data.point[i].x()*m_scaleFactorX, m_baseRect.top() + m_baseRect.height() - data.point[i].y()*m_scaleFactorY);
    }
//    qDebug() << "time needed: " << timer.elapsed() << " [ms]";
    draw();
    update();
}

void MyCustomWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawPixmap(0, 0, m_buffer);
    painter.drawRect(this->rect().adjusted(1, 1, -1, -1));
}

void MyCustomWidget::resizeEvent(QResizeEvent *event)
{
    // resize and repaint the buffer:
    m_buffer = QPixmap(event->size());
    m_baseRect.setTopLeft(QPointF(rect().width()/2-event->size().width()/4, rect().height()/2-event->size().height()/4));
    m_baseRect.setSize(QSize(event->size().width()/2, event->size().height()/2));

    m_scaleFactorX=m_baseRect.width()/m_laenge;
    m_scaleFactorY=m_baseRect.height()/m_hoehe;

    //calcInit();
    createRects();
    drawStatic();
    draw();
//    mViewport = rect();
//    updateAxisRect();
    //    replot();
}

void MyCustomWidget::mousePressEvent(QMouseEvent *event)
{
    m_backgroundColor = QColor(Qt::yellow);
    m_mousePoint= event->pos();
    draw();
    update();
    event->accept();
}

void MyCustomWidget::mouseReleaseEvent(QMouseEvent *event)
{
    m_backgroundColor = QColor(Qt::white);
    draw();
    update();
    event->accept();
}


void MyCustomWidget::draw()
{
    //calc();
    return;
    QPainter painter;

    if(painter.begin(&m_buffer)){
        painter.setRenderHint(QPainter::Antialiasing);
        m_isActiveDraw=true;
        painter.setPen(QPen(Qt::darkGreen, 2));
        painter.setBrush(Qt::white);
        painter.drawRect(m_baseRect);

        QElapsedTimer timer;
        timer.start();
        painter.setPen(QPen(Qt::darkBlue, 1));
        if(m_pointData.size())
            painter.drawPoints(&m_pointData[0], m_pointData.size());

//        qDebug() << "time needed draw: " << timer.elapsed() << " [ms]";
    }
    m_isActiveDraw=false;
    painter.end();

}

void MyCustomWidget::drawStatic()
{
    QPainter painter;

    if(painter.begin(&m_buffer)){
        painter.drawPixmap(m_buffer.rect(), QPixmap(QString::fromUtf8(":/ressources/Messedemo_Gehaeuse.png")));
        painter.translate(0, m_rects[0].height()/2-70);
//        for(int i=0; i<m_rects.size(); ++i){
//            painter.drawArc(m_rects[i], 180*16, -180*16);
//        }
        painter.resetTransform();
        painter.drawPixmap(QRectF(m_rects[0].left()+12-m_rects[0].width(), rect().height()/2.0-90, 300, 340), QPixmap(QString::fromUtf8(":/ressources/Messedemo_Achse.png")), m_buffer.rect());
    }
    painter.end();
}

void MyCustomWidget::calc()
{
//    double t=0;
//    double offset=0;
//    if(m_time>t_f){
//        double factor = floor(m_time/t_f);
//        if(factor>=m_anzahl){
//            //killTimer(m_timerId);
//            offset=0;
//            m_time=0;
//        }
//        t = m_time-(t_f*factor);
//        offset = v_x*t_f*factor;
//    }else{
//        t = m_time;
//    }
//    x = v_x*t+offset;
//    y = -0.5*g*t*t+v_y*t;

}

void MyCustomWidget::calcInit()
{
//    Q_ASSERT(m_hoehe>0.0);
//    Q_ASSERT(m_anzahl>0.0);

//    double s_x = m_laenge/m_anzahl;
//    double v_y = sqrt(2.0*g*m_hoehe);
//    Q_ASSERT(v_y>0.0);

//    v_x = s_x/(2.0*v_y)*g;
//    t_0 = v_y/g;
//    t_f = 2.0*t_0;

//    double alpha = fabs(-arcusTangens((v_x), (v_y)));
//    double diagonale = sqrt(v_x*v_x+v_y*v_y);
//    double diagonaleMax = sqrt(rect().width()*rect().width()+rect().height()*rect().height()*1.0)/2;
//    double alphaB = alpha*M_PI/180.0;
//    double v0 = v_x/(cos(alphaB));
//    double x_max=v0*v0*sin(2*alphaB)/(9.81);
//    double y_max=v0*v0*sin(alphaB)*sin(alphaB)/(2.0*9.81);

    //double alpha = atan(v_y/v_x)*180/M_PI;
}

double MyCustomWidget::arcusTangens(double x, double y)
{
    double result=0;
    if(x>0){
        result = atan(y/x);
    }else if(x<0 && y>0){
        result = atan(y/x)+M_PI;
    }else if(x<0 && y<0){
        result = atan(y/x)/*-M_PI*/;
    }else if(x==0 && y>0){
        result = M_PI/2;
    }else if(x==0 && y<0){
        result = -M_PI/2;
    }else{
        result = 0;
    }

    return result * 180.0/M_PI;
}

void MyCustomWidget::setHoehe(double value)
{
    Q_ASSERT(value > 0.0);
    m_hoehe=value;
    //calcInit();
    drawStatic();
    draw();
    update();
}

void MyCustomWidget::setLaenge(double value)
{
    Q_ASSERT(value > 0.0);
    m_laenge=value;
    //calcInit();
    drawStatic();
    draw();
    update();
}

void MyCustomWidget::setAnzahl(int value)
{

    m_numOld=0;
    Q_ASSERT(value > 0);
    m_anzahl=value;
    //calcInit();
    createRects();
    drawStatic();
    draw();
    update();
}

void MyCustomWidget::resetMax()
{
    m_maxTime=0;
}

void MyCustomWidget::timerEvent(QTimerEvent *event)
{
    m_time += 0.1;
    draw();
    update();
    QWidget::timerEvent(event);
}


void MyCustomWidget::createRects()
{
    QRectF paintRect = rect().adjusted(224, 13, -160, -371);
    m_rects.clear();
    for(int i=0; i<m_anzahl; ++i){
        QRectF rect = QRectF(paintRect.x()+paintRect.width()/m_anzahl*i, paintRect.y(), paintRect.width()/m_anzahl, paintRect.height());
        m_rects.append(rect);
    }
}

void MyCustomWidget::updateVisu()
{
    static bool flag=false;

    if(flag){
        flag=false;
        //int time = m_measureTimer.elapsed();
        m_measureTimer.restart();
    }

    if(!flag){
        m_measureTimer.restart();
        flag=true;
    }

    m_time += 0.4;
    draw();
    update();
}
