#include "Renderer.hpp"

namespace GModTeach {
namespace UI {

void Renderer::DrawText(
    SDL_Renderer* renderer,
    TTF_Font* font,
    const std::string& text,
    int x,
    int y,
    SDL_Color color
) {
    if (!renderer || !font || text.empty()) {
        return;
    }

    SDL_Surface* surface = TTF_RenderUTF8_Blended(font, text.c_str(), color);
    if (!surface) {
        return;
    }

    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
    if (texture) {
        SDL_Rect dst = { x, y, surface->w, surface->h };
        SDL_RenderCopy(renderer, texture, nullptr, &dst);
        SDL_DestroyTexture(texture);
    }

    SDL_FreeSurface(surface);
}

void Renderer::DrawCenteredText(
    SDL_Renderer* renderer,
    TTF_Font* font,
    const std::string& text,
    const SDL_Rect& bounds,
    SDL_Color color
) {
    if (!renderer || !font || text.empty()) {
        return;
    }

    int textW = 0, textH = 0;
    TTF_SizeUTF8(font, text.c_str(), &textW, &textH);

    const int x = bounds.x + (bounds.w - textW) / 2;
    const int y = bounds.y + (bounds.h - textH) / 2;

    DrawText(renderer, font, text, x, y, color);
}

void Renderer::GetTextSize(
    TTF_Font* font,
    const std::string& text,
    int& outWidth,
    int& outHeight
) {
    if (!font || text.empty()) {
        outWidth = 0;
        outHeight = 0;
        return;
    }
    TTF_SizeUTF8(font, text.c_str(), &outWidth, &outHeight);
}

} 
} 
