#include "TextComponent.h"
#include <yaml-cpp/yaml.h> 
#include <stdexcept>       
#include <utility>         

TextComponent::TextComponent(Object* owner) 
    : Component(owner), text("") {} 

TextComponent::TextComponent(Object* owner, std::string initialText)
    : Component(owner), text(std::move(initialText)) {} 

std::string TextComponent::GetName() const {
    return "TextComponent";
}

void TextComponent::Save(YAML::Emitter& out) const {
    out << YAML::BeginMap;
    out << YAML::Key << "component" << YAML::Value << GetName(); 
    out << YAML::Key << "text" << YAML::Value << text; 
    out << YAML::EndMap;
}


void TextComponent::Load(const YAML::Node& node) {
    if (!node["text"]) {
        text = "";
        RecoverableError("'text' key missing in YAML", Create::Exceptions::MissingField).Handle();
        return;
    }

    try {
        text = node["text"].as<std::string>();
    } catch (const YAML::BadConversion& e) {
        RecoverableError("Invalid 'text' format: " + std::string(e.what()), Create::Exceptions::InvalidFormat).Handle();
        return;
    }
}


const std::string& TextComponent::GetText() const {
    return text;
}

void TextComponent::SetText(const std::string& newText) {
    text = newText;
}

void TextComponent::SetText(std::string&& newText) {
    text = std::move(newText);
}