#include "../AnimationRender/AnimationRender.h"
#include "../AnimationRender/F_Draw.h"
#include "../AnimationRender/F_FrameRange.h"
#include "../AnimationRender/F_Interpolate.h"
#include "../AnimationRender/FrameCallback.h"
#include "../FrameRender/P_Circle.h"
#include "../FrameRender/P_LineSegment.h"
#include "../FrameRender/P_Square.h"
#include "../FrameRender/P_TransformCenter.h"
#include "../FrameRender/PixelCallback.h"
#include <stdlib.h>

int
main() {
	u32 width = 512;
	u32 height = 512;
	const u32 FPS = 60;
	u32 frames = FPS * 5;

	PixelCallback* child;
	TransformCenter childLeft = { .centerX = width / 4, .centerY = height * 3 / 4, };
	TransformCenter childRight = { .centerX = width * 3 / 4, .centerY = height * 3 / 4, };

	PixelCallback* segment;
	LineSegment segmentRight = {
		.aX = width / 2, .aY = height / 4,
		.bX = width * 3 / 4, .bY = height * 3 / 4,
		.width = 20, .color = 0xFFFF00FF,
	};
	LineSegment segmentLeft = {
		.aX = width / 2, .aY = height / 4,
		.bX = width / 4, .bY = height * 3 / 4,
		.width = 20, .color = 0xFF0088FF,
	};

	PixelCallback* callbacks[] = {
		/* Background */
		Square_setup((Square){
			.size = width, .color = 0xFFF0F0F0,
		}),
		/* Connecting line */
		NULL,
		segment = LineSegment_setup(segmentRight),
		/* Root node */
		TransformCenter_setup((TransformCenter){
			.centerX = width / 2, .centerY = height / 4,
		}),
		Circle_setup((Circle){
			.radius = 60, .color = 0xFFFF0000,
		}),
		/* Child node */
		NULL,
		child = TransformCenter_setup(childRight),
		Circle_setup((Circle){
			.radius = 60, .color = 0xFF00FF00,
		}),
	};
	u32 sizeCallbacks = sizeof(callbacks) / sizeof(*callbacks);

	FrameCallback* frameCallbacks[] = {
		/* Move right to left */
		FrameRange_setup((FrameRange){
			.startFrame = frames / 3 - FPS, .endFrame = frames / 3,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &childRight, .endValue = &childLeft,
			.value = child->callback_self,
			.interpolate = (f_interpolate)Interpolate_TransformCenter,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &segmentRight, .endValue = &segmentLeft,
			.value = segment->callback_self,
			.interpolate = (f_interpolate)Interpolate_LineSegment,
		}),
		/* Move left to right */
		NULL,
		FrameRange_setup((FrameRange){
			.startFrame = frames * 2 / 3 - FPS, .endFrame = frames * 2 / 3,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &childLeft, .endValue = &childRight,
			.value = child->callback_self,
			.interpolate = (f_interpolate)Interpolate_TransformCenter,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &segmentLeft, .endValue = &segmentRight,
			.value = segment->callback_self,
			.interpolate = (f_interpolate)Interpolate_LineSegment,
		}),
		/* Render */
		NULL,
		Draw_setup((Draw){
			.callbacks = (const PixelCallback**)callbacks, .sizeCallbacks = sizeCallbacks,
		}),
	};
	u32 sizeFrameCallbacks = sizeof(frameCallbacks) / sizeof(*frameCallbacks);

	RenderAnimation(width, height, frames, (const FrameCallback**)frameCallbacks, sizeFrameCallbacks);

	for (u32 i = 0; i < sizeCallbacks; ++i) {
		if (callbacks[i] == NULL) continue;
		free(callbacks[i]->callback_self);
		free(callbacks[i]);
	}
	for (u32 i = 0; i < sizeFrameCallbacks; ++i) {
		if (frameCallbacks[i] == NULL) continue;
		free(frameCallbacks[i]->callback_self);
		free(frameCallbacks[i]);
	}

	return 0;
}
