#include "anim.hpp"
#include "tokens.hpp"
#include <qpoint.h>

namespace ZShell::config {

QEasingCurve AnimEasing::buildCurve(const QList<qreal>& points) {
	QEasingCurve curve(QEasingCurve::BezierSpline);
	for (int i = 0; i + 5 < points.size(); i += 6) {
		QPointF c1(points[i], points[i + 1]);
		QPointF c2(points[i + 2], points[i + 3]);
		QPointF end(points[i + 4], points[i + 5]);
		curve.addCubicBezierSegment(c1, c2, end);
	}
	return curve;
}

void AnimEasing::bindCurves(AnimCurves* curves) {
	m_curves = curves;
	connect(
		curves,
		&AnimCurves::propertiesChanged,
		this,
		&AnimEasing::rebuildCurves);
	rebuildCurves();
}

void AnimEasing::rebuildCurves() {
	if (!m_curves) return;
	m_emphasized = buildCurve(m_curves->emphasized());
	m_emphasizedAccel = buildCurve(m_curves->emphasizedAccel());
	m_emphasizedDecel = buildCurve(m_curves->emphasizedDecel());
	m_standard = buildCurve(m_curves->standard());
	m_standardAccel = buildCurve(m_curves->standardAccel());
	m_standardDecel = buildCurve(m_curves->standardDecel());
	m_expressiveFastSpatial = buildCurve(m_curves->expressiveFastSpatial());
	m_expressiveDefaultSpatial =
		buildCurve(m_curves->expressiveDefaultSpatial());
	m_expressiveSlowSpatial = buildCurve(m_curves->expressiveSlowSpatial());
	m_expressiveFastEffects = buildCurve(m_curves->expressiveFastEffects());
	m_expressiveDefaultEffects =
		buildCurve(m_curves->expressiveDefaultEffects());
	m_expressiveSlowEffects = buildCurve(m_curves->expressiveSlowEffects());
	emit curvesChanged();
}

} // namespace ZShell::config
