#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 * 10;

	PixelCallback *root, *child;
	TransformCenter nodeTop = { .centerX = width / 2, .centerY = height / 4, };
	TransformCenter nodeLeft = { .centerX = width / 4, .centerY = height * 3 / 4, };
	TransformCenter nodeRight = { .centerX = width * 3 / 4, .centerY = height * 3 / 4, };

	PixelCallback* segment;
	LineSegment segmentRight = {
		.aX = nodeTop.centerX, .aY = nodeTop.centerY,
		.bX = nodeRight.centerX, .bY = nodeRight.centerY,
		.width = 30, .color = 0xFFFF00FF,
	};
	LineSegment segmentLeft = {
		.aX = nodeLeft.centerX, .aY = nodeLeft.centerY,
		.bX = nodeTop.centerX, .bY = nodeTop.centerY,
		.width = 30, .color = 0xFF0088FF,
	};

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

	FrameCallback* frameCallbacks[] = {
		/* Rotate left */
		FrameRange_setup((FrameRange){
			.startFrame = FPS * 1, .endFrame = FPS * 3,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &nodeTop, .endValue = &nodeLeft,
			.value = root->callback_self,
			.interpolate = (f_interpolate)Interpolate_TransformCenter,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &nodeRight, .endValue = &nodeTop,
			.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,
		}),
		/* Rotate right */
		NULL,
		FrameRange_setup((FrameRange){
			.startFrame = FPS * 6, .endFrame = FPS * 8,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &nodeLeft, .endValue = &nodeTop,
			.value = root->callback_self,
			.interpolate = (f_interpolate)Interpolate_TransformCenter,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &nodeTop, .endValue = &nodeRight,
			.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;
}
