#include "PauseState.h"
#include "../Engine.h"
#include "GamePlayState.h"
#include "MainMenuState.h"
#include <imgui.h>
#include <SDL2/SDL.h>

void PauseState::Enter(Engine& engine)
{
}

void PauseState::Exit(Engine& engine)
{
}

void PauseState::HandleEvents(Engine& engine, SDL_Event& event)
{
    if (event.type == SDL_KEYDOWN)
    {
        if (event.key.keysym.sym == SDLK_ESCAPE)
        {
            engine.RequestPopState();
        }
    }
}

void PauseState::Update(Engine& engine, float dt)
{
}

void PauseState::Render(Engine& engine)
{
    SDL_Renderer* renderer = engine.GetRenderer();
    SDL_SetRenderDrawColor(renderer, overlayColor.r, overlayColor.g, overlayColor.b, overlayColor.a);
    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
    SDL_RenderFillRect(renderer, nullptr);
}

void PauseState::RenderImGui(Engine& engine)
{
    ImGuiIO& io = ImGui::GetIO();
    ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.5f),
                            ImGuiCond_Always, ImVec2(0.5f, 0.5f));
    ImGui::SetNextWindowSize(ImVec2(400, 300), ImGuiCond_FirstUseEver);
    
    ImGui::Begin("Pause Menu", nullptr, ImGuiWindowFlags_NoTitleBar |
                                        ImGuiWindowFlags_NoResize |
                                        ImGuiWindowFlags_NoMove |
                                        ImGuiWindowFlags_NoCollapse);
    ImGui::Dummy(ImVec2(0, 40));
    ImGui::SetCursorPosX((ImGui::GetWindowSize().x - ImGui::CalcTextSize("PAUSED").x) * 0.5f);
    ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.0f, 1.0f), "PAUSED");

    ImGui::Dummy(ImVec2(0.0f, 60.0f));
    
    float buttonWidth = 200.f;
    float windowWidth = ImGui::GetWindowSize().x;

    ImGui::SetCursorPosX((windowWidth - buttonWidth) * 0.5f);
    if (ImGui::Button("Resume Game", ImVec2(buttonWidth, 30)))
    {
        engine.RequestPopState();
    }

    ImGui::SetCursorPosX((windowWidth - buttonWidth) * 0.5f);
    if (ImGui::Button("Map Editor", ImVec2(buttonWidth, 30)))
    {
        SDL_Log("TODO: MapEditorState");
    }

    ImGui::SetCursorPosX((windowWidth - buttonWidth) * 0.5f);
    if (ImGui::Button("Asset Editor", ImVec2(buttonWidth, 30)))
    {
        SDL_Log("TODO: AssetEditorState");
    }

    ImGui::SetCursorPosX((windowWidth - buttonWidth) * 0.5f);
    if (ImGui::Button("Exit to Main Menu", ImVec2(buttonWidth, 30)))
    {
        //engine.RequestStateChange(std::make_unique<MainMenuState>());
        engine.stateManager.PopState(engine); // Drop PauseState
        engine.stateManager.PopState(engine); // Drop GamePlayState
        //engine.stateManager.ChangeState(std::make_unique<MainMenuState>(), engine);
    }

    ImGui::End();
}