#include "pch.h"
#include "FuelScreen.h"

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

	const Vector2f fuelScreenCenter { ScreenCenter - fuelScreenSize / 2 };

	m_BuySound = new SoundEffect { "sound/buy.wav" };

	const Vector2f closeButtonOffset = Vector2f { 460, 396 - 14 };
	Vector2f closeButtonPos = fuelScreenCenter + 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);

	const Vector2f oneDollarButtonPos = Vector2f { 451, 287 };
	GuiButton* fiveDollarButton = new GuiButton { "gui/fuel/5dollars.png", oneDollarButtonPos, Vector2f { 0, 0 }, true, TextureManager::GetInstance() };
	fiveDollarButton->SetOnClick([this]()
	{
		if (GameManager::GetInstance().GetMoney() > 5) {
			GameManager::GetInstance().AddFuel(5);
			GameManager::GetInstance().IncreaseMoney(-5);
			m_BuySound->Play(0);
		}
		else {
			std::cout << "Not enough money" << std::endl;
		}
	});
	this->AddElement(fiveDollarButton);

	const Vector2f tenDollarButtonPos = oneDollarButtonPos + Vector2f { 113, -1 };
	GuiButton* tenDollarButton = new GuiButton { "gui/fuel/10dollars.png", tenDollarButtonPos, Vector2f { 0, 0 }, true, TextureManager::GetInstance() };
	tenDollarButton->SetOnClick([this]()
	{
		if (GameManager::GetInstance().GetMoney() > 10) {
			GameManager::GetInstance().AddFuel(10);
			GameManager::GetInstance().IncreaseMoney(-10);
			m_BuySound->Play(0);
		}
		else {
			std::cout << "Not enough money" << std::endl;
		}
	});
	this->AddElement(tenDollarButton);

	const Vector2f twentyFiveDollarButtonPos = oneDollarButtonPos + Vector2f { 0, -89 };
	GuiButton* twentyFiveDollarButton = new GuiButton { "gui/fuel/25dollars.png", twentyFiveDollarButtonPos, Vector2f { 0, 0 }, true, TextureManager::GetInstance() };
	twentyFiveDollarButton->SetOnClick([this]()
	{
		if (GameManager::GetInstance().GetMoney() > 25) {
			GameManager::GetInstance().AddFuel(25);
			GameManager::GetInstance().IncreaseMoney(-25);
			m_BuySound->Play(0);
		}
		else {
			std::cout << "Not enough money" << std::endl;
		}
	});
	this->AddElement(twentyFiveDollarButton);

	const Vector2f fiftyDollarButtonPos = twentyFiveDollarButtonPos + Vector2f { 114, 0 };
	GuiButton* fiftyDollarButton = new GuiButton { "gui/fuel/50dollars.png", fiftyDollarButtonPos, Vector2f { 0, 0 }, true, TextureManager::GetInstance() };
	fiftyDollarButton->SetOnClick([this]()
	{
		if (GameManager::GetInstance().GetMoney() > 50) {
			GameManager::GetInstance().AddFuel(50);
			GameManager::GetInstance().IncreaseMoney(-50);
			m_BuySound->Play(0);

		}
		else {
			std::cout << "Not enough money" << std::endl;
		}
	});
	this->AddElement(fiftyDollarButton);

	const Vector2f fillTankButtonPos = Vector2f { 450, 108 };
	GuiButton* fillTankButton = new GuiButton { "gui/fuel/fillTank.png", fillTankButtonPos, Vector2f { 0, 0 }, true, TextureManager::GetInstance() };
	fillTankButton->SetOnClick([this]()
	{
		if (GameManager::GetInstance().GetFuel() < GameManager::GetInstance().GetMaxFuel() - 2) {
			int fuelNeeded = static_cast<int>(GameManager::GetInstance().GetMaxFuel() - GameManager::GetInstance().GetFuel());
			if (fuelNeeded <= GameManager::GetInstance().GetMoney()) {
				GameManager::GetInstance().AddFuel((float)fuelNeeded);
				GameManager::GetInstance().IncreaseMoney(-fuelNeeded);
				m_BuySound->Play(0);
			}
			else {
				std::cout << "Not enough money" << std::endl;
			}
		}
		else {
			std::cout << "Don't need fuel" << std::endl;
		}
	});
	this->AddElement(fillTankButton);


	Vector2f offset { 50, 320 };
	m_MoneyText = new GuiText { fuelScreenCenter + offset, "$ 20", "fonts/Arial.ttf", 20, Colors::YELLOW };

}

FuelScreen::~FuelScreen() {
	delete m_MoneyText;
	delete m_BuySound;
};

void FuelScreen::Draw() const {

	int currentFuel = (int) GameManager::GetInstance().GetFuel();
	int maxFuel = GameManager::GetInstance().GetMaxFuel();

	utils::SetColor(Color4f { 99.f / 255.f, 82.f / 255.f, 36.f / 255.f, 1.f });
	Vector2f fuelBarPos = Vector2f { 275, 135 };
	const int MAX_HEIGHT = 210;
	Vector2f fuelBarSize = Vector2f { 40, utils::map(currentFuel, 0.f, maxFuel, 0.f, MAX_HEIGHT) };
	utils::FillRect(Rectf { fuelBarPos, fuelBarSize });

	Screen::Draw();
	m_MoneyText->Draw();

}
void FuelScreen::Update(float elapsedSecs) {
	Screen::Update(elapsedSecs);
	m_MoneyText->ChangeText("$ " + std::to_string(GameManager::GetInstance().GetMoney())); //Not the best but will work
}
