/*
 * Fooyin
 * Copyright © 2024, Luke Taylor <luket@pm.me>
 *
 * Fooyin is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Fooyin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Fooyin.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <gui/widgets/doubleslidereditor.h>

#include <gui/widgets/specialvaluespinbox.h>

#include <QDoubleSpinBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QSlider>

using namespace Qt::StringLiterals;

namespace Fooyin {
DoubleSliderEditor::DoubleSliderEditor(const QString& name, QWidget* parent)
    : QWidget{parent}
    , m_slider{new QSlider(Qt::Horizontal, this)}
    , m_spinBox{new SpecialValueDoubleSpinBox(this)}
{
    auto* layout = new QHBoxLayout(this);
    layout->setContentsMargins({});

    if(!name.isEmpty()) {
        auto* label = new QLabel(name + ":"_L1, this);
        layout->addWidget(label);
    }

    layout->addWidget(m_slider, 1);
    layout->addWidget(m_spinBox);

    QObject::connect(m_slider, &QSlider::valueChanged, this, &DoubleSliderEditor::sliderValueChanged);
    QObject::connect(m_spinBox, &QDoubleSpinBox::valueChanged, this, &DoubleSliderEditor::spinBoxValueChanged);

    m_slider->setSingleStep(1);
}

DoubleSliderEditor::DoubleSliderEditor(QWidget* parent)
    : DoubleSliderEditor{{}, parent}
{ }

double DoubleSliderEditor::value() const
{
    return m_spinBox->value();
}

double DoubleSliderEditor::minimum() const
{
    return m_spinBox->minimum();
}

void DoubleSliderEditor::setMinimum(double min)
{
    m_spinBox->setMinimum(min);
    updateSlider();
}

double DoubleSliderEditor::maximum() const
{
    return m_spinBox->maximum();
}

void DoubleSliderEditor::setMaximum(double max)
{
    m_spinBox->setMaximum(max);
    updateSlider();
}

double DoubleSliderEditor::singleStep() const
{
    return m_spinBox->singleStep();
}

void DoubleSliderEditor::setSingleStep(double step)
{
    m_spinBox->setSingleStep(step);
    updateSlider();
}

void DoubleSliderEditor::setTicksVisible(bool visible)
{
    m_slider->setTickPosition(visible ? QSlider::TicksBelow : QSlider::NoTicks);
}

void DoubleSliderEditor::setRange(double min, double max)
{
    m_spinBox->setRange(min, max);
    updateSlider();
}

QString DoubleSliderEditor::prefix() const
{
    return m_spinBox->prefix();
}

void DoubleSliderEditor::setPrefix(const QString& prefix)
{
    m_spinBox->setSpecialPrefix(prefix);
}

QString DoubleSliderEditor::suffix() const
{
    return m_spinBox->suffix();
}

void DoubleSliderEditor::setSuffix(const QString& suffix)
{
    m_spinBox->setSpecialSuffix(suffix);
}

void DoubleSliderEditor::addSpecialValue(double val, const QString& text)
{
    m_spinBox->addSpecialValue(val, text);
}

void DoubleSliderEditor::setValue(double value)
{
    if(value != m_spinBox->value()) {
        m_spinBox->setValue(value);
        updateSlider();
    }
}

void DoubleSliderEditor::sliderValueChanged(int value)
{
    const double min  = m_spinBox->minimum();
    const double max  = m_spinBox->maximum();
    const double step = m_spinBox->singleStep();

    const double spinVal = std::min(max, min + (static_cast<double>(value) * step));

    if(spinVal != m_spinBox->value()) {
        m_spinBox->setValue(spinVal);
    }
}

void DoubleSliderEditor::spinBoxValueChanged(double value)
{
    const QSignalBlocker blocker{m_spinBox};
    updateSlider();

    Q_EMIT valueChanged(value);
}

void DoubleSliderEditor::updateSlider()
{
    const QSignalBlocker blocker{m_slider};

    const double min  = m_spinBox->minimum();
    const double max  = m_spinBox->maximum();
    const double step = m_spinBox->singleStep();

    const double range = max - min;
    if(range <= 0.0 || step <= 0.0) {
        m_slider->setRange(0, 0);
        return;
    }

    const int stepCount = std::max(1, static_cast<int>(std::round(range / step)));
    m_slider->setRange(0, stepCount);
    m_slider->setPageStep(std::max(1, stepCount / 10));
    m_slider->setTickInterval(1);

    const int sliderVal = std::clamp(static_cast<int>(std::round((m_spinBox->value() - min) / step)), 0, stepCount);

    if(sliderVal != m_slider->value()) {
        m_slider->setValue(sliderVal);
    }
}
} // namespace Fooyin

#include "gui/widgets/moc_doubleslidereditor.cpp"
