#include "component_enemy.h"

/* DEPENDENCIES: Character, ActionDamage, ActionOnDeleteDropGoodies */

/* Sets enemy component info, with an atlas. */
void
component_enemy_init
(
	ComponentEnemy* self,
	ECS* ecs,
	Texture texture,
	ivec2 frameSize,
	ivec2 atlasSize,
	vec2 size,
	vec2 physicsSize,
	vec3 position,
	f32 friction,
	f32 velocityMax,
	u32 healthValue,
	bool isBounce,
	bool isAffectedByGravity,
	bool isSolid,
	u32 goodieCount,
	u32 levelValue,
	Vector* entityList
)
{
	ComponentCharacter* character;
	ComponentActionOnDeleteDropGoodies* actionOnDeleteDropGoodies;
	ComponentActionOnOutOfBoundsDelete* actionOnOutOfBoundsDelete;
	ComponentActionDamage* actionDamage;
	ComponentLevel* level;
	
	character = ecs_component_get(ecs, ECS_COMPONENT_CHARACTER, self->component.id);
	actionOnDeleteDropGoodies = ecs_component_get(ecs, ECS_COMPONENT_ACTION_ON_DELETE_DROP_GOODIES, self->component.id);
	actionDamage = ecs_component_get(ecs, ECS_COMPONENT_ACTION_DAMAGE, self->component.id);
	actionOnOutOfBoundsDelete = ecs_component_get(ecs, ECS_COMPONENT_ACTION_ON_OUT_OF_BOUNDS_DELETE, self->component.id);
	level = ecs_component_get(ecs, ECS_COMPONENT_LEVEL, self->component.id);

	component_character_init
	(
		character,
		ecs,
		texture,
		frameSize,
		atlasSize,
		size,
		physicsSize,
		position,
		friction,
		velocityMax,
		COMPONENT_ENEMY_ALLEGIANCE,
		healthValue,
		isBounce,
		isAffectedByGravity,
		isSolid
	);

	component_action_on_delete_drop_goodies_init(actionOnDeleteDropGoodies, goodieCount);

	component_action_damage_init
	(
		actionDamage, 
		COMPONENT_ENEMY_ACTION_DAMAGE_ATTACK_TYPE, 
		COMPONENT_ENEMY_ACTION_DAMAGE_TIMER, 
		COMPONENT_ENEMY_ACTION_DAMAGE_VALUE
	);

	component_action_on_out_of_bounds_delete_init
	(
		actionOnOutOfBoundsDelete, 
		ENEMY_ACTION_ON_OUT_OF_BOUNDS_DELETE_RECTANGLE
	);

	component_level_init(level, levelValue);

	self->entityList = entityList;
}

/* Runs on character add. */
void
component_enemy_add(ComponentEnemy* self, ECS* ecs)
{
	ecs_component_add(ecs, ECS_COMPONENT_CHARACTER, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_ACTION_ON_OUT_OF_BOUNDS_DELETE, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_ACTION_DELETE_ON_DEAD, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_ACTION_ON_DELETE_DROP_GOODIES, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_ACTION_DAMAGE, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_LEVEL, self->component.id);
}

/* Runs on enemy deletion. */
void
component_enemy_delete(ComponentEnemy* self, ECS* ecs)
{
	if (self->entityList)
	{
		for (s32 i = 0; i < (s32)self->entityList->count; i++)
		{
			u32 id;

			id = *(u32*)vector_get(self->entityList, i);

			if (id == self->component.id)
				vector_remove(self->entityList, i);

		}
	}

	ecs_component_delete(ecs, ECS_COMPONENT_ACTION_DELETE_ON_DEAD, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_CHARACTER, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_ACTION_ON_OUT_OF_BOUNDS_DELETE, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_ACTION_ON_DELETE_DROP_GOODIES, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_ACTION_DAMAGE, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_LEVEL, self->component.id);
}
