#pragma once
#include <memory>
#include <functional>
#include <unordered_map>

#include "GameManager.h"
#include "SFML/Graphics/RenderWindow.hpp"

class Scene;

class SceneManager {
private:
    sf::RenderWindow& gameWindow;
    GameManager& gameManager;
    std::unordered_map<std::string, std::function<std::unique_ptr<Scene>()>> sceneFactories;
    std::unique_ptr<Scene> currentScene;
    std::string activeScene;
    std::string pendingScene;
    bool pendingSceneChange = false;
public:
    SceneManager(sf::RenderWindow& gameWindow, GameManager& gameManager);

    void applySceneChange();

    void addScene(const std::string& name, const std::function<std::unique_ptr<Scene>()>& sceneFactory) {
        sceneFactories.emplace(name, sceneFactory);
    }

    void update(float deltaTime) const;
    void render(sf::RenderWindow& window) const;

    std::string getCurrentScene() const {
        return activeScene;
    };

    GameManager& getGameManager() const { return gameManager; }
    sf::RenderWindow& getGameWindow() const { return gameWindow; }

    void changeScene(const std::string& sceneName);

    void eventHandler(sf::Event& event) const;

    void _quit();
};