#include "F_Interpolate.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void Interpolate_callback(Interpolate* self, struct FrameContext* fc) {
	if (self->interpolate == NULL) {
		fprintf(stderr, "Interpolation function cannot be NULL!\n");
		return;
	}

	double p = (double)(fc->frame - fc->startFrame) / (fc->endFrame - fc->startFrame);

	if (0.0 <= p && p <= 1.0)
		self->interpolate(self->value, self->startValue, self->endValue, p);
}

FrameCallback* Interpolate_setup(Interpolate obj) {
	FrameCallback* fca = malloc(sizeof(struct FrameCallback));

	fca->callback = (FrameCallback_callback)Interpolate_callback;
	fca->callback_self = malloc(sizeof(obj));
	memcpy(fca->callback_self, &obj, sizeof(obj));

	return fca;
}

#define NUM_INTERPOLATE(a, b, perc) ((a) == (b) ? (a) : ((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;
}

void Interpolate_Square(Square* value, const Square* start, const Square* end, double progress) {
	value->size = NUM_INTERPOLATE(start->size, end->size, progress);
	value->color = colorInterpolate(start->color, end->color, progress);
}

void Interpolate_Circle(Circle* value, const Circle* start, const Circle* end, double progress) {
	value->radius = NUM_INTERPOLATE(start->radius, end->radius, progress);
	value->color = colorInterpolate(start->color, end->color, progress);
}

void Interpolate_Checker(Checker* value, const Checker* start, const Checker* end, double progress) {
	value->squareSize = NUM_INTERPOLATE(start->squareSize, end->squareSize, progress);
	value->colorA = colorInterpolate(start->colorA, end->colorA, progress);
	value->colorB = colorInterpolate(start->colorB, end->colorB, progress);
}

void Interpolate_Image(Image* value, const Image* start, const Image* end, double progress) {
	value->scale = NUM_INTERPOLATE(start->scale, end->scale, progress);
	if (start->img.img != end->img.img)
		fprintf(stderr, "Refusing to interpolate between images!");
}

void Interpolate_LineSegment(LineSegment* value, const LineSegment* start, const LineSegment* end, double progress) {
	value->aX = NUM_INTERPOLATE(start->aX, end->aX, progress);
	value->aY = NUM_INTERPOLATE(start->aY, end->aY, progress);
	value->bX = NUM_INTERPOLATE(start->bX, end->bX, progress);
	value->bY = NUM_INTERPOLATE(start->bY, end->bY, progress);
	value->width = NUM_INTERPOLATE(start->width, end->width, progress);
	value->color = colorInterpolate(start->color, end->color, progress);
}

void Interpolate_TransformCenter(TransformCenter* value, const TransformCenter* start, const TransformCenter* end, double progress) {
	value->centerX = NUM_INTERPOLATE(start->centerX, end->centerX, progress);
	value->centerY = NUM_INTERPOLATE(start->centerY, end->centerY, progress);
}

void Interpolate_TransformRotate(TransformRotate* value, const TransformRotate* start, const TransformRotate* end, double progress) {
	value->angle = NUM_INTERPOLATE(start->angle, end->angle, progress);
}
