#include "heightmap_preview.h"
#include "ui_heightmap_preview.h"
#include <QPainter>
#include <math.h>
#include <iostream>

heightmap_preview::heightmap_preview(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::heightmap_preview)
{
    m_ui->setupUi(this);

    QWidget *scrollAreaWidgetContents = new QWidget();
    scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents"));
    scrollAreaWidgetContents->setGeometry(QRect(0, 0, 428, 312));
    QVBoxLayout *verticalLayout_2 = new QVBoxLayout(scrollAreaWidgetContents);
    verticalLayout_2->setSpacing(6);
    verticalLayout_2->setMargin(11);
    verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
    verticalLayout_2->addWidget(&ImageLabel);
    QSpacerItem *verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Expanding);
    verticalLayout_2->addItem(verticalSpacer);

    m_ui->scrollArea->setWidget(scrollAreaWidgetContents);
    connect(&ImageLabel, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(on_MouseMove(QMouseEvent*)));
    engine_preview = 0;
}

heightmap_preview::~heightmap_preview()
{
    delete m_ui;
}

void heightmap_preview::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void heightmap_preview::update_preview(QPixmap pixmap)
{
    int w = pixmap.width();
    int h = pixmap.height();
    m_ui->VStart->setMaximum(h);
    m_ui->VEnd->setMaximum(h);
    m_ui->HStart->setMaximum(w);
    m_ui->HEnd->setMaximum(w);
    m_ui->VStart->setEnabled(true);
    m_ui->HStart->setEnabled(true);
    m_ui->VEnd->setEnabled(true);
    m_ui->HEnd->setEnabled(true);
    clean_pixmap = pixmap;
    ImageLabel.setPixmap(pixmap);

    if(m_ui->DrawObjects->isChecked())draw_objects();
    if(m_ui->DrawBorders->isChecked())draw_borders();
    if(m_ui->DrawCamera->isChecked())draw_camera();
    if(m_ui->DrawCamPath->isChecked())draw_cam_path();
}
void heightmap_preview::update_preview()
{
    ImageLabel.setPixmap(clean_pixmap);
    if(m_ui->DrawObjects->isChecked())draw_objects();
    if(m_ui->DrawBorders->isChecked())draw_borders();
    if(m_ui->DrawCamera->isChecked())draw_camera();
    if(m_ui->DrawCamPath->isChecked())draw_cam_path();
}

void heightmap_preview::on_VStart_sliderMoved(int position)
{
    if(position > m_ui->VEnd->value())m_ui->VEnd->setValue(position);
    update_preview();
}

void heightmap_preview::on_HEnd_sliderMoved(int position)
{
    if(position < m_ui->HStart->value())m_ui->HStart->setValue(position);
    update_preview();
}

void heightmap_preview::on_HStart_sliderMoved(int position)
{
    if(position>m_ui->HEnd->value())m_ui->HEnd->setValue(position);
    update_preview();
}

void heightmap_preview::on_VEnd_sliderMoved(int position)
{
    if(position < m_ui->VStart->value())m_ui->VStart->setValue(position);
    update_preview();
}

