
#pragma once

#include <vulkan/vulkan.h>
#include "PathUtils.h"
#include "Utils.h"

class PizzaManager
{
public:
	uint32_t numberOfPizzasToBeDelivered = 5;
	glm::vec2 scooterLocation = glm::vec2(0,0);
	std::vector<glm::vec2> houseLocations;
	uint32_t indexOfHouseWithoutDeliveryRequest = 0;
	std::shared_ptr<HudObject> hudObject;
	std::shared_ptr<PizzaInHandObject> pizzaObjectInHand;
	bool robotAtePizza = false;
	bool allPizzasDelivered = false;
	bool playerHasPizzaInHand = true;
	uint32_t numberOfPizzasDelivered = 0;
	std::vector<bool> pizzaDeliveredToHouse;

	PizzaManager() 
	{
		// mark all house except one as wanting pizza. Assumes 6 houses
		for (int i = 0; i < 6; i++)
			pizzaDeliveredToHouse.push_back(false);
		pizzaDeliveredToHouse[indexOfHouseWithoutDeliveryRequest] = true; // this is the house that does not want pizza , they already have pizza from other delivery service
	}

	bool update(const glm::vec3& inPlayerPosition, const std::vector<glm::vec3>& inRobotLocations)
	{
		// Check if robot got close enough to scooter to eat pizza >> Game over
		for (auto robotLocation : inRobotLocations)
		{
			glm::vec2 robotLocation2d(robotLocation.x, robotLocation.z);
			if (glm::length(robotLocation2d - scooterLocation) < 3)
			{
				robotAtePizza = true;
				return true;
			}
		}

		// check if player is close to scooter and put pizza in hand
		glm::vec2 playerPosition2d(inPlayerPosition.x, inPlayerPosition.z);
		if (glm::length(playerPosition2d - scooterLocation) < 3)
		{
			playerHasPizzaInHand = true;
			pizzaObjectInHand->renderThisPizza = true;
		}

		// check if player is close to house that needs pizza and deliver pizza if in hand
		for (int i = 0; i < houseLocations.size(); i++)
		{
			if (glm::length(playerPosition2d - houseLocations[i]) < 2 && playerHasPizzaInHand && !pizzaDeliveredToHouse[i])
			{
				pizzaDeliveredToHouse[i] = true;
				numberOfPizzasDelivered++;
				playerHasPizzaInHand = false;
				hudObject->updateRemainingPizzas(numberOfPizzasToBeDelivered - numberOfPizzasDelivered);
				pizzaObjectInHand->renderThisPizza = false;
			}
		}

		// check if all pizzas were delivered
		if (numberOfPizzasDelivered == numberOfPizzasToBeDelivered)
		{
			allPizzasDelivered = true;
			return true; // game over
		}

		// game keeps running
		return false;	
	}
};