#include "SvgTextElement.h"

#include "XmlAttribute.h"

#include <sstream>

SvgTextElement::SvgTextElement()
	:SvgElement("text")
{
	auto baseline = std::make_unique<XmlAttribute>("dominant-baseline");
	baseline->setValue("middle");
	addAttribute(std::move(baseline));

	auto anchor = std::make_unique<XmlAttribute>("text-anchor");
	anchor->setValue("middle");
	addAttribute(std::move(anchor));
}

void SvgTextElement::setFillOpacity(float opacity)
{
	auto attr = std::make_unique<XmlAttribute>("fill-opacity");
	attr->setValue(std::to_string(opacity));
	addAttribute(std::move(attr));
}

void SvgTextElement::setLocation(const Point& loc)
{
	auto x = std::make_unique<XmlAttribute>("x");
	auto y = std::make_unique<XmlAttribute>("y");

	x->setValue(std::to_string(loc.getX()));
	y->setValue(std::to_string(loc.getY()));

	addAttribute(std::move(x));
	addAttribute(std::move(y));
}

void SvgTextElement::setContent(const std::string& content)
{
	setText(content);
}

void SvgTextElement::setFill(const Color& fill)
{
	auto attr = std::make_unique<XmlAttribute>("fill");

	std::stringstream sstr;
	sstr << "rgb(" << fill.toString() << ")";
	attr->setValue(sstr.str());

	addAttribute(std::move(attr));
}

void SvgTextElement::setFontFamily(const std::string& family)
{
	auto attr = std::make_unique<XmlAttribute>("font-family");
	attr->setValue(family);
	addAttribute(std::move(attr));
}

void SvgTextElement::setFontSize(float size)
{
	auto attr = std::make_unique<XmlAttribute>("font-size");
	attr->setValue(std::to_string(size));
	addAttribute(std::move(attr));
}