void heightmap_preview::draw_borders()
{
    QPixmap pixmap(*ImageLabel.pixmap());
    QPainter painter(&pixmap);
    int w = pixmap.width();
    int h = pixmap.height();
    int h1 = m_ui->HStart->value();
    int h2 = m_ui->HEnd->value();
    int v1 = m_ui->VStart->value();
    int v2 = m_ui->VEnd->value();
    painter.setPen(QPen(QBrush(QColor(0xffffffff)), 1, Qt::DashLine));

    painter.drawLine(QPoint(h1, 0), QPoint(h1, v1));
    painter.drawLine(QPoint(h2, 0), QPoint(h2, v1));

    painter.drawLine(QPoint(h1, v2), QPoint(h1, h));
    painter.drawLine(QPoint(h2, v2), QPoint(h2, h));

    painter.drawLine(QPoint(0, v1), QPoint(h1, v1));
    painter.drawLine(QPoint(0, v2), QPoint(h1, v2));

    painter.drawLine(QPoint(h1, v1), QPoint(w, v1));
    painter.drawLine(QPoint(h1, v2), QPoint(w, v2));

    painter.setBrush(QBrush(QColor(0, 0, 64, 64)));
    painter.drawRect(QRect(QPoint(h1, v1), QPoint(h2, v2)));

    ImageLabel.setPixmap(pixmap);
}
void heightmap_preview::draw_camera()
{
    QPixmap pixmap(*ImageLabel.pixmap());
    QPainter painter(&pixmap);
    int w = pixmap.width();
    int h = pixmap.height();
    painter.setPen(QColor(0, 255, 0, 255));
    float x_pos = (-engine_preview->camera_position.x_pos+0.5) * w;
    float y_pos = (-engine_preview->camera_position.z_pos+0.5) * h;
    float cam_y_rot = engine_preview->camera_rotation.y_angle;
    float ex, ey;
    ey = h - (y_pos + w * cos(GRAD2RAD*cam_y_rot));
    ex = x_pos + w * sin(GRAD2RAD*cam_y_rot);
    painter.drawLine(QPoint(x_pos, y_pos), QPoint(ex, ey));
    ImageLabel.setPixmap(pixmap);
}
void heightmap_preview::draw_objects()
{
    int w = clean_pixmap.width();
    int h = clean_pixmap.height();
    QPixmap pixmap(*ImageLabel.pixmap());
    QPainter painter(&pixmap);
    QVector<QPoint> coordinates;

    std::vector<M3D::Coords> object_list = engine_preview->get_object_list();
    if(object_list.empty())return;
    //Посадим елки
    for(std::vector<M3D::Coords>::iterator it=object_list.begin(); it!=object_list.end(); ++it)
    {
        M3D::Coords coord = *it;
        QPoint point(round((coord.x_pos+0.5)*w), round(h*(coord.z_pos+0.5)));
        coordinates.push_back(point);
    }
    painter.setPen(QColor(0xffffffff));
    painter.drawPoints(QPolygon(coordinates));
    ImageLabel.setPixmap(pixmap);
}
void heightmap_preview::draw_cam_path()
{
    QPixmap pixmap(*ImageLabel.pixmap());
    QPainter painter(&pixmap);
    int w = pixmap.width();
    int h = pixmap.height();
    painter.setPen(QColor(255, 0, 0, 255));
    if(engine_preview->camera_path.empty())return;
    std::vector<CameraPath>::iterator it=engine_preview->camera_path.begin();
    std::vector<CameraPath>::iterator it_next=engine_preview->camera_path.begin();
    CameraPath current = *it_next;
    float cam_y_rot = current.rotation.y_angle;
    float x_pos = (-current.position.x_pos+0.5) * w;
    float y_pos = (-current.position.z_pos+0.5) * h;
    float ex, ey;
    ey = h - (y_pos + w*cos(GRAD2RAD*cam_y_rot));
    ex = x_pos + w*sin(GRAD2RAD*cam_y_rot);
    painter.drawLine(QPoint(x_pos, y_pos), QPoint(ex, ey));

    it_next++;
    for(; it_next!=engine_preview->camera_path.end(); ++it_next)
    {
        CameraPath prev = *it;
        CameraPath current = *it_next;
        float x_pos0 = (-prev.position.x_pos+0.5) * w;
        float y_pos0 = (-prev.position.z_pos+0.5) * h;
        x_pos = (-current.position.x_pos+0.5) * w;
        y_pos = (-current.position.z_pos+0.5) * h;
        painter.drawLine(QPoint(x_pos0, y_pos0), QPoint(x_pos, y_pos));

        cam_y_rot = current.rotation.y_angle;
        ex, ey;
        ey = h - (y_pos + w*cos(GRAD2RAD*cam_y_rot));
        ex = x_pos + w*sin(GRAD2RAD*cam_y_rot);
        painter.drawLine(QPoint(x_pos, y_pos), QPoint(ex, ey));
        ++it;
    }
    ImageLabel.setPixmap(pixmap);

}
void heightmap_preview::set_engine_preview(EnginePreview *engine)
{
    engine_preview = engine;
}

QRectF heightmap_preview::get_selected_rect()
{
    QPixmap pixmap(*ImageLabel.pixmap());
    QPainter painter(&pixmap);
    int w = pixmap.width();
    int h = pixmap.height();
    int h1 = m_ui->HStart->value();
    int h2 = m_ui->HEnd->value();
    int v1 = m_ui->VStart->value();
    int v2 = m_ui->VEnd->value();

    return QRectF(QPointF((float)h1/(float)w, (float)v1/(float)h), QPointF((float)h2/(float)w, (float)v2/(float)h));
}
void heightmap_preview::on_MouseMove(QMouseEvent *event)
{
    emit mouseMove(event);
}
void MyLabel::mouseMoveEvent ( QMouseEvent * event )
{
    emit mouseMove(event);
}
void MyLabel::mousePressEvent ( QMouseEvent * event )
{
    emit mouseMove(event);
}
