#include "component_control_heart.h"

/* DEPENDENCIES: Heart */

static void _heart_use(ComponentControlHeart* self, ECS* ecs);

/* Uses the heart. */
static void
_heart_use(ComponentControlHeart* self, ECS* ecs)
{
	ComponentActionWave* actionWave;
	ComponentActionSpawn* actionSpawn;
	ComponentHeart* heart;

	heart = ecs_component_get(ecs, ECS_COMPONENT_HEART, self->component.id);
	actionWave = ecs_component_get(ecs, ECS_COMPONENT_ACTION_WAVE, self->spawnID);
	actionSpawn = ecs_component_get(ecs, ECS_COMPONENT_ACTION_SPAWN, self->spawnID);

	if (heart->concurrentUses > 0)
		actionSpawn->levelBonus++;

	heart->uses++;
	heart->concurrentUses++;

	self->cooldown = COMPONENT_CONTROL_HEART_COOLDOWN_MAX;

	actionWave->isSpawnWave = true;

	sound_play(&ecs->game->resources.sounds[SOUND_HEART], SOUND_PRIORITY);
}

/* Initializes control heart component. */
void
component_control_heart_init(ComponentControlHeart* self, u32 spawnID)
{
	self->spawnID = spawnID;
}

/* Ticks control heart.. */
void
component_control_heart_tick(ComponentControlHeart* self, ECS* ecs)
{
	ComponentActionSpawn* actionSpawn;

	actionSpawn = ecs_component_get(ecs, ECS_COMPONENT_ACTION_SPAWN, self->spawnID);

	/* if we're no longer in combat, no more concurrent uses */
	if (actionSpawn && !actionSpawn->isCombat)
	{
		ComponentHeart* heart;

		heart = ecs_component_get(ecs, ECS_COMPONENT_HEART, self->component.id);

		heart->concurrentUses = 0;
	}

	if (self->cooldown > 0)
	{
		self->cooldown--;
		return;
	}

	if (input_press(&ecs->game->input, INPUT_HEART))
		_heart_use(self, ecs);
}
