#include "component_action_lock.h"

/* DEPENDENCIES: Physics, Return */

static void _component_action_lock_select_check(ComponentActionLock* self, ECS* ecs);

/* Checks if the mouse has graced a radius around the locked projectile. */
/* If the mouse is moved out of it, a redirect angle is set. */
static void
_component_action_lock_select_check(ComponentActionLock* self, ECS* ecs)
{
	Circle circle;
	vec2 mousePosition;
	ComponentPhysics* physics;

	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);

	circle.position[0] = physics->position[0];
	circle.position[1] = physics->position[1];

	circle.radius = self->selectRadius;

	mouse_world_position_get(&ecs->game->input.mouse, &ecs->game->renderer, mousePosition);

	if (self->isSelected)
	{
		if (!circle_has_point(&circle, mousePosition))
		{
			if (!self->isRedirectAngleGiven)
			{
				self->angle = ATAN
				(
					mousePosition[0],
					circle.position[0],
					mousePosition[1],
					circle.position[1]

				);

				self->isRedirectAngleGiven = true;
			}

			self->isSelected = false;
		}
	
	}
	else
	{
		if (circle_has_point(&circle, mousePosition))
		{
			if (self->isRedirectAngleGiven)
				self->isRedirectAngleGiven = false;
			self->isSelected = true;
		}
	}
}

/* Lock in place. */
void
component_action_lock_lock(ComponentActionLock* self, ECS* ecs)
{
	ComponentPhysics* physics;
	ComponentActionReturn* actionReturn;
	ComponentActionDeleteOnTouchEntity* actionDeleteOnTouchEntity;
	ComponentActionDamage* actionDamage;

	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);
	actionReturn = ecs_component_get(ecs, ECS_COMPONENT_ACTION_RETURN, self->component.id);
	actionDeleteOnTouchEntity = ecs_component_get(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TOUCH_ENTITY, self->component.id);
	actionDamage = ecs_component_get(ecs, ECS_COMPONENT_ACTION_DAMAGE, self->component.id);
	
	glm_vec3_copy(physics->velocity, self->velocity);

	actionDamage->component.isDisabled = true;
	actionReturn->component.isDisabled = true;
	self->isLocked = true;
	self->cooldown = self->cooldownMax;
	actionDeleteOnTouchEntity->component.isDisabled = false;
}

/* Unlocks, from being locked. */
void
component_action_lock_unlock(ComponentActionLock* self, ECS* ecs)
{
	ComponentPhysics* physics;
	ComponentActionReturn* actionReturn;
	ComponentActionDamage* actionDamage;

	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);
	actionReturn = ecs_component_get(ecs, ECS_COMPONENT_ACTION_RETURN, self->component.id);
	actionDamage = ecs_component_get(ecs, ECS_COMPONENT_ACTION_DAMAGE, self->component.id);

	glm_vec3_copy(self->velocity, physics->velocity); 
	actionReturn->component.isDisabled = false;
	actionDamage->component.isDisabled = false;
	
	self->isSelected = false;
	self->isRedirectAngleGiven = false;
	self->isLocked = false;
}

/* Initialize lock component. */
void
component_action_lock_init(ComponentActionLock* self, u32 cooldown, f32 selectRadius, u32 sender)
{
	self->cooldownMax = cooldown;
	self->cooldown = self->cooldownMax;
	self->selectRadius = selectRadius;
	self->sender = sender;
}

/* Ticks lock component. */
/* Remain stationary. */
void 
component_action_lock_tick(ComponentActionLock* self, ECS* ecs)
{
	ComponentPhysics* physics;

	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);

	if (self->isLock)
	{
		if (!self->isLocked)
			component_action_lock_lock(self, ecs);

		glm_vec3_zero(physics->velocity);

		_component_action_lock_select_check(self, ecs);

		self->cooldown--;

		if (self->cooldown <= 0)
		{
			self->cooldown = 0;
			self->isLock = false;
			component_action_lock_unlock(self, ecs);
		}
	}
	else
	{
		if (self->isLocked)
			component_action_lock_unlock(self, ecs);
	}
}
