#include "XmlElement.h"


XmlElement::XmlElement(const std::string& tagName)
    : mTagName(tagName),
      mChildren()
{

}

XmlElementUPtr XmlElement::Create(const std::string& tagName)
{
    return std::make_unique<XmlElement>(tagName);
}

void XmlElement::SetTagName(const std::string& tagName)
{
    mTagName = tagName;
}

void XmlElement::AddChild(XmlElementUPtr child)
{
    mChildren.push_back(std::move(child));
}

void XmlElement::AddAttribute(XmlAttributeUPtr attribute)
{
    mAttributes.push_back(std::move(attribute));
}

std::string XmlElement::GetTagName() const
{
    return mTagName;
}

std::string XmlElement::GetText() const
{
    return mText;
}

void XmlElement::SetText(const std::string& text)
{
    mText = text;
}

XmlAttribute* XmlElement::GetAttribute(const std::string& attributeName) const
{
    for(const auto& attribute : mAttributes)
    {
        if(attribute->GetName() == attributeName)
        {
            return attribute.get();
        }
    }
    return nullptr;
}

XmlAttribute* XmlElement::GetAttribute(std::size_t index) const
{
    if(index < mAttributes.size())
    {
        return mAttributes[index].get();
    }
    return nullptr;
}

std::size_t XmlElement::GetNumAttributes() const
{
    return mAttributes.size();
}

std::size_t XmlElement::GetNumChildren() const
{
    return mChildren.size();
}

XmlElement* XmlElement::GetChild(std::size_t index) const
{
    return mChildren[index].get();
}
