#include <object/ui/uitext.hpp>

namespace Game::Object {
    UIText::UIText(const std::string& name, std::shared_ptr<Renderer::Font> font, const Transform& transform, float x, float y)
        : Entity(name, font, transform), mX(x), mY(y) { }

    void UIText::start() {
        // Center the text on the requested position
        mTransform.x = mX - mTex->getWidth() * mTransform.adjustedScaleX() / 2.f;
        mTransform.y = mY - mTex->getHeight() * mTransform.adjustedScaleY() / 2.f;
    }

    void UIText::update(float deltaTime) {} /* {
        if (!mIsActive) return;

        if (Input::isMouseButtonJustPressed(SDL_BUTTON_LEFT) && mClickFunction) {
            float mouseX = Input::getMouseX();
            float mouseY = Input::getMouseY();
            float textLeft = mTransform.x;
            float textRight = mTransform.x + mTex->getWidth() * mTransform.adjustedScaleX();
            float textTop = mTransform.y;
            float textBottom = mTransform.y + mTex->getHeight() * mTransform.adjustedScaleY();

            if (mouseX >= textLeft && mouseX <= textRight && mouseY >= textTop && mouseY <= textBottom) {
                using ClickFnType = void(*)();
                ClickFnType clickFn = reinterpret_cast<ClickFnType>(mClickFunction);
                clickFn();
            }
        }
    }*/

    void UIText::setText(const std::string& text) {
        mText = text;
        std::dynamic_pointer_cast<Renderer::Font>(mTex)->build({255, 255, 255, 255}, text);
    }

    std::string UIText::getText() const {
        return mText;
    }
}