#include "pch.h"
#include "SellScreen.h"

#include "colors.h"
#include "GameManager.h"
#include "SellSreenRow.h"
#include "../ScreenManager.h"
#include "utils.h"
SellScreen::SellScreen(const std::string& filePath, Vector2f pos, Vector2f size, TextureManager* manager): Screen(filePath, pos, size, manager) {
	const Vector2f sellScreenSize { 492, 396 };
	// Vector2f fuelScreenCenter = Vector2f { utils::GetViewport().x / 2 - fuelScreenSize.x / 2, utils::GetViewport().y / 2 - fuelScreenSize.y / 2 };
	const Vector2f ScreenCenter = Vector2f { utils::GetViewport().x / 2, utils::GetViewport().y / 2 };

	const Vector2f sellScreenCenter = ScreenCenter - sellScreenSize / 2;
	m_BuySound = new SoundEffect { "sound/buy.wav" };

	const Vector2f closeButtonOffset = Vector2f { 460, 396 - 14 };
	Vector2f closeButtonPos = sellScreenCenter + closeButtonOffset;
	closeButtonPos.y -= 18;
	GuiButton* closeFuelButton = new GuiButton { "gui/close.png", closeButtonPos, Vector2f { 0, 0 }, true, TextureManager::GetInstance() };
	closeFuelButton->SetOnClick([this]()
	{
		ScreenManager::GetInstance()->CloseScreen();
	});
	this->AddElement(closeFuelButton);

	std::string HeaderText = "Cargo Bay      Qty.       Value          Total Value"; //Dont ask
	Vector2f headerTextPos = Vector2f { 285, 400 };
	GuiText* m_HeaderText = new GuiText { headerTextPos, HeaderText, "fonts/Arial.ttf", 14, Colors::LIGHTGRAY };
	this->AddElement(m_HeaderText);


	std::string sellAllPath = "gui/sell/sellall.png";
	Vector2f sellAllPos = Vector2f { 410, 100 };

	GuiButton* sellAllButton = new GuiButton { sellAllPath, sellAllPos, Vector2f { 97, 27 }, false, TextureManager::GetInstance() };
	sellAllButton->SetOnClick([this]()
	{
		this->SellAll();
		m_BuySound->Play(0);
	});
	this->AddElement(sellAllButton);

	std::string totalSell = "Balls";
	Vector2f totalSellPos = Vector2f { 590, 105 };
	m_TotalMoneyText = new GuiText { totalSellPos, totalSell, "fonts/Arial.ttf", 18, Colors::GREEN };
	this->AddElement(m_TotalMoneyText);
}
SellScreen::~SellScreen() {
	for (SellSreenRow* row : m_Rows) {
		delete row;
	}
	delete m_BuySound;
}
void SellScreen::Draw() const {
	Screen::Draw();
	for (const SellSreenRow* row : m_Rows) {
		row->Draw();
	}

	// m_TotalMoneyText->Draw();
	utils::SetColor(Colors::GREEN);
	utils::DrawLine(550, 130, 650, 130);

}
void SellScreen::Update(float elapsedSecs) {
	Screen::Update(elapsedSecs);
	if (m_AreRowsDirty) {
		//remake rows
		m_AreRowsDirty = false;

		for (SellSreenRow* row : m_Rows) {
			delete row;
		}
		m_Rows.clear();

		std::vector<ItemStack *> items = GameManager::GetInstance().GetInventory()->GetItems();
		int totalWorth { 0 };
		int index { 0 }; //Fix to not overshoot when an empty item is in the inventory
		for (ItemStack* item : items) {
			if (item->m_ItemType == InventoryItem::Empty) {
				continue;
			}
			SellSreenRow* row = new SellSreenRow(*item, Vector2f { 250, (float)360 - index * 30 }, Vector2f { 400, 30 });
			totalWorth += item->m_Quantity * PlayerInventory::GetItemValue(item->m_ItemType);
			m_Rows.push_back(std::move(row));
			index++;
		}


		m_TotalMoneyText->ChangeText("$" + std::to_string(GameManager::GetInstance().GetInventory()->GetTotalValue()));
		int characterCount = m_TotalMoneyText->GetText().length();
		m_TotalMoneyText->SetPosition(Vector2f { (float)610 - characterCount * 10, 105 });
	}

	for (SellSreenRow* row : m_Rows) {
		row->Update(elapsedSecs);
	}
}
void SellScreen::MarkDirty() {
	m_AreRowsDirty = true;
}
void SellScreen::SellAll() {
	PlayerInventory* inventory = GameManager::GetInstance().GetInventory();
	std::vector<ItemStack *> items = inventory->GetItems();
	if (items.empty()) {
		return;
	}

	int totalWorth { 0 };
	for (ItemStack* item : items) {
		if (item->m_ItemType == InventoryItem::Empty) {
			continue;
		}
		totalWorth += item->m_Quantity * PlayerInventory::GetItemValue(item->m_ItemType);
		inventory->RemoveItem(item);
	}
	GameManager::GetInstance().IncreaseMoney(totalWorth);
}
