#include "settings.h"
#include "ui_settings.h"
#include "mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include <QDir>
#include <QUrl>
#include <Controller/settingscontroller.h>

Settings::Settings(QWidget *parent) :
    View(parent),
    ui(new Ui::Settings)
{        
    ui->setupUi(this);
    controller = new SettingsController(this);
    initElements();
    ui->retranslateUi(this);
}

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

void Settings::showError(QString error)
{
    if(error.isEmpty())
        ui->errorForm->hide();
    else
       ui->errorForm->show();
    ui->errorTxt->setText(error);
}

void Settings::showSuccess(QString success)
{
    if(success.isEmpty())
        ui->successForm->hide();
    else
       ui->successForm->show();
    ui->successTxt->setText(success);
}

void Settings::showInfo(QString info)
{

}

void Settings::on_aboutBtn_clicked()
{
    QMessageBox about(tr("Acerca de HP View Desingner Assistant"),
                      tr("Proyecto de Fin de Grado realizado por Alberto Gutiérrez Pérez\n"
                         "en colaboración con HP\n"
                         "Universidad de Valladolid\n"
                         "\n"
                         "Basado en Qt 5.8.0\n"
                         "Fecha de realización Junio 2017\n"
                         "Versión 1.0.0\n"
                         "\n"
                         "Contacto: albertogperez@outlook.com"),
                      QMessageBox::NoIcon,
                      QMessageBox::Accepted | QMessageBox::Default,
                      QMessageBox::NoButton,
                      QMessageBox::NoButton);
    QPixmap hpIcon(":/resources/icon/hplogo.png");
    QPixmap infoIcon(":/resources/icon/info.png");
    about.setWindowIcon(infoIcon);
    about.setIconPixmap(hpIcon.scaled(50,50,Qt::KeepAspectRatio, Qt::SmoothTransformation));
    about.exec();
}

void Settings::on_homeBtn_clicked()
{
    controller->on_homeBtn_clicked(false);
}

void Settings::on_alwaysAskRadio_clicked(bool checked)
{
    alwaysAskOptions(checked);
    ((SettingsController *)controller)->saveAlwaysAsk();
}

void Settings::on_browserBtn_clicked()
{
    QString fileName = QFileDialog::getExistingDirectory(this,tr("Seleccione carpeta"), QDir::currentPath(), QFileDialog::ShowDirsOnly | QFileDialog::DontUseNativeDialog );
    ui->browserDirInput->setText(fileName);
    ((SettingsController *)controller)->saveDefaultDir();
}

void Settings::on_helpBtn_clicked()
{
    QString url = getRootHelpPath().append("/VDA-Help/settings.html");
    controller->on_helpBtn_clicked(url, currentPosition);
}

QString Settings::getDefaultDir()
{
    return ui->browserDirInput->text();
}

void Settings::setDefaultDir(QString path)
{ 
    ui->browserDirInput->setText(path);

}

void Settings::setAlwaysAsk(bool value)
{
    ui->alwaysAskRadio->setChecked(value);
    alwaysAskOptions(value);
}

bool Settings::getAlwaysAsk()
{
    return ui->alwaysAskRadio->isChecked();
}

void Settings::retranslate()
{
    ui->retranslateUi(this);
}

void Settings::setSpanishLanguage()
{
    ui->spanishRadio->setChecked(true);
}

void Settings::setEnglishLanguage()
{
    ui->englishRadio->setChecked(true);
}

void Settings::initElements()
{
    setToolTips();    
    ui->errorForm->hide();
    ui->successForm->hide();
}

void Settings::isFullFill()
{

}

void Settings::alwaysAskOptions(bool checked)
{    
    ui->browserDirInput->setEnabled(!checked);
    if(checked)
        ui->browserDirInput->setText("");
}

void Settings::setToolTips()
{
    ui->resetValueBtn->setToolTip(tr("Elimina los ajustes guardados"));
}

void Settings::setValidators(){

}

void Settings::on_browserDirInput_textEdited()
{
    ((SettingsController *)controller)->saveDefaultDir();
}

void Settings::on_resetValueBtn_clicked()
{
    ui->browserDirInput->clear();
    ui->browserDirInput->setEnabled(true);
    ui->alwaysAskRadio->setChecked(false);
    ui->browserBtn->setEnabled(true);
    ui->languageBtnGroup->setExclusive(false);
    ui->spanishRadio->setChecked(false);
    ui->englishRadio->setChecked(false);
    ui->languageBtnGroup->setExclusive(true);
    ((SettingsController *)controller)->clearValues();
}

void Settings::on_spanishRadio_clicked()
{
  ((SettingsController *)controller)->changeLanguage("es");
}

void Settings::on_englishRadio_clicked()
{
    ((SettingsController *)controller)->changeLanguage("en");
}
