#include "widgets/settingswidget.h"
#include "ui_settingswidget.h"
#include "models/filesystem.h"
#include "models/lotrodatmanager.h"
#include "widgets/mainwindow.h"

#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>

SettingsWidget::SettingsWidget(QSettings* settings, PatchDownloader* patch_downloader, LotroDatManager* lotro_dat_manager, QWidget *parent) :
    QWidget(parent), app_settings(settings), lotro_manager(lotro_dat_manager),
    ui(new Ui::SettingsWidget)
{
    ui->setupUi(this);
    ui->folder_value_common->setText(app_settings->value("General/game_folder_path", "Не выбрана").toString());
}

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

void SettingsWidget::on_interface_scale_combobox_common_currentIndexChanged(const QString &arg1)
{
    MainWindow* window = qobject_cast<MainWindow*>(qApp->activeWindow());

    if (!window) {
        ui->interface_scale_combobox_common->setCurrentText(app_settings->value("general/ui_scale", 100).toString() + "%");
        qDebug() << "CANNOT FIND MAIN WINDOW!!!";
        return;
    }

    int value = arg1.left(arg1.length() - 1).toInt();
    window->changeFontSizeRecursive(value, window);
    window->resize(900 * value / 100, 650 * value / 100);
    app_settings->setValue("general/ui_scale", value);
}

void SettingsWidget::on_change_folder_button_clicked() {
    QStringList known_paths = FileSystem::recognizeRegistryLotroPath();
    QString template_path = known_paths.empty() ? "" : known_paths[0];
    QString str = QFileDialog::getOpenFileName(0, "Расположение игры", template_path, "LotroLauncher.exe");
    QString game_folder= str.replace("/LotroLauncher.exe", "").replace("\\", "/").replace("//", "/");

    if (app_settings->value("General/game_folder_path", "").toString() == game_folder)
        return;

    app_settings->setValue("General/game_folder_path", game_folder);
    ui->folder_value_common->setText(game_folder);
    QMetaObject::invokeMethod(lotro_manager, "InitialiseManager", Qt::QueuedConnection);
}

void SettingsWidget::on_lotro_patch_language_combobox_common_activated(int index)
{
    QString value = "";
    if (index == 0)
        value = "English";
    if (index == 1)
        value = "DE";
    if (index == 2)
        value = "FR";

    if (app_settings->value("General/original_locale", "").toString() == value)
        return;

    app_settings->setValue("General/original_locale", value);
    QMetaObject::invokeMethod(lotro_manager, "InitialiseManager", Qt::QueuedConnection);
}

void SettingsWidget::on_patch_all_button_common_clicked()
{
    QMetaObject::invokeMethod(lotro_manager, "InstallActivePatches", Qt::QueuedConnection);
}

void SettingsWidget::on_remove_backup_common_clicked()
{
    QMetaObject::invokeMethod(lotro_manager, "RemoveBackup", Qt::QueuedConnection);
}

void SettingsWidget::on_create_backup_common_clicked()
{
    QMetaObject::invokeMethod(lotro_manager, "CreateBackup", Qt::QueuedConnection);
}

void SettingsWidget::on_restore_backup_common_clicked()
{
    QMetaObject::invokeMethod(lotro_manager, "RestoreBackup", Qt::QueuedConnection);
}
