#include "pch.h"
#include "RangeSlider.h"
#include <QDebug>
#include "util.h"

RangeSlider::RangeSlider(QWidget *parent)
	: QWidget(parent), sliderRangeColor(200,200,200), rangeColor(0,122,204)
{
}

RangeSlider::~RangeSlider()
{
}

void RangeSlider::setMinSliderValue(int value)
{
	value = std::clamp(value, minRange, maxRange);
	if (value > maxSliderValue) {
		minSliderValue = maxSliderValue;
		maxSliderValue = value;
		emit minChanged(minSliderValue);
		emit maxChanged(maxSliderValue);
	}
	else {
		minSliderValue = value;
		emit minChanged(minSliderValue);
	}
}

void RangeSlider::setMaxSliderValue(int value)
{
	value = std::clamp(value, minRange, maxRange);
	if (value < minSliderValue) {
		maxSliderValue = minSliderValue;
		minSliderValue = value;
		emit minChanged(minSliderValue);
		emit maxChanged(maxSliderValue);
	}
	else {
		maxSliderValue = value;
		emit maxChanged(maxSliderValue);
	}
}

void RangeSlider::setRange(int min, int max)
{
	if (min > max) {
		std::swap(min, max);
	}
	minRange = min;
	maxRange = max;
}

void RangeSlider::setMinRange(int value)
{
	if (value > maxRange) {
		minRange = maxRange;
		maxRange = value;
	}
	else {
		minRange = value;
	}

}

void RangeSlider::setMaxRange(int value)
{
	if (value < minRange) {
		maxRange = minRange;
		minRange = value;
	}
	else {
		maxRange = value;
	}
}

void RangeSlider::paintEvent(QPaintEvent* event)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::RenderHint::HighQualityAntialiasing);
	painter.setPen(sliderRangeColor);
	//painter.setBrush(sliderRangeColor);
	//FindMiddle
	int middle = rect().center().y();
	painter.drawRect(QRect(QPoint(minPos(), middle + 2), QPoint(maxPos(), middle - 2)));

	double minSliderAlpha = util::inverseLinearInterpolation(minRange, maxRange, minSliderValue);
	double minSliderPos = util::linearInterpolate(minPos(), maxPos(), minSliderAlpha);

	double maxSliderAlpha = util::inverseLinearInterpolation(minRange, maxRange, maxSliderValue);
	double maxSliderPos = util::linearInterpolate(minPos(), maxPos(), maxSliderAlpha);
	
	//DrawRange
	painter.fillRect(QRectF(QPointF(minSliderPos, (double)middle - 2), QPointF(maxSliderPos, (double)middle + 2)), rangeColor);

	
	//DrawSlider
	QPen pen(rangeColor);
	pen.setWidth(2);
	painter.setPen(pen);
	painter.setBrush(sliderRangeColor);
	painter.drawEllipse(QPointF(minSliderPos, (double)middle + 1), 10, 10);
	painter.drawEllipse(QPointF(maxSliderPos, (double)middle + 1), 10, 10);
}

void RangeSlider::mouseMoveEvent(QMouseEvent* event)
{
	updateSliderPosition(event);
}

void RangeSlider::mousePressEvent(QMouseEvent* event)
{
	updateSliderPosition(event);
}


void RangeSlider::updateSliderPosition(QMouseEvent* event)
{
	
	
	
	//pos to alpha
	double alpha = util::inverseLinearInterpolation(minPos(), maxPos(), event->pos().x());
	double minSliderAlpha = util::inverseLinearInterpolation(minRange, maxRange, minSliderValue);
	double maxSliderAlpha = util::inverseLinearInterpolation(minRange, maxRange, maxSliderValue);

	if (minSliderAlpha < alpha && alpha < maxSliderAlpha) {

	}


	double value = util::linearInterpolate(minRange, maxRange, alpha);
	
	double distanceToMin = std::abs(minSliderValue - value);
	double distanceToMax = std::abs(maxSliderValue - value);
	if (distanceToMin < distanceToMax) {
		setMinSliderValue(value);
	}
	else {
		setMaxSliderValue(value);
	}
	update();
}

int RangeSlider::maxPos()
{
	return rect().right() - 11;
}

int RangeSlider::minPos()
{
	return rect().left() + 11;
}

