#include "component_shop_button.h"

static void _button_init(ComponentShopButton* self, ECS* ecs, vec3 position);
static void _price_string_get(ComponentShopButton* self, ECS* ecs, char* string);
static void _count_string_get(ComponentShopButton* self, ECS* ecs, char* string);
static void _text_set(ComponentShopButton* self, ECS* ecs);

/* Gets the string for the price text. */
static void
_price_string_get(ComponentShopButton* self, ECS* ecs, char* string)
{
	ComponentShopItem* shopItem;

	shopItem = ecs_component_get(ecs, ECS_COMPONENT_SHOP_ITEM, self->shopItemID);

	switch (shopItem->type)
	{
		case SHOP_ITEM_UPGRADE_LIFETIME:
		case SHOP_ITEM_UPGRADE_REDIRECTS:
		case SHOP_ITEM_UPGRADE_AMMO:
		case SHOP_ITEM_HEALTH:
			snprintf
			(
				string,
				SHOP_BUTTON_TEXT_MAX,
				"%u",
				shopItem->price
			);
			break;
		case SHOP_ITEM_FEED:
			strncpy(string, "ALL", SHOP_BUTTON_TEXT_MAX);
			break;
		default:
			break;
	}
}

/* Gets the string for the count text. */
static void
_count_string_get(ComponentShopButton* self, ECS* ecs, char* string)
{
	ComponentShopItem* shopItem;

	shopItem = ecs_component_get(ecs, ECS_COMPONENT_SHOP_ITEM, self->shopItemID);

	switch (shopItem->type)
	{
		case SHOP_ITEM_UPGRADE_LIFETIME:
		case SHOP_ITEM_UPGRADE_REDIRECTS:
		case SHOP_ITEM_UPGRADE_AMMO:
			snprintf
			(
				string,
				SHOP_BUTTON_TEXT_MAX,
				"%u",
				shopItem->purchaseCount
			);
			break;
		case SHOP_ITEM_HEALTH:
		case SHOP_ITEM_FEED:
			strncpy(string, "---", SHOP_BUTTON_TEXT_MAX);
			break;
		default:
			break;
	}
}

/* Sets the text. */
static void
_text_set(ComponentShopButton* self, ECS* ecs)
{
	ComponentText* priceText;
	ComponentText* countText;
	char priceString[SHOP_BUTTON_TEXT_MAX];
	char countString[SHOP_BUTTON_TEXT_MAX];

	priceText = ecs_component_get(ecs, ECS_COMPONENT_TEXT, self->priceTextID);
	countText = ecs_component_get(ecs, ECS_COMPONENT_TEXT, self->countTextID);

	_price_string_get(self, ecs, priceString);
	_count_string_get(self, ecs, countString);

	component_text_string_set(priceText, priceString, strlen(priceString));
	component_text_string_set(countText, countString, strlen(countString));
}

/* Initializes the button aspect. */
static void
_button_init(ComponentShopButton* self, ECS* ecs, vec3 position)
{
	ComponentButton* button;
	ComponentSprite* sprite;
	ComponentShopItem* shopItem;

	button = ecs_component_get(ecs, ECS_COMPONENT_BUTTON, self->component.id);
	shopItem = ecs_component_get(ecs, ECS_COMPONENT_SHOP_ITEM, self->shopItemID);

	component_button_init
	(
		button,
		ecs,
		ecs->game->resources.textures[TEXTURE_ICONS],
		(s32*)ICON_FRAME_SIZE,
		(s32*)ICON_ATLAS_SIZE,
		(f32*)SHOP_BUTTON_SIZE,
		position
	);

	sprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->component.id);

	switch (shopItem->type)
	{
		case SHOP_ITEM_UPGRADE_LIFETIME:
			sprite->atlas.index = ICON_SHOP_LIFETIME;
			break;
		case SHOP_ITEM_UPGRADE_REDIRECTS:
			sprite->atlas.index = ICON_SHOP_REDIRECTS;
			break;
		case SHOP_ITEM_UPGRADE_AMMO:
			sprite->atlas.index = ICON_SHOP_AMMO;
			break;
		case SHOP_ITEM_HEALTH:
			sprite->atlas.index = ICON_SHOP_HEALTH;
			break;
		case SHOP_ITEM_FEED:
			sprite->atlas.index = ICON_SHOP_ELEMENTAL;
			break;
		default:
			break;
	}

	sprite->position[2] += SHOP_BUTTON_SPRITE_Z_OFFSET;
}

