#include "component_animation_color_change.h"

/* Sets animation_color_change component info. */
void
component_animation_color_change_init
(
	ComponentAnimationColorChange* self, 
	vec4 toColor, 
	vec4 fromColor,
	u32 time,
	AnimationColorChangeMode mode
)
{
	glm_vec4_copy(toColor, self->toColor);
	glm_vec4_copy(fromColor, self->fromColor);
	self->timerMax = time;
	self->timer = self->timerMax;
	self->mode = mode;
	self->isDone = false;
}

/* Ticks animation_color_change component. */
void
component_animation_color_change_tick(ComponentAnimationColorChange* self, ECS* ecs)
{
	DrawObject* drawObject;
	f32 progress;
	vec4 color;
	vec4 colorDifference;
	vec4 colorStep;

	drawObject = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->component.id);
	
	if (!drawObject)
		drawObject = ecs_component_get(ecs, ECS_COMPONENT_TEXT, self->component.id);

	if (!drawObject)
		return;

	if (self->isDone)
	{

		switch (self->mode)
		{
			case ANIMATION_COLOR_CHANGE_MODE_RETURN:
				glm_vec4_copy(self->fromColor, drawObject->color);
				break;
			case ANIMATION_COLOR_CHANGE_MODE_STICK:
			default:
				break;
		}
		return;
	}

	progress = ((f32)self->timer / self->timerMax);

	glm_vec4_copy(self->fromColor, color);
	glm_vec4_sub(color, self->toColor, colorDifference);
	glm_vec4_scale(colorDifference, (1.0f - progress), colorStep);
	glm_vec4_sub(color, colorStep, color);

	glm_vec4_copy(color, drawObject->color);

	if (self->timer <= 0)
	{
		self->isDone = true;
		return;
	}

	self->timer--;
}
