#include "component_action_wave.h"

static void _timer_set(ComponentActionWave* self, ECS* ecs);
static void _wave_add(ComponentActionWave* self, ECS* ecs);

/* Sets the wave timer. */
static void
_timer_set(ComponentActionWave* self, ECS* ecs)
{
	self->timer = 1800;
}

/* Adds a wave to the spawn component. */
static void
_wave_add(ComponentActionWave* self, ECS* ecs)
{
	ComponentActionSpawn* actionSpawn;
	ComponentLevel* level;

	actionSpawn = ecs_component_get(ecs, ECS_COMPONENT_ACTION_SPAWN, self->component.id);
	level = ecs_component_get(ecs, ECS_COMPONENT_LEVEL, self->component.id);

	component_action_spawn_wave_add(actionSpawn, ecs, (SpawnWave*)&SPAWN_WAVES[self->index]);

	self->index++;

	if (self->index >= COMPONENT_ACTION_WAVE_COUNT)
	{
		self->index = 0;
		level->value++;
	}

	_timer_set(self, ecs);
}

/* Runs on add wave. */
void
component_action_wave_add(ComponentActionWave* self, ECS* ecs)
{
	_timer_set(self, ecs);
}

/* Ticks action_wave component. */
void
component_action_wave_tick(ComponentActionWave* self, ECS* ecs)
{
	if (self->timer > 0)
	{
		self->timer--;

		if (self->timer == 0)
			self->isSpawnWave = true;
	}

	if (self->isSpawnWave)
	{
		self->timer = 0;
		self->isSpawnWave = false;

		_wave_add(self, ecs);
	}
}
