#include "PdfPageTree.h"

#include "PdfXRefTable.h"
#include "PdfPage.h"

PdfPageTree::PdfPageTree()
{
    mRootPage = std::make_unique<PdfPage>(this);
}

unsigned PdfPageTree::indexObjects(unsigned count)
{
    auto newCount = count + 1;
    mObjectNumber = newCount;
    newCount = mRootPage->indexObjects(newCount);
    return newCount;
}

std::string PdfPageTree::toString(PdfXRefTable* xRefTable)
{
    updateDictionary();
    std::string content = getStringPrefix();
    content += mDictionary.toString();
    content += getStringSuffix();
    xRefTable->addRecord(content.size(), mGenerationNumber, mIsFree);

    content += mRootPage->toString(xRefTable);
    return content;
}

void PdfPageTree::updateDictionary()
{
    mDictionary.addStringItem("Type", "/Pages");
    std::string kids = "[" + mRootPage->getRefString() + "]";
    mDictionary.addStringItem("Kids", kids);
    mDictionary.addStringItem("Count", "1");
}
