#include "HtmlDocument.h"

#include "HtmlHeadElement.h"
#include "HtmlBodyElement.h"

#include <iostream>

HtmlDocument::HtmlDocument()
    : XmlDocument()
{
    auto root = XmlElement::Create("html");

    setRoot(std::move(root));

    auto header = std::make_unique<HtmlHeadElement>();
    getRoot()->addChild(std::move(header));

    auto body = std::make_unique<HtmlBodyElement>();
    getRoot()->addChild(std::move(body));
}

std::unique_ptr<HtmlDocument> HtmlDocument::Create()
{
    return std::make_unique<HtmlDocument>();
}

void HtmlDocument::addElementToBody(std::unique_ptr<HtmlElement> element)
{
    if (auto body_element = getRoot()->getFirstChildWithTagName("body"))
    {
        body_element->addChild(std::move(element));
    }
}

HtmlBodyElement* HtmlDocument::getBodyElement() const
{
    return dynamic_cast<HtmlBodyElement*>(getRoot()->getFirstChildWithTagName("body"));
}
