#include "AnimationInterpolate.h"
#include "AnimationObject.h"
#include "AnimationMove.h"
#include <string.h>

#define NUM_INTERPOLATE(a, b, perc) (a * (1.0 - (perc)) + b * (perc))

color
colorInterpolate(color ina, color inb, double percentage) {
	ARGB a, b;
	ARGB_set(&a, ina);
	ARGB_set(&b, inb);

	a.a = NUM_INTERPOLATE(a.a, b.a, percentage);
	a.r = NUM_INTERPOLATE(a.r, b.r, percentage);
	a.g = NUM_INTERPOLATE(a.g, b.g, percentage);
	a.b = NUM_INTERPOLATE(a.b, b.b, percentage);

	return a.a << 24 | a.r << 16 | a.g << 8 | a.b;
}

ARGB
A_Interpolate(const Animation* anim, PixelRendererParams a) {
	struct A_Interpolate* in = a.priv;

	if (in->interpolate == NULL) {
		fprintf(stderr, "Interpolation function cannot be NULL!\n");
		ARGB_set(&a.pixel, 0xFFFF00FF);
		return a.pixel;
	}

	double perc = (double)(a.frame.current - a.frame.start) / (a.frame.end - a.frame.start);
	if (perc < 0.0) perc = 0.0;
	if (perc > 1.0) perc = 1.0;

	/*
		 ?               ?
		a*(1-p) + b*p = x
		?
		a*(1-p1) + b*p1 = c   ==> a = (c - b*p1) / (1 - p1)
		     ?            ?
		c*(1-p2) + b*p2 = x

		?                    ?
		a*(1-p) + b*p = c*(1-p2) + b*p2
		                                           ?
		(c - b*p1) / (1 - p1) * (1-p) + b*p = c*(1-p2) + b*p2
		(c - b*p1) / (1 - p1) * (1-p) + b*p = c - c*p2 + b*p2
		(c - b*p1) / (1 - p1) * (1-p) + b*p = c + (b - c) * p2
		((c - b*p1) / (1 - p1) * (1-p) + b*p - c) / (b - c) = p2
		simplified:
		(p - p1) / (1 - p1) = p2
	 */

	in->interpolate(in->value, in->goalValue, (perc - in->prevPercentage) / (1.0 - in->prevPercentage));
	if (a.row == 0 && a.col == 0) {
		fprintf(stderr, "@ %f : %f -> %f ==> %d \n", perc, perc - in->prevPercentage, (perc - in->prevPercentage) / (1.0 - in->prevPercentage), ((struct AM_Linear*)in->value)->startCol);
	}

	in->prevPercentage = perc;

	return a.pixel;
}

void
AO_LineInterpolate(void* valueLine, const void* finalLine, double percentage) {
	struct AO_Line* value = valueLine;
	const struct AO_Line* final = finalLine;

	value->a = NUM_INTERPOLATE(value->a, final->a, percentage);
	value->b = NUM_INTERPOLATE(value->b, final->b, percentage);
	value->c = NUM_INTERPOLATE(value->c, final->c, percentage);
	value->width = NUM_INTERPOLATE(value->width, final->width, percentage);
	value->color = colorInterpolate(value->color, final->color, percentage);
}

void
AM_LinearInterpolate(void* valueIn, const void* finalIn, double percentage) {
	struct AM_Linear* value = valueIn;
	const struct AM_Linear* final = finalIn;

	value->startRow = NUM_INTERPOLATE(value->startRow, final->startRow, percentage);
	value->startCol = NUM_INTERPOLATE(value->startCol, final->startCol, percentage);
	value->dRow = NUM_INTERPOLATE(value->dRow, final->dRow, percentage);
	value->dCol = NUM_INTERPOLATE(value->dCol, final->dCol, percentage);
	value->wrapCoordinates = NUM_INTERPOLATE(value->wrapCoordinates, final->wrapCoordinates, percentage);
}

void
AM_SpinInterpolate(void* valueIn, const void* finalIn, double percentage) {
	struct AM_Spin* value = valueIn;
	const struct AM_Spin* final = finalIn;

	value->theta = NUM_INTERPOLATE(value->theta, final->theta, percentage);
	value->dTheta = NUM_INTERPOLATE(value->dTheta, final->dTheta, percentage);
	value->centerRow = NUM_INTERPOLATE(value->centerRow, final->centerRow, percentage);
	value->centerCol = NUM_INTERPOLATE(value->centerCol, final->centerCol, percentage);
}
