#include <QKeyEvent>

#include "mainwindow.h"
#include "ui_mainwindow.h"

bool FPS_NAVIGATION_VIEW = false;
bool fpsMouseInit = false;
Eigen::Vector2f fpsMousePos;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->render->addWidget(&_gl);
    _gl.setFocus();

    if(TP_ID == 1)
    {
        ui->combo_scenes->addItem("Scene 1");
        ui->combo_scenes->addItem("Scene 2");
        ui->combo_scenes->addItem("Scene 3");
        ui->combo_scenes->addItem("Scene 4");
        ui->combo_scenes->addItem("Scene 5");
    }
    else if(TP_ID == 2)
    {
        ui->combo_scenes->addItem("Scene 6");
        ui->combo_scenes->addItem("Scene 7");
        ui->combo_scenes->addItem("Scene 8");
    }
    else if(TP_ID == 3)
        ui->combo_scenes->addItem("Scene 9");
    else
    {
        std::cerr << "ERROR : TP_ID unknown" << std::endl;
        exit(0);
    }


    // mouse move event work without click
    setMouseTracking(true);
    centralWidget()->setMouseTracking(true);
    _gl.setMouseTracking(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_combo_scenes_currentIndexChanged(const QString &arg1)
{
    _gl.setScene(QString(arg1).remove("Scene ").toInt());
    _gl.setFocus();
}

void MainWindow::on_btn_reset_scene_clicked()
{
    _gl.setScene(ui->combo_scenes->currentText().remove("Scene ").toInt());
}


void MainWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Escape:
        exit(EXIT_SUCCESS);
        break;
    case Qt::Key_Control:
        FPS_NAVIGATION_VIEW = true;
        fpsMouseInit = true;
        break;
    }
    _gl.update();
}

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
    cout << "key release event : " << event->key() << endl;
    switch (event->key()) {
    case Qt::Key_Control:
        FPS_NAVIGATION_VIEW = false;
        fpsMouseInit = false;
        break;
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    cout << "mouse press event : " << event->button() << endl;

    // relative pos in the render frame
    int x = event->pos().x() - _gl.pos().x();
    int y = event->pos().y() - _gl.pos().y();

    // color picking
    if(x >= 0 && x < _gl.camera()->vpWidth())
        if(y >= 0 && y < _gl.camera()->vpHeight())
            _gl.colorPicking(Vector2i(x, y));
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    /// TODO : manage camera position
    if(FPS_NAVIGATION_VIEW)
    {
        cout << "mouse move event : " << event->pos().x() << " " << event->pos().y() << endl;

        Vector2f mp = Vector2f(event->pos().x(), event->pos().y());
        Vector3f position, target, up;
        /// TODO : fill position direction and up


        // camera look at
        _gl.camera()->lookAt(position, target, up);
        // update fps info
        fpsMousePos = mp;
        fpsMouseInit = true;
        // update render
        _gl.update();

    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    cout << "mouse release event : " << event->button() << endl;
}

void MainWindow::wheelEvent(QWheelEvent *event)
{
    cout << "mouse wheel event : " << event->delta() << endl;
}
