#include "Button.hpp"
#include "Renderer.hpp"

namespace GModTeach {
namespace UI {

Button::Button(SDL_Rect bounds, std::string label)
    : m_bounds(bounds), m_label(std::move(label)) {}

bool Button::Contains(int mouseX, int mouseY) const {
    return mouseX >= m_bounds.x && mouseX <= (m_bounds.x + m_bounds.w) &&
           mouseY >= m_bounds.y && mouseY <= (m_bounds.y + m_bounds.h);
}

void Button::UpdateHover(int mouseX, int mouseY) {
    if (!m_enabled) {
        m_hovered = false;
        return;
    }
    m_hovered = Contains(mouseX, mouseY);
}

void Button::Draw(SDL_Renderer* renderer, TTF_Font* font) const {
    if (!renderer || !font) return;

    if (!m_enabled) {
        // null state
        SDL_SetRenderDrawColor(renderer, CLR_DISABLED.r, CLR_DISABLED.g, CLR_DISABLED.b, CLR_DISABLED.a);
        SDL_RenderDrawRect(renderer, &m_bounds);
        Renderer::DrawCenteredText(renderer, font, m_label, m_bounds, CLR_DISABLED);
        return;
    }

    if (m_hovered) {
        // oover white with blue text
        SDL_SetRenderDrawColor(renderer, CLR_WHITE.r, CLR_WHITE.g, CLR_WHITE.b, CLR_WHITE.a);
        SDL_RenderFillRect(renderer, &m_bounds);
        Renderer::DrawCenteredText(renderer, font, m_label, m_bounds, CLR_GMOD_BLUE);
    } else {
        // blue with white outline and text
        SDL_SetRenderDrawColor(renderer, CLR_WHITE.r, CLR_WHITE.g, CLR_WHITE.b, CLR_WHITE.a);
        SDL_RenderDrawRect(renderer, &m_bounds);
        Renderer::DrawCenteredText(renderer, font, m_label, m_bounds, CLR_WHITE);
    }
}

} 
} 
