/* gcc -std=c99 *.c -lm && ./a.out | mpv --no-correct-pts --container-fps-override=30 -
 * gcc -std=c99 *.c -lm && ./a.out | ffmpeg -r 30 -i - output.mp4
 */

#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/FrameRender.h"
#include "FrameRender/P_Checker.h"
#include "FrameRender/P_Circle.h"
#include "FrameRender/P_Image.h"
#include "FrameRender/P_Square.h"
#include "FrameRender/P_TransformCenter.h"
#include "FrameRender/P_TransformRotate.h"
#include "FrameRender/PixelCallback.h"
#include "RGBImage.h"
#include <stdlib.h>

# define M_PI		3.14159265358979323846	/* pi */

int
main() {
	u32 width = 512;
	u32 height = 512;

	FILE* fbclc = fopen("bclc.ppm", "rb");
	RGBImage bclc = ppm_read(fbclc);
	fclose(fbclc);

	PixelCallback* callbacks[] = {
		Checker_setup((Checker){
			.squareSize = 64, .colorA = 0xFF000000, .colorB = 0xFFFF00FF,
		}),
		TransformCenter_setup((TransformCenter){
			.centerX = width / 2, .centerY = height / 2,
		}),
		Square_setup((Square){
			.size = 100, .color = 0xFFFFFF00,
		}),
		NULL,
		TransformRotate_setup((TransformRotate){
			.angle = M_PI / 8,
		}),
		Square_setup((Square){
			.size = 200, .color = 0xFF00FFFF,
		}),
		NULL,
		TransformCenter_setup((TransformCenter){
			.centerX = width * 3/4, .centerY = height / 2,
		}),
		Circle_setup((Circle){
			.radius = 50, .color = 0x80FFFFFF,
		}),
		NULL,
		TransformCenter_setup((TransformCenter){
			.centerX = width / 8, .centerY = height * 3/5,
		}),
		Image_setup((Image){
			.img = bclc,
			.scale = 0.2,
		}),
	};
	u32 sizeCallbacks = sizeof(callbacks) / sizeof(*callbacks);

	TransformCenter img1 = { .centerX = width / 8, .centerY = height * 3/5 },
	                img2 = { .centerX = height / 8, .centerY = width / 8 };

	TransformRotate r1 = { .angle = M_PI / 8 }, r2 = { .angle = M_PI * 2 };

	Circle c1 = { .radius = 50, .color = 0x00FFFFFF, }, c2 = { .radius = 50, .color = 0xFFFFFFFF, };

	FrameCallback* frameCallbacks[] = {
		Interpolate_setup((Interpolate){
			.startValue = &c1, .endValue = &c2,
			.value = callbacks[8]->callback_self,
			.interpolate = (f_interpolate)Interpolate_Circle,
		}),
		FrameRange_setup((FrameRange){
			.startFrame = 25, .endFrame = 125,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &img1, .endValue = &img2,
			.value = callbacks[10]->callback_self,
			.interpolate = (f_interpolate)Interpolate_TransformCenter,
		}),
		NULL,
		FrameRange_setup((FrameRange){
			.startFrame = 0, .endFrame = 50,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &r1, .endValue = &r2,
			.value = callbacks[4]->callback_self,
			.interpolate = (f_interpolate)Interpolate_TransformRotate,
		}),
		NULL,
		Draw_setup((Draw){
			.callbacks = (const PixelCallback**)callbacks, .sizeCallbacks = sizeCallbacks,
		}),
	};
	u32 sizeFrameCallbacks = sizeof(frameCallbacks) / sizeof(*frameCallbacks);

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

	RGBImage_delete(&bclc);
	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;
}
