#include "PdfDictionary.h"

std::string PdfDictionary::toString() const
{
    const auto keys = getStringKeys();
    if (keys.empty())
    {
        return "";
    }

    std::string content = "  <<";
    bool first = true;
    auto ending = keys.size() == 1 ? "" : "\n";
    for (const auto& key : keys)
    {
        if (first)
        {
            content += " /" + key + " " + getItem(key) + ending;
            first = false;
        }
        else
        {
            content += "     /" + key + " " + getItem(key) + ending;
        }
    }

    const auto dictKeys = getDictKeys();
    ending = dictKeys.size() == 1 ? "" : "\n";
    for (const auto& key : getDictKeys())
    {
        auto pdfDict = dynamic_cast<PdfDictionary*>(getDict(key));
        if (first)
        {
            content += " /" + key + " " + pdfDict->toString() + ending;
            first = false;
        }
        else
        {
            content += "     /" + key + " " + pdfDict->toString() + ending;
        }
    }

    content += "  >>\n";
    return content;
}
