#ifndef SettingsWidget_H
#define SettingsWidget_H

#include <QWidget>
#include <QTimer>
#include <QSettings>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QEvent>
#include <QDebug>
#include <QScrollArea>
#include <QScrollBar>
#include <QWheelEvent>

namespace Ui {
class SettingsWidget;
}

class PatchList;

namespace SettingsWidgetPrivate {

class ComboboxScrollingDisabler : public QObject {
    Q_OBJECT

protected:
    bool eventFilter(QObject *, QEvent *event) override {
        if (event->type() == QEvent::Wheel) {
            event->ignore();
            return true;
        } else {
            return false;
        }
    }
};

class SettingsTabsScroller : public QObject {
public:
    SettingsTabsScroller(QList<QWidget*> entries, QScrollArea* target_widget) : target_widget_(target_widget), current_entry_id_(0), entries_(std::move(entries)) {
        scroll_entries_animation_ = new QPropertyAnimation(target_widget_->verticalScrollBar(), "value");
        scroll_entries_animation_->setDuration(450);
        scroll_entries_animation_->setEasingCurve(QEasingCurve::InOutQuart);
    }

    ~SettingsTabsScroller() {
        scroll_entries_animation_->deleteLater();
    }

protected:
    bool eventFilter(QObject *obj, QEvent *event) override {
//        if (obj != target_widget_) {
//            return false;
//        }

        if (event->type() == QEvent::Wheel) {
            event->ignore();
            QWheelEvent *wheel_event = (QWheelEvent*)(event);

            int wheel_angle = wheel_event->angleDelta().y();

            qDebug() << "SCROLL EVENT! " << wheel_event->pixelDelta() << "/" << wheel_event->angleDelta();

            if (scroll_entries_animation_->state() == QPropertyAnimation::Running) {
                return true;
            }

            if (wheel_angle < 0 && current_entry_id_ + 1 < entries_.size()) {
                ++current_entry_id_;
                scroll_entries_animation_->setStartValue(target_widget_->verticalScrollBar()->value());
                scroll_entries_animation_->setEndValue(entries_[current_entry_id_]->y());
                scroll_entries_animation_->start();
                return true;
            }

            if (wheel_angle > 0 && current_entry_id_ - 1 >= 0) {
                 --current_entry_id_;
                 scroll_entries_animation_->setStartValue(target_widget_->verticalScrollBar()->value());
                 scroll_entries_animation_->setEndValue(entries_[current_entry_id_]->y());
                 scroll_entries_animation_->start();
                 return true;
            }

            return true;
        } else {
            return false;
        }
    }

private:
    int current_entry_id_;
    QScrollArea* target_widget_;
    QList<QWidget*> entries_;
    QPropertyAnimation* scroll_entries_animation_;
};

}

class SettingsWidget : public QWidget
{
    Q_OBJECT

public:

    explicit SettingsWidget(PatchList *legacy_patches, QWidget *parent = 0);

    ~SettingsWidget();

    enum E_STATUS {
        E_NONE = 0,
        E_ERROR = -2,
        E_WARNING = -1,
        E_OK = 1
    };

public slots:
    void updateFontsSizes();
    void setStatusToBlock(int block_id, E_STATUS status);

protected:
    void resizeEvent(QResizeEvent *event) override;

private slots:
    void on_interface_scale_combobox_currentIndexChanged(const QString &arg1);

    void on_change_folder_button_clicked();

    void on_lotro_base_language_combobox_currentIndexChanged(int index);

private:
    Ui::SettingsWidget *ui;

    PatchList *legacy_patches_;

    SettingsWidgetPrivate::ComboboxScrollingDisabler* combobox_scrolling_disabler;
    SettingsWidgetPrivate::SettingsTabsScroller* scroller;
};

#endif // SettingsWidget_H
