#include "component_pickup.h"

/* DEPENDENCIES: AnimationColorChange, GameObject, Timer, ActionDeleteOnTouchGameEntityType */

/* Sets pickup component info, with an atlas. */
void
component_pickup_init
(
 	ComponentPickup* self,
	ECS* ecs,
	Texture texture,
	ivec2 frameSize,
	ivec2 atlasSize,
	vec2 size,
	vec2 physicsSize,
	vec3 position,
	f32 friction,
	f32 velocityMax,
	vec3 velocity,
	f32 height,
	u32 time,
	u32 fadeThreshold,
	GameEntityType type
)
{
	ComponentGameObject* gameObject;
	ComponentPhysics* physics;
	ComponentActionDeleteOnTouchGameEntityType* actionDeleteOnTouchGameEntityType;
	ComponentActionDeleteOnTimer* actionDeleteOnTimer;
	
	gameObject = ecs_component_get(ecs, ECS_COMPONENT_GAME_OBJECT, self->component.id);
	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);
	actionDeleteOnTimer = ecs_component_get(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TIMER, self->component.id);
	actionDeleteOnTouchGameEntityType = ecs_component_get(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TOUCH_GAME_ENTITY_TYPE, self->component.id);
	
	component_game_object_atlas_init
	(
		gameObject,
		ecs,
		texture,
		frameSize,
		atlasSize,
		size,
		physicsSize,
		position,
		friction,
		velocityMax,
		COMPONENT_PICKUP_GAME_OBJECT_IS_BOUNCE,
		COMPONENT_PICKUP_GAME_OBJECT_IS_AFFECTED_BY_GRAVITY,
		COMPONENT_PICKUP_GAME_OBJECT_IS_SOLID
	);

	glm_vec3_copy(velocity, physics->velocity);
	
	gameObject->height = height;

	component_action_delete_on_touch_game_entity_type_init(actionDeleteOnTouchGameEntityType, type);

	component_action_delete_on_timer_init(actionDeleteOnTimer, time);

	self->fadeThreshold = fadeThreshold;
}

/* Runs on pickup add. */
void
component_pickup_add(ComponentPickup* self, ECS* ecs)
{
	ComponentAnimationColorChange* animationColorChange;

	ecs_component_add(ecs, ECS_COMPONENT_GAME_OBJECT, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TIMER, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TOUCH_GAME_ENTITY_TYPE, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TIMER, self->component.id);
	
	ecs_component_add(ecs, ECS_COMPONENT_ACTION_GRIPPED, self->component.id);

	animationColorChange = ecs_component_add(ecs, ECS_COMPONENT_ANIMATION_COLOR_CHANGE, self->component.id);

	animationColorChange->component.isDisabled = true;
}

/* Runs on pickup deletion. */
void
component_pickup_delete(ComponentPickup* self, ECS* ecs)
{
	ecs_component_delete(ecs, ECS_COMPONENT_GAME_OBJECT, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TOUCH_GAME_ENTITY_TYPE, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_ANIMATION_COLOR_CHANGE, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TIMER, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_ACTION_GRIPPED, self->component.id);
}

/* Ticks pickup. */
void
component_pickup_tick(ComponentPickup* self, ECS* ecs)
{
	ComponentActionDeleteOnTimer* actionDeleteOnTimer;

	actionDeleteOnTimer = ecs_component_get(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TIMER, self->component.id);

	if 
	(
		!self->isHitFadeThreshold &&
		actionDeleteOnTimer->value == self->fadeThreshold
	)
	{
		ComponentAnimationColorChange* animationColorChange;

		animationColorChange = ecs_component_get(ecs, ECS_COMPONENT_ANIMATION_COLOR_CHANGE, self->component.id);
		
		animationColorChange->component.isDisabled = false;

		component_animation_color_change_init
		(
			animationColorChange, 
			COMPONENT_PICKUP_ANIMATION_COLOR_CHANGE_TO_COLOR,
			COMPONENT_PICKUP_ANIMATION_COLOR_CHANGE_FROM_COLOR,
			actionDeleteOnTimer->value,
			COMPONENT_PICKUP_ANIMATION_COLOR_CHANGE_MODE
		);

		self->isHitFadeThreshold = true;
	}
}
