#include "component_behavior_approach_and_shoot.h"

static void _approach_and_shoot(ComponentBehaviorApproachAndShoot* self, ECS* ecs);

/* DEPENDENCIES: ActionShoot, Chase, Physics */

/* If we're range in of the target, tick the timer down till it's time to blast. */
static void
_approach_and_shoot(ComponentBehaviorApproachAndShoot* self, ECS* ecs)
{
	ComponentPhysics* physics;
	ComponentPhysics* targetPhysics;
	ComponentActionShoot* actionShoot;

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

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

	self->timer--;

	if (self->timer <= 0)
	{
		self->timer = self->timerMax;
		actionShoot->isFire = true;
	}
}

/* Sets behavior_approach_and_shoot component info. */
void
component_behavior_approach_and_shoot_init
(
	ComponentBehaviorApproachAndShoot* self, 
	ECS* ecs,
	ProjectileInit projectileInit,
	f32 speed,
	f32 projectileSpeed,
	f32 offset,
	f32 height,
	f32 distance,
	u32 timer,
	u32 targetID
)
{
	ComponentActionShoot* actionShoot;
	ComponentBehaviorChase* behaviorChase;

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

	component_behavior_chase_init(behaviorChase, 50, targetID);

	component_action_shoot_init
	(
		actionShoot, 
		projectileInit, 
		projectileSpeed, 
		offset, 
		height
	);

	self->distance = distance;
	self->timerMax = timer;
	self->timer =  self->timerMax;
}

/* Sets behavior_approach_and_shoot component info. */
void
component_behavior_approach_and_shoot_add(ComponentBehaviorApproachAndShoot* self, ECS* ecs)
{
	ecs_component_add(ecs, ECS_COMPONENT_BEHAVIOR_CHASE, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_ACTION_SHOOT, self->component.id);
}

/* Sets behavior_approach_and_shoot component info. */
void
component_behavior_approach_and_shoot_delete(ComponentBehaviorApproachAndShoot* self, ECS* ecs)
{
	ecs_component_delete(ecs, ECS_COMPONENT_BEHAVIOR_CHASE, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_ACTION_SHOOT, self->component.id);
}

/* Ticks behavior_approach_and_shoot component. */
void
component_behavior_approach_and_shoot_tick(ComponentBehaviorApproachAndShoot* self, ECS* ecs)
{	
	ComponentPhysics* physics;
	ComponentPhysics* targetPhysics;
	ComponentBehaviorChase* behaviorChase;
	ComponentBehaviorActInRectangle* behaviorActInRectangle;
	f32 distance;

	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);
	targetPhysics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->targetID);
	behaviorChase = ecs_component_get(ecs, ECS_COMPONENT_BEHAVIOR_CHASE, self->component.id);
	behaviorActInRectangle = ecs_component_get(ecs, ECS_COMPONENT_BEHAVIOR_ACT_IN_RECTANGLE, self->component.id);

	distance = DISTANCE_2D
	(
		physics->position[0],
		targetPhysics->position[0],
		physics->position[1],
		targetPhysics->position[1]
	);

	if 
	(
		distance <= self->distance &&
		behaviorActInRectangle->isAbleToAct
	)
	{
		_approach_and_shoot(self, ecs);
		behaviorChase->component.isDisabled = true;
	}
	else
	{
		self->timer =  self->timerMax;
		behaviorChase->component.isDisabled = false;
	}
	
}
