#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_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/RGBImage.h"
#include <stdlib.h>

# define M_PI		3.14159265358979323846	/* pi */

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

	FILE* fbclc = fopen("renders/bclc.ppm", "rb");
	if (fbclc == NULL) {
		fprintf(stderr, "Couldn't open image file!\n");
		return 1;
	}
	RGBImage bclc = ppm_read(fbclc);
	fclose(fbclc);

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

	PixelCallback* square;
	TransformRotate r1 = { .angle = M_PI / 8 }, r2 = { .angle = M_PI * 4 / 1.1 };

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

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

	FrameCallback* frameCallbacks[] = {
		/* Fade in circle */
		Interpolate_setup((Interpolate){
			.startValue = &c1, .endValue = &c2,
			.value = circle->callback_self,
			.interpolate = (f_interpolate)Interpolate_Circle,
		}),
		/* Move image */
		FrameRange_setup((FrameRange){
			.startFrame = 25, .endFrame = 125,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &img1, .endValue = &img2,
			.value = img->callback_self,
			.interpolate = (f_interpolate)Interpolate_TransformCenter,
		}),
		/* Rotate square */
		NULL,
		FrameRange_setup((FrameRange){
			.startFrame = 0, .endFrame = 50,
		}),
		Interpolate_setup((Interpolate){
			.startValue = &r1, .endValue = &r2,
			.value = square->callback_self,
			.interpolate = (f_interpolate)Interpolate_TransformRotate,
		}),
		/* Render */
		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;
}
