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