#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;
}