/* Initializes a button component. */
void
component_shop_button_init
(
	ComponentShopButton* self, 
	ECS* ecs,
	vec3 position,
	u32 shopItemID
)
{
	vec3 countIconPosition;
	vec3 countStreakPosition;
	vec3 countTextPosition;
	vec3 priceIconPosition;
	vec3 priceStreakPosition;
	vec3 priceTextPosition;
	char priceString[SHOP_BUTTON_TEXT_MAX];
	char countString[SHOP_BUTTON_TEXT_MAX];

	self->streakID = ecs_entity_add(ecs);
	self->priceStreakID = ecs_entity_add(ecs);
	self->countStreakID = ecs_entity_add(ecs);
	self->priceIconID = ecs_entity_add(ecs);
	self->countIconID = ecs_entity_add(ecs);
	self->priceTextID = ecs_entity_add(ecs);
	self->countTextID = ecs_entity_add(ecs);

	self->shopItemID = shopItemID;

	_button_init(self, ecs, position);

	glm_vec3_copy(position, priceStreakPosition);
	glm_vec3_copy(position, countStreakPosition);
	glm_vec3_copy(position, priceIconPosition);
	glm_vec3_copy(position, countIconPosition);
	glm_vec3_copy(position, priceTextPosition);
	glm_vec3_copy(position, countTextPosition);

	glm_vec3_add(priceStreakPosition, (f32*)SHOP_BUTTON_PRICE_STREAK_OFFSET, priceStreakPosition);
	glm_vec3_add(countStreakPosition, (f32*)SHOP_BUTTON_COUNT_STREAK_OFFSET, countStreakPosition);
	glm_vec3_add(priceIconPosition, (f32*)SHOP_BUTTON_PRICE_ICON_OFFSET, priceIconPosition);
	glm_vec3_add(countIconPosition, (f32*)SHOP_BUTTON_COUNT_ICON_OFFSET, countIconPosition);
	glm_vec3_add(priceTextPosition, (f32*)SHOP_BUTTON_PRICE_TEXT_OFFSET, priceTextPosition);
	glm_vec3_add(countTextPosition, (f32*)SHOP_BUTTON_COUNT_TEXT_OFFSET, countTextPosition);

	entity_streak_square_init(ecs, self->streakID, position, (f32*)SHOP_BUTTON_STREAK_SQUARE_SIZE);
	entity_streak_init(ecs, self->priceStreakID, priceStreakPosition, (f32*)SHOP_BUTTON_STREAK_SIZE);
	entity_streak_init(ecs, self->countStreakID, countStreakPosition, (f32*)SHOP_BUTTON_STREAK_SIZE);
	entity_icon_init(ecs, self->priceIconID, priceIconPosition, (f32*)SHOP_BUTTON_ICON_SIZE, ICON_GOODIES);
	entity_icon_init(ecs, self->countIconID, countIconPosition, (f32*)SHOP_BUTTON_ICON_SIZE, ICON_COUNT);

	_price_string_get(self, ecs, priceString);
	_count_string_get(self, ecs, countString);

	entity_text_init
	(
		ecs, 
		self->priceTextID, 
		&ecs->game->resources.fonts[FONT_HUD],
		priceString,
		strlen(priceString),
		-1,
		priceTextPosition,
		(f32*)COLOR_OPAQUE
	);

	entity_text_init
	(
		ecs, 
		self->countTextID, 
		&ecs->game->resources.fonts[FONT_HUD],
		countString,
		strlen(countString),
		-1,
		countTextPosition,
		(f32*)COLOR_OPAQUE
	);
		
}

