#include "component_behavior_boomerang.h"

/* DEPENDENCIES: Physics, Health */

static void _begin(ComponentBehaviorBoomerang* self, ECS* ecs);
static void _boomerang(ComponentBehaviorBoomerang* self, ECS* ecs);

/* Resets after a boomerang is finished. */
static void
_begin(ComponentBehaviorBoomerang* self, ECS* ecs)
{
	ComponentPhysics* physics;
	ComponentPhysics* targetPhysics;
	ComponentAnimationLevitate* animationLevitate;

	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);
	animationLevitate = ecs_component_get(ecs, ECS_COMPONENT_ANIMATION_LEVITATE, self->component.id);
	
	targetPhysics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->targetID);

	/* this should not happen. */
	if (!targetPhysics)
		return;

	self->angle = ATAN
	(
		physics->position[0],
		targetPhysics->position[0],
		physics->position[1],
		targetPhysics->position[1]
	);

	/* Fling the opposite way as a heads up. */
	physics->velocity[0] = -cos(self->angle) * self->initialSpeed;
	physics->velocity[1] = -sin(self->angle) * self->initialSpeed;

	if (animationLevitate)
	{
		ComponentGameObject* gameObject;

		gameObject = ecs_component_get(ecs, ECS_COMPONENT_GAME_OBJECT, self->component.id);

		gameObject->height = animationLevitate->height;
		animationLevitate->component.isDisabled = true;
	}

	self->isBegin = true;

	self->soundTimer = COMPONENT_BEHAVIOR_BOOMERANG_SOUND_TIMER_MAX;
}

/* A c c e l e r a t e . */
static void
_boomerang(ComponentBehaviorBoomerang* self, ECS* ecs)
{
	ComponentPhysics* physics;

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

	physics->velocity[0] += cos(self->angle) * self->speed;
	physics->velocity[1] += sin(self->angle) * self->speed;

	self->soundTimer--;

	if (self->soundTimer == 0)
	{
		sound_play(&ecs->game->resources.sounds[SOUND_EXCORSANT_SPIN], SOUND_NO_PRIORITY);
		self->soundTimer = COMPONENT_BEHAVIOR_BOOMERANG_SOUND_TIMER_MAX;
	}
}

/* Sets behavior_boomerang component info. */
void
component_behavior_boomerang_init
(
 	ComponentBehaviorBoomerang* self, 
	ECS* ecs,
	f32 chaseSpeed,
	f32 initialSpeed,
	f32 speed,
	u32 timer,
	u32 targetID
)
{
	ComponentBehaviorChase* behaviorChase;
	ComponentLevel* level;
	u32 levelTimer;

	behaviorChase = ecs_component_get(ecs, ECS_COMPONENT_BEHAVIOR_CHASE, self->component.id);
	level = ecs_component_get(ecs, ECS_COMPONENT_LEVEL, self->component.id);

	component_behavior_chase_init(behaviorChase, chaseSpeed, targetID);

	levelTimer = timer - (COMPONENT_BEHAVIOR_BOOMERANG_LEVEL_TIMER_MULTIPLIER * (level->value - 1));

	printf("LEVEL: %i\n", level->value);
	printf("LEVEL TIMER: %i\n", levelTimer);

	levelTimer = MIN(levelTimer, COMPONENT_BEHAVIOR_BOOMERANG_TIMER_MAX);

	self->speed = speed;
	self->initialSpeed = initialSpeed;
	self->timerMax = levelTimer;
	self->timer = self->timerMax;
	self->targetID = targetID;

	printf("TIMER MAX: %i\n", self->timerMax);
}

/* Sets behavior_boomerang component info. */
void
component_behavior_boomerang_add(ComponentBehaviorBoomerang* self, ECS* ecs)
{
	ecs_component_add(ecs, ECS_COMPONENT_BEHAVIOR_CHASE, self->component.id);
}


/* Sets behavior_boomerang component info. */
void
component_behavior_boomerang_delete(ComponentBehaviorBoomerang* self, ECS* ecs)
{
	ecs_component_delete(ecs, ECS_COMPONENT_BEHAVIOR_CHASE, self->component.id);
}

/* Ticks behavior_boomerang component. */
void
component_behavior_boomerang_tick(ComponentBehaviorBoomerang* self, ECS* ecs)
{
	if (self->isBoomerang)
	{
		if (!self->isBegin)
		{
			_begin(self, ecs);
			return;
		}

		_boomerang(self, ecs);
	}
	else
	{

		ComponentBehaviorActInRectangle* behaviorActInRectangle;

		behaviorActInRectangle = ecs_component_get(ecs, ECS_COMPONENT_BEHAVIOR_ACT_IN_RECTANGLE, self->component.id);

		if (!behaviorActInRectangle->isAbleToAct)
			return;

		if (self->timer > 0)
		{
			self->timer--;

			if (self->timer <= 0)
			{
				ComponentBehaviorChase* behaviorChase;

				behaviorChase = ecs_component_get(ecs, ECS_COMPONENT_BEHAVIOR_CHASE, self->component.id);

				behaviorChase->component.isDisabled = true;

				self->timer = 0;
				self->isBoomerang = true;

			}
		}
	}
}
