#include "ui_settingswidget.h"

#include "models/settings.h"
#include "models/patchlist.h"

#include "widgets/settingswidget.h"
#include "widgets/mainwindow.h"

#include "constants.h"

#include <QDebug>
#include <QScrollBar>
#include <QFileDialog>
#include <QMetaObject>

SettingsWidget::SettingsWidget(PatchList *legacy_patches, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::SettingsWidget)
    , legacy_patches_(legacy_patches)
{
    ui->setupUi(this);
    for (int i = 1; i <= 4; ++i)
        setStatusToBlock(i, E_ERROR);

    combobox_scrolling_disabler = new SettingsWidgetPrivate::ComboboxScrollingDisabler();
    scroller = new SettingsWidgetPrivate::SettingsTabsScroller({ui->sub_entry_1,
                                                                ui->sub_entry_2,
                                                                ui->sub_entry_3,
                                                                ui->sub_entry_4
                                                               },
                                                                ui->content_scroll_area
                                                               );

    ui->lotro_base_language_combobox->installEventFilter(combobox_scrolling_disabler);
    ui->interface_scale_combobox->installEventFilter(combobox_scrolling_disabler);
//    ui->content_scroll_area->verticalScrollBar()->installEventFilter(scroller);
}

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

void SettingsWidget::updateFontsSizes()
{
    ui->frame_title->setFont(trajan_10pt);
    ui->group_label_1->setFont(trajan_9pt);
    ui->group_label_2->setFont(trajan_9pt);
    ui->group_label_3->setFont(trajan_9pt);

    ui->sub_entry_1_title->setFont(garamond_11pt);
    ui->sub_entry_2_title->setFont(garamond_11pt);
    ui->sub_entry_3_title->setFont(garamond_11pt);

    ui->backup_create_button->setFont(trajan_8pt);
    ui->backup_restore_button->setFont(trajan_8pt);
    ui->backup_remove_button->setFont(trajan_8pt);

    ui->apply_button_entry_1->setFont(trajan_11pt);
    ui->apply_button_entry_2->setFont(trajan_11pt);

    ui->game_folder_label->setFont(crimson_11pt);
    ui->game_folder_path->setFont(crimson_11pt);
    ui->lotro_base_language_label->setFont(crimson_11pt);
    ui->lotro_base_language_combobox->setFont(crimson_11pt);
    ui->lotro_base_language_combobox->insertItem(ui->lotro_base_language_combobox->count(), ""); // is needed to invoke elements resize
    ui->lotro_base_language_combobox->removeItem(ui->lotro_base_language_combobox->count() - 1);
    ui->skiprawdownload_checkbox->setFont(crimson_11pt);
    ui->nosplashscreen_checkbox->setFont(crimson_11pt);

    ui->backup_status_label->setFont(crimson_11pt);
    ui->backup_status_value->setFont(crimson_11pt);
    ui->backup_creation_time_label->setFont(crimson_11pt);
    ui->backup_creation_time_value->setFont(crimson_11pt);
    ui->backup_path_label->setFont(crimson_11pt);
    ui->backup_path_value->setFont(crimson_11pt);

    ui->patch_emotes_checkbox->setFont(crimson_11pt);
    ui->patch_items_checkbox->setFont(crimson_11pt);
    ui->patch_loadscreens_checkbox->setFont(crimson_11pt);
    ui->patch_maps_checkbox->setFont(crimson_11pt);
    ui->patch_sounds_checkbox->setFont(crimson_11pt);
    ui->patch_textures_checkbox->setFont(crimson_11pt);
    ui->patch_video_checkbox->setFont(crimson_11pt);
    ui->patch_texts_checkbox->setFont(crimson_11pt);

    ui->micropatch_checkbox->setFont(crimson_12pt);
    ui->interface_scale_label->setFont(crimson_12pt);
    ui->interface_scale_combobox->setFont(crimson_12pt);
    ui->interface_scale_combobox->insertItem(ui->interface_scale_combobox->count(), "250%"); // is needed to invoke elements resize
    ui->interface_scale_combobox->removeItem(ui->interface_scale_combobox->count() - 1);

    ui->texts_block_label->setFont(crimson_12pt);
    ui->graphics_block_label->setFont(crimson_12pt);
    ui->sounds_block_label->setFont(crimson_12pt);
}

void SettingsWidget::setStatusToBlock(int block_id, SettingsWidget::E_STATUS status)
{
    QWidget* widget = ui->checkpoints_list->findChild<QWidget*>("group_status_icon_" + QString::number(block_id));
    if (!widget)
        return;

    if (status == E_NONE)
        widget->setStyleSheet("");
    if (status == E_OK)
        widget->setStyleSheet("border-image: url(:/status/ok_icon.png);");
    if (status == E_WARNING)
        widget->setStyleSheet("border-image: url(:/status/alert_icon.png);");
    if (status == E_ERROR)
        widget->setStyleSheet("border-image: url(:/status/error_icon.png);");
}

void SettingsWidget::resizeEvent(QResizeEvent *event)
{
    updateFontsSizes();
    double coefficient = window_width / default_window_width;

    ui->frame_title->move(QPoint(45, 33) * coefficient);
    ui->frame_title->resize(QSize(201, 21) * coefficient);
    ui->checkpoints_list->move(QPoint(25, 70) * coefficient);
    ui->checkpoints_list->resize(QSize(265, 321) * coefficient);

    ui->content_scroll_area->move(QPoint(310, 0) * coefficient);
    ui->content_scroll_area->resize(QSize(671, 521) * coefficient);
    ui->content_widget->setMinimumWidth(650 * coefficient);

    ui->group_status_icon_1->setMinimumSize(QSize(25, 25) * coefficient);
    ui->group_status_icon_2->setMinimumSize(QSize(25, 25) * coefficient);
    ui->group_status_icon_3->setMinimumSize(QSize(25, 25) * coefficient);

    ui->change_folder_button->setMinimumSize(QSize(36, 32) * coefficient);

    ui->patch_texts_icon->setMinimumSize(QSize(80, 80) * coefficient);
    ui->patch_graphics_icon->setMinimumSize(QSize(80, 80) * coefficient);
    ui->patch_sounds_icon->setMinimumSize(QSize(80, 80) * coefficient);

    ui->apply_button_entry_1->setMinimumHeight(double(55) * coefficient);
    ui->apply_button_entry_2->setMinimumHeight(double(55) * coefficient);
}

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

    if (!window) {
        ui->interface_scale_combobox->setCurrentText(Settings::getValue("General/UI_scale").toString() + "%");
        qDebug() << "CANNOT FIND MAIN WINDOW!!!";
        return;
    }

    int value = arg1.left(arg1.length() - 1).toInt();
    window->resize(1000 * value / 100, 648 * value / 100);

    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");

    if (str.isEmpty()) {
        return;
    }

    QString game_folder= str.replace("/LotroLauncher.exe", "").replace("\\", "/").replace("//", "/") + "/";

    if (Settings::getValue("Lotro/game_path").toString() == game_folder)
        return;

    Settings::setValue("Lotro/game_path", game_folder);
    ui->game_folder_path->setText(game_folder);
}

void SettingsWidget::on_lotro_base_language_combobox_currentIndexChanged(int index)
{
    QString value = "English";

    if (index == 0)
        value = "English";
    if (index == 1)
        value = "DE";
    if (index == 2)
        value = "FR";

    if (Settings::getValue("Lotro/original_locale").toString() == value)
        return;

    Settings::setValue("Lotro/original_locale", value);
}
