#include <QDir>
#include <QProcess>
#include <QFileDialog>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "floorplan.h"
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

void MainWindow::on_verticalSlider_2_valueChanged(int value)
{
    ui->sceneView->camera.EyeDistanceInCm = value;
    repaint();
}

void MainWindow::on_actionLoad_triggered()
{
    try
    {
        QString file = QFileDialog::getOpenFileName(this,"Load Duke Nukem 3D map",
                                                    QDir::home().absolutePath(),
                                                    "Duke Nukem Map (*.map)");

        DukeNukem3D::DukeMap *map = new DukeNukem3D::DukeMap(file.toStdString());
        Scene* oldScene = ui->sceneView->scene;
        ui->sceneView->scene = map->ToScene();
        if(oldScene != NULL)
            delete oldScene;
        delete map;

    }
    catch(...)
    {
    }

    repaint();
}

void MainWindow::on_actionLoad_Floor_Plan_triggered()
{
    try
    {
        QString file = QFileDialog::getOpenFileName(this,"Load Floor-Plan",
                                                    QDir::home().absolutePath(),
                                                    "Any File (*.*)");

        FloorPlan::House *house = new FloorPlan::House(file.toStdString());
        Scene* oldScene = ui->sceneView->scene;
        ui->sceneView->scene = house->ToScene();
        if(oldScene != NULL)
            delete oldScene;
        delete house;
    }
    catch(...)
    {
    }

    repaint();
}

void MainWindow::on_actionExport_To_Blender_triggered()
{
    QDir t = QDir::temp();
    QString f = t.absolutePath() + "/threedee2blender.py";
    ofstream os(f.toStdString().c_str());
    ui->sceneView->scene->ExportToBlender(os);
    os.close();

    QProcess *process = new QProcess(this);
    QStringList params;
    params.append("-P");
    params.append(f);
    process->start("blender", params);
}