/* Disables a shop button component. */
void 
component_shop_button_disable(ComponentShopButton* self, ECS* ecs)
{
	ComponentButton* button;
	ComponentSprite* buttonSprite;
	ComponentSprite* streakSprite;
	ComponentSprite* priceStreakSprite;
	ComponentSprite* priceIconSprite;
	ComponentSprite* countStreakSprite;
	ComponentSprite* countIconSprite;
	ComponentText* priceText;
	ComponentText* countText;

	button = ecs_component_get(ecs, ECS_COMPONENT_BUTTON, self->component.id);
	buttonSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->component.id);
	streakSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->streakID);
	priceStreakSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->priceStreakID);
	countStreakSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->countStreakID);
	countIconSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->countIconID);
	priceIconSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->priceIconID);
	priceText = ecs_component_get(ecs, ECS_COMPONENT_TEXT, self->priceTextID);
	countText = ecs_component_get(ecs, ECS_COMPONENT_TEXT, self->countTextID);

	glm_vec4_copy((f32*)COLOR_TRANSPARENT, buttonSprite->color);
	glm_vec4_copy((f32*)COLOR_TRANSPARENT, streakSprite->color);
	glm_vec4_copy((f32*)COLOR_TRANSPARENT, priceStreakSprite->color);
	glm_vec4_copy((f32*)COLOR_TRANSPARENT, countStreakSprite->color);
	glm_vec4_copy((f32*)COLOR_TRANSPARENT, countIconSprite->color);
	glm_vec4_copy((f32*)COLOR_TRANSPARENT, priceIconSprite->color);
	glm_vec4_copy((f32*)COLOR_TRANSPARENT, priceText->color);
	glm_vec4_copy((f32*)COLOR_TRANSPARENT, countText->color);

	button->component.isDisabled = true;
	self->component.isDisabled = true;
}


/* Disables a shop button component. */
void 
component_shop_button_enable(ComponentShopButton* self, ECS* ecs)
{
	ComponentButton* button;
	ComponentSprite* buttonSprite;
	ComponentSprite* streakSprite;
	ComponentSprite* priceStreakSprite;
	ComponentSprite* priceIconSprite;
	ComponentSprite* countStreakSprite;
	ComponentSprite* countIconSprite;
	ComponentText* priceText;
	ComponentText* countText;

	button = ecs_component_get(ecs, ECS_COMPONENT_BUTTON, self->component.id);
	buttonSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->component.id);
	streakSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->streakID);
	priceStreakSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->priceStreakID);
	countStreakSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->countStreakID);
	countIconSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->countIconID);
	priceIconSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->priceIconID);
	priceText = ecs_component_get(ecs, ECS_COMPONENT_TEXT, self->priceTextID);
	countText = ecs_component_get(ecs, ECS_COMPONENT_TEXT, self->countTextID);

	glm_vec4_copy((f32*)COLOR_OPAQUE, buttonSprite->color);
	glm_vec4_copy((f32*)COLOR_OPAQUE, streakSprite->color);
	glm_vec4_copy((f32*)COLOR_OPAQUE, priceStreakSprite->color);
	glm_vec4_copy((f32*)COLOR_OPAQUE, countStreakSprite->color);
	glm_vec4_copy((f32*)COLOR_OPAQUE, countIconSprite->color);
	glm_vec4_copy((f32*)COLOR_OPAQUE, priceIconSprite->color);
	glm_vec4_copy((f32*)COLOR_OPAQUE, priceText->color);
	glm_vec4_copy((f32*)COLOR_OPAQUE, countText->color);

	button->component.isDisabled = false;
	self->component.isDisabled = false;
}

/* Runs on a button component being added. */
void 
component_shop_button_add(ComponentShopButton* self, ECS* ecs)
{
	ecs_component_add(ecs, ECS_COMPONENT_BUTTON, self->component.id);
}

/* Runs on a button component being added. */
void 
component_shop_button_delete(ComponentShopButton* self, ECS* ecs)
{
	ecs_component_delete(ecs, ECS_COMPONENT_BUTTON, self->component.id);

	ecs_entity_delete(ecs, self->streakID);
	ecs_entity_delete(ecs, self->priceStreakID);
	ecs_entity_delete(ecs, self->countStreakID);
	ecs_entity_delete(ecs, self->countIconID);
	ecs_entity_delete(ecs, self->priceIconID);
	ecs_entity_delete(ecs, self->countTextID);
	ecs_entity_delete(ecs, self->priceTextID);
}

/* Ticks button component. */
void 
component_shop_button_tick(ComponentShopButton* self, ECS* ecs)
{
	ComponentButton* button;

	button = ecs_component_get(ecs, ECS_COMPONENT_BUTTON, self->component.id);

	if (self->isUpdate)
	{
		_text_set(self, ecs);
		self->isUpdate = false;
	}

	if (button->state == BUTTON_PRESSED)
	{
		ComponentShopItem* shopItem;

		shopItem = ecs_component_get(ecs, ECS_COMPONENT_SHOP_ITEM, self->shopItemID);
		
		shopItem->isBought = true;
		self->isUpdate = true;
	}
}
