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

CXpsFixedDocumentSequence::CXpsFixedDocumentSequence(CArrayList* documents)
    : _documents(documents), CXmlDocument(L"FixedDocumentSequence")
{
    this->setRealClassName(L"CXpsFixedDocumentSequence");
}

CXpsFixedDocumentSequence::CXpsFixedDocumentSequence(CArrayList* documents, CString xmlVersion, CString xmlEncoding)
    : _documents(documents), CXmlDocument(L"FixedDocumentSequence", xmlVersion, xmlEncoding)
{
    this->setRealClassName(L"CXpsFixedDocumentSequence");
}

CXpsFixedDocumentSequence::~CXpsFixedDocumentSequence() {
}

BOOL CXpsFixedDocumentSequence::addDocumentReference(INT docNumber, CString fixedDocName) {
    CXmlNode* item = new CXmlNode(L"DocumentReference");
    if (item) {
        item->addAttribute(L"Source", CString(L"/Documents/") + docNumber + L"/" + fixedDocName);
        this->_items->append((LONG)item);
        return TRUE;
    }
    
    return FALSE;
}

CString CXpsFixedDocumentSequence::toXmlString() {
    CString retval;
    
    if (this->_documents) {
        INT size = this->_documents->size();
        LONG* items = this->_documents->getItemsBuf();
        CXmlNode* node;
        CXpsDocument* xpsDoc;
        for (INT i = 0; i < size; i++) {
            xpsDoc = (CXpsDocument*)items[i];
            node = new CXmlNode(L"DocumentReference");
            node->addAttribute(L"Source", xpsDoc->getDocumentInternalDirectory() + L"/FixedDocument.fdoc");
            this->addItem(node);
        }
        
        retval = CXmlDocument::toXmlString();
        
        this->removeAllItems();
    } else {
        retval = CXmlDocument::toXmlString();
    }
    
    return retval;
}
