// ************************************************************************************************************
// Author: Marcin Załęczny
// Release Date: 2010-11-08
// Licence: The GNU Lesser General Public License, version 3.0 (LGPLv3)
// ************************************************************************************************************
#include "mzXpsFixedDocument.h"

VOID CXpsFixedDocument::_commonInit(CString documentRootDir) {
    this->setRealClassName(L"CXpsFixedDocument");
    this->_pagenums = new CArrayList(20);
    this->_documentRootDir = documentRootDir;
    this->addXmlns(L"http://schemas.microsoft.com/xps/2005/06");
}

CXpsFixedDocument::CXpsFixedDocument(CString documentRootDir)
    : CXmlDocument(L"FixedDocument")
{
    this->_commonInit(documentRootDir);
}

CXpsFixedDocument::CXpsFixedDocument(CString documentRootDir, CString xmlVersion, CString xmlEncoding)
    : CXmlDocument(L"FixedDocument", xmlVersion, xmlEncoding)
{
    this->_commonInit(documentRootDir);
}

CXpsFixedDocument::~CXpsFixedDocument() {
    if (this->_pagenums) {
        delete this->_pagenums;
        this->_pagenums = NULL;
    }
}

BOOL CXpsFixedDocument::hasPage(LONG pagenum) {
    return this->_pagenums->contains(pagenum);
}

BOOL CXpsFixedDocument::addPageContent(INT pagenum) {
    CXmlNode* item = new CXmlNode(L"PageContent");
    
    if (item && !this->hasPage(pagenum)) {
        if (pagenum < 1) {
            pagenum = this->_pagenums->getFirstAvailableValue();
        }
        item->addAttribute(L"Source", this->_documentRootDir + L"/Pages/" + pagenum + L".fpage");
        this->_items->append((LONG)item);
        this->_pagenums->append(pagenum);
        return TRUE;
    }
    
    return FALSE;
}
