#include "pch.h"
#include "GameManager.h"

#include "Gui/Screens/ScreenManager.h"

GameManager* GameManager::m_pInstance = nullptr;


GameManager& GameManager::GetInstance() {
	if (m_pInstance == nullptr) {
		m_pInstance = new GameManager();
	}
	return *m_pInstance;
}
void GameManager::DestroyInstance() {
	delete m_pInstance->m_pInventory;
	delete m_pInstance->m_FuelLowSound;
	delete m_pInstance;
}
void GameManager::SetMainScreen(MainScreen* pMainScreen) {
	m_pMainScreen = pMainScreen;
	//TODO: not the best but ¯\_(ツ)_/¯
}
void GameManager::SetPlayer(Player* pPlayer) {
	m_pPlayer = pPlayer;
}
void GameManager::SetFuel(float fuel) {
	m_Fuel = fuel;
	//Limit to 0 - MAXa
	
}
float GameManager::GetFuel() const {
	return m_Fuel;
}
void GameManager::DecreaseFuel(float fuel) {
	m_Fuel -= fuel;
	m_Fuel = std::max(0.0f, m_Fuel);
}
void GameManager::AddFuel(float fuel) {
	m_Fuel += fuel;
	if(m_Fuel > this->GetMaxFuel()) {
		m_Fuel = (float)this->GetMaxFuel();
	}
}
void GameManager::SetHullIntegrity(int hullIntegrity) {
	m_HullIntegrity = hullIntegrity;
}
int GameManager::GetHullIntegrity() const {
	return m_HullIntegrity;
}
void GameManager::DamageHull(int damage) {
	m_HullIntegrity -= damage;
}
void GameManager::SetScore(int score) {
	m_Score = score;
}
int GameManager::GetScore() const {
	return m_Score;
}
void GameManager::IncreaseScore(int score) {
	m_Score += score;
}
void GameManager::SetMoney(int money) {
	m_Money = money;
}
int GameManager::GetMoney() const {
	return m_Money;
}
void GameManager::IncreaseMoney(int money) {
	m_Money += money;
}
void GameManager::Update(float) {
	m_pMainScreen->SetFuelMeterValue(this->GetMaxFuel() - m_Fuel);
	m_pMainScreen->SetHullMeterValue((float)m_HullIntegrity);
	m_pMainScreen->SetScore(std::to_string(m_Score));
	m_pMainScreen->SetMoney("$ " + std::to_string(m_Money));

	if (m_HullIntegrity <= 0 or m_Fuel <= 0) {
		m_pPlayer->Die();
		m_GameOver = true;
		ScreenManager::GetInstance()->OpenScreen(ScreenManager::m_GameOverScreen);
	}
	if(!m_GameOver) {
		if(m_Fuel <= 20) {
			if(m_FuelLowSound->IsPlaying() == false) {
				m_FuelLowSound->Play(1);
			}
		}
	}
}
int GameManager::GetMaxFuel() {
	return 100; //TODO: change if i ever add upgrades
}
PlayerInventory* GameManager::GetInventory() {
	return m_pInventory;
}
Player* GameManager::GetPlayer() {
	return m_pPlayer;
}
GameManager::GameManager() {
	m_pInventory = new PlayerInventory();
	m_FuelLowSound = new SoundEffect{ "sound/fuel_low.wav" };
}



