#include "pch.h"
#include "PlayerInventory.h"

#include <iostream>

#include "Gui/Screens/ScreenManager.h"
#include "Gui/Screens/SellScreen/SellScreen.h"
PlayerInventory::PlayerInventory() {
}
PlayerInventory::~PlayerInventory() {
	for(ItemStack* stack : m_Items) {
		delete stack;
	}
}
void PlayerInventory::ClearInventory() {
	for (ItemStack* stack : m_Items) {
		delete stack;
	}
}
int PlayerInventory::GetTotalValue() const {
	int totalValue{ 0 };
	for (ItemStack* stack : m_Items) {
		totalValue += stack->m_Quantity * GetItemValue(stack->m_ItemType);
	}
	return totalValue;
}
void PlayerInventory::AddItem(const ItemStack& item) {
	dynamic_cast<SellScreen*>(ScreenManager::m_SellScreen)->MarkDirty();
	//Check if its in the array
	for(ItemStack* stack : m_Items) {
		if(stack->m_ItemType == item.m_ItemType) {
			std::cout << "Added item to inventory: Existing Array" << std::endl;
			stack->m_Quantity += item.m_Quantity;
			if(stack->m_Quantity == 2){}
			return;
		}
	}

	m_Items.push_back(new ItemStack(item));
	std::cout << "Added item to inventory: New Item" << std::endl;

	
}
void PlayerInventory::RemoveItem(ItemStack* item) {
	//Remove the item
	for (size_t i = 0; i < m_Items.size(); i++) {
		if(m_Items[i] == item) {
			delete m_Items[i];
			m_Items.erase(m_Items.begin() + i);
			dynamic_cast<SellScreen*>(ScreenManager::m_SellScreen)->MarkDirty();
			return;
		}
	}
}
std::vector<ItemStack*> PlayerInventory::GetItems() const {
	return m_Items;
}
InventoryItem PlayerInventory::GetItemByType(GroundTileType* tile) {
	if(tile == nullptr) {
		return InventoryItem::Empty;
	}
	switch (tile->GetType()) {
	case GroundTileTypes::Bronze:
		return InventoryItem::Bronzium;
	case GroundTileTypes::Air:
		return InventoryItem::Empty;
	case GroundTileTypes::Gold:
		return InventoryItem::Goldium;
	case GroundTileTypes::Iron:
		return InventoryItem::Ironium;
	default:
		return InventoryItem::Empty;
	}
}
std::string PlayerInventory::GetItemName(InventoryItem item) {
	switch (item) {
	case InventoryItem::Bronzium:
		return "Bronzium";
	case InventoryItem::Diamond:
		return "Diamond";
	case InventoryItem::Einsteinium:
		return "Einsteinium";
	case InventoryItem::Emerald:
		return "Emerald";
	case InventoryItem::Goldium:
		return "Goldium";
	case InventoryItem::Ironium:
		return "Ironium";
	case InventoryItem::Platnium:
		return "Platnium";
	case InventoryItem::Ruby:
		return "Ruby";
	case InventoryItem::Silverium:
		return "Silverium";
	case InventoryItem::Special:
		return "Special";
	default:
		return "Empty";
	}
}
int PlayerInventory::GetItemValue(InventoryItem item) {
	switch(item) {
	case InventoryItem::Bronzium:
		return 60;
	case InventoryItem::Ironium:
		return 30;
	case InventoryItem::Silverium:
		return 100;
	case InventoryItem::Goldium:
		return 250;
	case InventoryItem::Platinum:
		return 750;
	case InventoryItem::Einsteinium:
		return 2000;
	case InventoryItem::Emerald:
		return 5000;
	case InventoryItem::Ruby:
		return 20000;
	case InventoryItem::Diamond:
		return 100000;
	default:
		return 0;
	}
}
Texture* PlayerInventory::GetItemIcon(InventoryItem item) {
	std::string texturePath = "gui/sell/icons/";
	std::string itemName = GetItemName(item);
	texturePath += itemName + ".png";
	return TextureManager::GetInstance()->GetTexture(texturePath);
}
