#include "component_action_lock.h"

/* DEPENDENCIES: Physics, Return */

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

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

	actionReturn->component.isDisabled = true;
	self->isLocked = true;
}

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

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

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

/* Initialize lock component. */
void
component_action_lock_init(ComponentActionLock* self, u32 sender)
{
	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);
	}
	else
	{
		if (self->isLocked)
			component_action_lock_unlock(self, ecs);
	}
}
