#include "HtmlParagraphElement.h"

std::string HtmlParagraphElement::toString(unsigned depth, bool) const
{
    const auto prefix = std::string(2*depth, ' ');

    auto content = prefix + "<" + getTagName();
    for (const auto& [key, attribute] : getAttributes())
    {
        content += " " + attribute->getName() + "=\"" + attribute->getValue() + "\"";
    }

    const auto num_children = mChildren.size();
    if (num_children == 0 && getText().empty())
    {
        content += "/>\n";
        return content;
    }
    else
    {
        content += ">";
    }

    if (!getText().empty())
    {
        content += getText();
    }

    if (num_children>0)
    {
        content += "\n";
    }

    for(const auto& child : mChildren)
    {
        content += child->toString(depth+1, true);
    }
    if (num_children>0)
    {
        content += prefix;
    }

    content += "</" + getTagName() + ">\n";
    return content;
}
