#include "component_upgrades.h"

/* DEPENDENCIES: Redirects, Ammo, ActionShoot */

/* Sets values for various components relating to upgrades. */
static void
_component_upgrades_set(ComponentUpgrades* self, ECS* ecs)
{
	ComponentActionShoot* actionShoot;
	ComponentRedirects* redirects;
	ComponentAmmo* ammo;

	actionShoot = ecs_component_get(ecs, ECS_COMPONENT_ACTION_SHOOT, self->component.id);
	redirects = ecs_component_get(ecs, ECS_COMPONENT_REDIRECTS, self->component.id);
	ammo = ecs_component_get(ecs, ECS_COMPONENT_AMMO, self->component.id);

	if (self->currentLevels[UPGRADE_AMMO] != self->previousLevels[UPGRADE_AMMO])
	{
		s32 difference;

		difference = (self->currentLevels[UPGRADE_AMMO] - self->previousLevels[UPGRADE_AMMO]);

		ammo->value += difference;
		ammo->max += difference;
	}

	if (self->currentLevels[UPGRADE_REDIRECTS] != self->previousLevels[UPGRADE_REDIRECTS])
	{
		s32 difference;

		difference = (self->currentLevels[UPGRADE_REDIRECTS] - self->previousLevels[UPGRADE_REDIRECTS]);

		redirects->value += difference;
		redirects->max += difference;
	}
	

	if (self->currentLevels[UPGRADE_LIFETIME] != self->previousLevels[UPGRADE_LIFETIME])
		actionShoot->lifetimeBonus = (self->currentLevels[UPGRADE_LIFETIME] * COMPONENT_UPGRADES_LIFETIME_LEVEL_MULTIPLIER); 
}

/* Adds an upgrades component. */
void
component_upgrades_add(ComponentUpgrades* self, ECS* ecs)
{
	memcpy(self->currentLevels, COMPONENT_UPGRADES_LEVELS_DEFAULT, sizeof(u32) * UPGRADE_COUNT);
	memcpy(self->previousLevels, self->currentLevels, sizeof(u32) * UPGRADE_COUNT);

	_component_upgrades_set(self, ecs);
}

/* Ticks upgrade component. */
void
component_upgrades_tick(ComponentUpgrades* self, ECS* ecs)
{
	if (memcmp(self->currentLevels, self->previousLevels, sizeof(u32) * UPGRADE_COUNT) != 0)
		_component_upgrades_set(self, ecs);

	memcpy(self->previousLevels, self->currentLevels, sizeof(u32) * UPGRADE_COUNT);
}
