/* 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 "Animation.h"
#include "AnimationObject.h"
#include "AnimationMove.h"
#include "AnimationInterpolate.h"
#include <stdio.h>

int
main() {
	Animation anim = Animation_new(512, 512, 120);

	// Background
	struct AO_CheckerPattern chp1 = {
		.squareSize = 32,
		.colorA = 0xFF000000, .colorB = 0xFFFF00FF,
	};
	struct AM_Linear ml1 = {
		.startRow = 0, .startCol = 0,
		.dRow = -1, .dCol = -1,

		.callback = AO_CheckerPattern, .priv = &chp1,
	};
	Animation_pushEvent(&anim, 0, 120, AM_Linear, &ml1);

	// Image
	FILE* fbclc = fopen("bclc.ppm", "r");
	struct AO_Image bclc;
	struct AM_Linear ml10;
	if (fbclc != NULL) {
		bclc.img = ppm_read(fbclc);
		bclc.noRepeat = true;
		bclc.zoom = 0.4;

		ml10 = (struct AM_Linear){
			.startRow = 0, .startCol = 0,
			.dRow = 0.5, .dCol = 0.5,

			.callback = AO_Image, .priv = &bclc,
		};
		Animation_pushEvent(&anim, 0, 100, AM_Linear, &ml10);
	}

	// Line
	struct AO_Line l1start = {
		.a = -8, .b = 6, .c = -128,
		.width = 0.5,
		.color = 0xFF50F0F5
	};
	struct AO_Line l1end = {
		.a = 8, .b = -4, .c = 0,
		.width = 10,
		.color = 0xFFF550F0
	};
	struct AO_Line l1;

	struct A_Interpolate i1 = {
		.startFrame = 0, .endFrame = 120,
		.value = &l1, .initialState = &l1start, .finalState = &l1end,
		.interpolate = AO_LineInterpolate,

		.callback = AO_Line, .priv = &l1,
	};
	Animation_pushEvent(&anim, 0, 120, A_Interpolate, &i1);

	// Yellow square
	struct AO_Square s1 = {
		.width = 20, .height = 20, .color = 0xFFFFFF00,
	};
	struct AM_Spin ms1 = {
		.theta = 3.14159 / 15, .centerCol = 10, .centerRow = 10,

		.callback = AO_Square, .priv = &s1,
	};
	struct AM_Linear ml3 = {
		.startRow = 100, .startCol = 100,
		.dRow = 3, .dCol = 1,

		.callback = AM_Spin, .priv = &ms1,
	};
	Animation_pushEvent(&anim, 0, 80, AM_Linear, &ml3);

	// White circle
	struct AO_Circle s4 = {
		.radius = 20, .color = 0xFFFFFFFF,
	};
	struct AM_Linear ml4 = {
		.startRow = 40, .startCol = 40,
		.dRow = 2, .dCol = 2,

		.callback = AO_Circle, .priv = &s4,
	};
	Animation_pushEvent(&anim, 0, 80, AM_Linear, &ml4);

	// Green square
	struct AO_Square s2 = {
		.width = 35, .height = 50, .color = 0xA009FAA5,
	};
	struct AM_Linear ml2 = {
		.startRow = 20, .startCol = 5,
		.dRow = -4, .dCol = -2,
		.wrapCoordinates = true,

		.callback = AO_Square, .priv = &s2,
	};
	Animation_pushEvent(&anim, 30, 110, AM_Linear, &ml2);

	// Segment
	struct AO_HorSegment ls1 = {
		.left = -40, .right = 40,
		.width = 3, .color = 0xFFA0A0A0,
	};
	struct AM_Spin ms3 = {
		.theta = 3.14159 / 10,
		.centerRow = 0, .centerCol = 0,

		.callback = AO_HorSegment, .priv = &ls1,
	};
	struct AM_Linear ml6 = {
		.startRow = 0, .startCol = 0,
		.dRow = 4, .dCol = 4,

		.callback = AM_Spin, .priv = &ms3,
	};
	Animation_pushEvent(&anim, 0, 120, AM_Linear, &ml6);

	Animation_render(&anim);

	fclose(fbclc);
	RGBImage_delete(&bclc.img);
	Animation_delete(&anim);

	return 0;
}
