#include "MarkdownDocument.h"

#include "MarkdownElement.h"
#include "MarkdownComponents.h"

MarkdownDocument::~MarkdownDocument()
{

}

void MarkdownDocument::addElement(std::unique_ptr<MarkdownElement> element)
{
    mElements.push_back(std::move(element));
}

std::size_t MarkdownDocument::getNumElements() const
{
    return mElements.size();
}

MarkdownElement* MarkdownDocument::getElement(std::size_t idx) const
{
    return mElements[idx].get();
}

std::vector<MarkdownLink*> MarkdownDocument::getAllLinks() const
{
    std::vector<MarkdownLink*> links;
    for(auto& element : mElements)
    {
        if (element->getType() == MarkdownElement::Type::PARAGRAPH)
        {
            auto para_links = dynamic_cast<MarkdownElementWithChildren*>(element.get())->getAllLinks();
            links.insert(links.end(), para_links.begin(), para_links.end());
        }
        else if (element->getType() == MarkdownElement::Type::BULLET_LIST)
        {
            auto bullet_list = dynamic_cast<MarkdownBulletList*>(element.get());
            for(unsigned idx=0; idx<bullet_list->getNumChildren(); idx++)
            {
                auto para_links = bullet_list->getChild(idx)->getAllLinks();
                links.insert(links.end(), para_links.begin(), para_links.end());
            }
        }
    }
    return links;
}

std::size_t MarkdownDocument::getNumMultilineContexts() const
{
    return mCustomMultilineContexts.size();
}

std::size_t MarkdownDocument::getNumInlineContexts() const
{
    return mCustomInlineContexts.size();
}

MarkdownCustomMultilineElementContext* MarkdownDocument::getMultilineContext(std::size_t idx) const
{
    return mCustomMultilineContexts[idx].get();
}

MarkdownCustomInlineElementContext* MarkdownDocument::getInlineContext(std::size_t idx) const
{
    return mCustomInlineContexts[idx].get();
}

void MarkdownDocument::registerCustomInlineElement(std::unique_ptr<MarkdownCustomInlineElementContext> context)
{
    mCustomInlineContexts.push_back(std::move(context));
}

void MarkdownDocument::registerCustomMultilineElement(std::unique_ptr<MarkdownCustomMultilineElementContext> context)
{
    mCustomMultilineContexts.push_back(std::move(context));
}
