#include "mainwindowcontroller.h"
#include <View/informationscreen.h>
#include <View/inkscreen.h>
#include <View/menuscreen.h>
#include <View/settings.h>
#include <QQuickView>
#include <QDesktopServices>
#include <QQuickItem>
#include <ui_informationscreen.h>
#include <QFileInfo>
#include <Common/constants.h>
#include <Services/editor.h>
#include <QDir>
#include <QTimer>
#include <QMovie>
#include <QMessageBox>
#include <View/editscreen.h>
#include <QObject>

MainWindowController::MainWindowController(MainWindow *view)
{
    this->view = view;
}

MainWindowController::~MainWindowController()
{

}

void MainWindowController::settingsOption()
{
    Settings *settings = new Settings();
    settings->show(view->pos(), view->size());
    view->close();
}

void MainWindowController::helpOption()
{
    QString index = QCoreApplication::applicationFilePath().replace("ViewDesignerAssistant.exe", "").append("/VDA-Help/MainWindow-help.html");
    QFileInfo file = QFileInfo(index);
    if(file.exists() && file.isFile())
    {
        QPoint position = view->getCurrentPosition();

        QMovie *movie = new QMovie(":/resources/icon/load.gif");
        QLabel *movieLabel = new QLabel();
        movieLabel->setWindowFlags(Qt::FramelessWindowHint);
        movieLabel->setWindowOpacity(0.5);
        movieLabel->setAlignment(Qt::AlignCenter);
        movieLabel->setFixedWidth(view->width());
        movieLabel->setFixedHeight(view->height());
        movieLabel->setMovie(movie);
        if(position != QPoint(0,0))
            movieLabel->move(position);
        movieLabel->show();
        movie->start();
        if(QDesktopServices::openUrl(QUrl::fromLocalFile(index)))
        {
            QTimer::singleShot(1000, movieLabel, &QLabel::close);
        }
        else
        {
            movieLabel->close();
        }
    }
    else
    {
        QMessageBox errorMessage;
        errorMessage.setWindowTitle(QObject::tr("Ups!, algo inesperado ha ocurrido"));
        errorMessage.setIconPixmap(QPixmap(":/resources/icon/error.png").scaled(40,40,Qt::KeepAspectRatio, Qt::SmoothTransformation));
        errorMessage.setText(QString(QObject::tr("No se han encontrado el fichero de ayuda: %1 en el sistema. Si el problema persiste reinstale la aplicación")).arg(index));
        errorMessage.setFixedSize(500,500);
        errorMessage.exec();
    }
}

void MainWindowController::createMenuScreen()
{
    MenuScreen *menuScreen = new MenuScreen();
    menuScreen->show(view->pos(), view->size());
    view->close();
}

void MainWindowController::createInformationScreen()
{
    InformationScreen *infoScreen = new InformationScreen();
    infoScreen->show(view->pos(), view->size());
    view->close();
}

void MainWindowController::createInkScreen()
{
    InkScreen *inkScreen = new InkScreen();
    inkScreen->show(view->pos(), view->size());
    view->close();
}

void MainWindowController::edit()
{
    editScreen = new EditScreen();
    ((EditScreen *)editScreen)->setController(this);
    editScreen->show();
}

void MainWindowController::editScreenFromFile(QString fileName)
{
    editScreen->close();
    QFileInfo checkFile(fileName);
    if(checkFile.exists())
    {
        Interfaz *interfaz = Editor::edit(fileName);
        if(interfaz == nullptr)
        {
            QStringList *errors = Editor::getErrorList();
            view->showError(errors->size(), errors->join(""));
        }
        else
        {
            int type = Editor::getType();
            selectedType(interfaz, type);
        }
    }
}

void MainWindowController::selectedType(Interfaz *interfaz, int type)
{
    switch (type) {
    case 1:
    {
        MenuScreen *menuScreen = new MenuScreen();
        menuScreen->setEditModel(interfaz);
        menuScreen->show(view->pos(), view->size());
    }
        break;
    case 2:
    {
        InformationScreen *infoScreen = new InformationScreen();
        infoScreen->setEditModel(interfaz);
        infoScreen->show(view->pos(), view->size());
    }
        break;
    case 3:
    {
        InkScreen *inkScreen = new InkScreen();
        inkScreen->setEditModel(interfaz);
        inkScreen->show(view->pos(), view->size());
    }
        break;
    default:
        break;
    }

    view->close();
}

