#include "controller.h"
#include <View/mainwindow.h>
#include <View/settings.h>
#include <QQuickView>
#include <QMessageBox>
#include <QFileDialog>
#include <Common/constants.h>
#include <QDesktopServices>
#include <QLabel>
#include <QTimer>
#include <QMovie>
#include <Manager/settingsmanager.h>

Controller::Controller()
{

}

void Controller::on_homeBtn_clicked(bool ask)
{
    bool exit = true;
    if(ask) exit = confirmExit();
    if(exit)
    {
        QSize size;
        QPoint position;

        if(generalView != nullptr)
        {
            size = generalView->size();
            position = generalView->pos();
            generalView->close();
        }
        else
        {
            if(optionsView != nullptr)
            {
                size = optionsView->size();
                position = optionsView->pos();
                optionsView->close();
            }
        }

        MainWindow *mainMenu = new MainWindow();
        mainMenu->move(position);
        mainMenu->resize(size);
        mainMenu->show();
    }
}

bool Controller::confirmExit(){

    QMessageBox question;
    question.setIconPixmap(QPixmap(":/help/resources/help/img/warning.png").scaled(40,40,Qt::KeepAspectRatio, Qt::SmoothTransformation));
    question.setWindowTitle("Aviso");
    question.setWindowIcon(QIcon(":/resources/icon/hplogo.png"));
    question.setText(tr("Si continuas perderás los cambios. ¿Realmente desea continuar?"));
    question.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    question.setButtonText(QMessageBox::Yes, tr("Si"));
    question.setButtonText(QMessageBox::No, tr("No"));
    question.setDefaultButton(QMessageBox::No);
    int reply= question.exec();

    if (reply == QMessageBox::No)
        return false;
    return true;
}

void Controller::showPreview(QString path)
{
    QQuickView *view = new QQuickView;
    view->setSource(QUrl::fromLocalFile(path));
    QPixmap hpIcon(":/resources/icon/hplogo.png");
    view->setTitle("Vista Previa");
    view->setIcon(hpIcon);
    view->show();
}

QString Controller::getLongSavePath()
{
    QString defaultSaveDir = SettingsManager::getDefaultDir();

    if(defaultSaveDir.isNull() || defaultSaveDir.isEmpty() || !QDir(defaultSaveDir).exists())
    {
        defaultSaveDir = QDir::currentPath();
    }

    QString path = QFileDialog::getSaveFileName(nullptr, tr("Guardar como"), defaultSaveDir, "Qml (*.qml)", nullptr, QFileDialog::DontUseNativeDialog);

    if(!path.isEmpty() && !path.endsWith(".qml"))
        path.append(".qml");

    return path;
}

void Controller::switchToOptionsView()
{
    if(generalView != nullptr)
    {
        optionsView->show(generalView->pos(), generalView->size());
        generalView->close();
        generalView = nullptr;
    }
    else
    {
        return;
    }
}

void Controller::on_settingsBtn_clicked(){
    if(confirmExit())
    {
        QSize size;
        QPoint position;
        if(generalView != nullptr)
        {
            size = generalView->size();
            position = generalView->pos();
            generalView->close();
        }
        else
        {
            size = optionsView->size();
            position = optionsView->pos();
            optionsView->close();
        }
        Settings *settings = new Settings();
        settings->show(position,size);
    }
}

void Controller::on_helpBtn_clicked(QString path, QPoint position)
{    
    QFileInfo file = QFileInfo(path);
    if(file.exists() && file.isFile())
    {
        View *view = generalView != nullptr ? generalView : optionsView;

        QMovie *movie = new QMovie(":/resources/icon/load.gif");
        QLabel *movieLabel = new QLabel();
        movieLabel->setWindowFlags(Qt::FramelessWindowHint);
        movieLabel->setAlignment(Qt::AlignCenter);
        movieLabel->setWindowOpacity(0.8);
        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(path)))
        {
            QTimer::singleShot(1000, movieLabel, &QLabel::close);
        }

        else
        {
            movieLabel->close();
        }
    }
    else
    {
        QMessageBox errorMessage;
        errorMessage.setWindowTitle(tr("Ups!, algo inesperado ha ocurrido"));
        errorMessage.setIconPixmap(QPixmap(":/resources/icon/error.png").scaled(40,40,Qt::KeepAspectRatio, Qt::SmoothTransformation));
        errorMessage.setText(QString(tr("No se han encontrado el fichero de ayuda %1 en el sistema.\n Si el problema persiste reinstale la aplicación")).arg(path));
        errorMessage.setFixedSize(500,500);
        errorMessage.exec();
    }
}
