#include "StateManager.h"
#include "../Engine.h"

void StateManager::PushState(std::unique_ptr<BaseState> state, Engine& engine)
{
    if (!states.empty()) { states.back()->Exit(engine); }
    
    states.push_back(std::move(state));
    states.back()->Enter(engine);
}

void StateManager::PopState(Engine& engine)
{
    if (states.empty())
    {
        SDL_Log("StateManager::PopState() warning: PopState() called on empty stack");
        return;
    }
    
    states.back()->Exit(engine);
    states.pop_back();
    
    if (!states.empty())
    {
        states.back()->Enter(engine);
    }
    else
    {
        SDL_Log("StateManager::PopState() warning: Stack is now empty after PopState");
    }
}

void StateManager::ChangeState(std::unique_ptr<BaseState> state, Engine& engine)
{
    if (!states.empty())
    {
        states.back()->Exit(engine);
        states.pop_back();
    }
    
    states.push_back(std::move(state));
    states.back()->Enter(engine);
}

void StateManager::HandleEvents(Engine& engine, SDL_Event& e)
{
    if (!states.empty())
    {
        states.back()->HandleEvents(engine, e);
    }
}

void StateManager::Update(Engine& engine, float dt)
{
    if (!states.empty())
    {
        states.back()->Update(engine, dt);
    }
}

void StateManager::Render(Engine& engine)
{
    // TODO: Check if this should be here
    //for (const auto& state : states)
    //{
    //    state->Render(engine);
    //}
    
    if (!states.empty())
    {
        states.back()->Render(engine);
    }
}

void StateManager::RenderImGui(Engine& engine)
{
    if (!states.empty())
    {
        states.back()->RenderImGui(engine);
    }
}

BaseState* StateManager::GetCurrentState() const
{
    if (!states.empty())
    {
        return states.back().get();
    }
    
    return nullptr;
}