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


CXpsPackage::CXpsPackage() {   
    this->setRealClassName(L"CXpsPackage");
    
    this->_documents = new CArrayList(10);
    
    this->_rels = new CXpsRelationships();
    this->_rels->addXmlns(L"http://schemas.openxmlformats.org/package/2006/relationships");
    this->_rels->addRelationship(L"/FixedDocumentSequence.fdseq", L"R0", L"http://schemas.microsoft.com/xps/2005/06/fixedrepresentation");

    this->_fixedDocumentSequenceFdseqRel = new CXpsRelationships();
    this->_fixedDocumentSequenceFdseqRel->addXmlns(L"http://schemas.openxmlformats.org/package/2006/relationships");
    this->_fixedDocumentSequenceFdseqRel->addRelationship(L"/Metadata/Job_PT.xml", L"R0", "http://schemas.microsoft.com/xps/2005/06/printticket");
    
    this->_contentTypes = new CXpsContentTypes();
    this->_contentTypes->addXmlns(L"http://schemas.openxmlformats.org/package/2006/content-types");
    this->_contentTypes->addContentType(L"rels", L"application/vnd.openxmlformats-package.relationships+xml");
    this->_contentTypes->addContentType(L"fdseq", L"application/vnd.ms-package.xps-fixeddocumentsequence+xml");
    this->_contentTypes->addContentType(L"xml", L"application/vnd.ms-printing.printticket+xml");
    
    this->_contentTypes->addContentType(L"fdoc", L"application/vnd.ms-package.xps-fixeddocument+xml");
    this->_contentTypes->addContentType(L"fpage", L"application/vnd.ms-package.xps-fixedpage+xml");
    this->_contentTypes->addContentType(L"odttf", L"application/vnd.ms-package.obfuscated-opentype");
    this->_contentTypes->addContentType(L"ttf", L"application/vnd.ms-opentype");
    this->_contentTypes->addContentType(L"JPG", L"image/jpeg");

    
    this->_fixedDocumentSequenceFdseq = new CXpsFixedDocumentSequence(this->_documents);
    this->_fixedDocumentSequenceFdseq->addXmlns(L"http://schemas.microsoft.com/xps/2005/06");
    
    this->_metadata = new CXpsMetadata();
    this->_metadata->addJobPT();
    this->_metadata->addMxdcEmptyPT();
}

CXpsPackage::~CXpsPackage() {   
    if (this->_rels) {
        delete this->_rels;
        this->_rels = NULL;
    }
    
    if (this->_fixedDocumentSequenceFdseqRel) {
        delete this->_fixedDocumentSequenceFdseqRel;
        this->_fixedDocumentSequenceFdseqRel = NULL;
    }
    
    if (this->_fixedDocumentSequenceFdseq) {
        delete this->_fixedDocumentSequenceFdseq;
        this->_fixedDocumentSequenceFdseq = NULL;
    }
    
    if (this->_contentTypes) {
        delete this->_contentTypes;
        this->_contentTypes = NULL;
    }
    
    if (this->_metadata) {
        delete this->_metadata;
        this->_metadata = NULL;
    }
    
    if (this->_documents) {
        this->removeAllDocuments();
    }
}

VOID CXpsPackage::removeAllDocuments() {
    if (this->_documents) {
        CXpsDocument* doc;
        LONG* items = this->_documents->getItemsBuf();
        for (UINT i = 0; i < this->_documents->size(); i++) {
            doc = (CXpsDocument*)items[i];
            delete doc;
        }
        this->_documents->clear();
        this->_documents = NULL;
    }
}

BOOL CXpsPackage::saveToFile(CString fname, BOOL deleteTempXPSDirectoryStructure) {
    CString realPath = RealPath(fname);
    CString rootDir;
    CXpsDocument* doc;
    CString curDate = CDateTime::date(L"YmdHis");
    CString curDateHuman = CDateTime::date(L"Y-m-d H:i:s");
    CString tmpFolder = CSystemShell::GetTempFolder() + L"\\progmar\\" + curDate + L"_" + GetPathnameNoExt(realPath);
    CreateDirectory(tmpFolder, TRUE);

    rootDir = tmpFolder;
    CreateDirectory(rootDir + L"\\_rels", TRUE);
    CreateDirectory(rootDir + L"\\Documents");
    CreateDirectory(rootDir + L"\\Metadata");
    
    this->_rels->saveXmlFile(rootDir + L"\\_rels\\.rels");
    this->_fixedDocumentSequenceFdseqRel->saveXmlFile(rootDir + L"\\_rels\\FixedDocumentSequence.fdseq.rels");
    
    this->_contentTypes->saveXmlFile(rootDir + L"\\[Content_Types].xml");
    this->_fixedDocumentSequenceFdseq->saveXmlFile(rootDir + L"\\FixedDocumentSequence.fdseq");
    
    this->_metadata->saveToFiles(rootDir + L"\\Metadata");
    
    LONG* items = this->_documents->getItemsBuf();
    INT size = this->_documents->size();
    for (UINT i = 0; i < size; i++) {
        doc = (CXpsDocument*)items[i];
        doc->saveToFiles(rootDir + L"\\Documents");
    }

    ZipFolder(fname.getBuffer(), tmpFolder.getBuffer(), FALSE);
    if (deleteTempXPSDirectoryStructure) {
        DeleteDirectory(tmpFolder);
    }
    
    return TRUE;
}

CXpsDocument* CXpsPackage::addDocument() {
    INT size = this->_documents->size();
    CXpsDocument* xpsDoc = new CXpsDocument(size + 1);
    this->_documents->append((LONG)xpsDoc);
    return xpsDoc;
}

CXpsDocument* CXpsPackage::getDocument(UINT i) {
    return NULL;
}

