context: #region // Copyright (C) 2014, SIL International. All Rights Reserved.
  // --------------------------------------------------------------------------------------------
  // <copyright file="EpubManifest.cs" from='2009' to='2014' company='SIL International'>
  //      Copyright (C) 2014, SIL International. All Rights Reserved.
  //
  //      Distributable under the terms of either the Common Public License or the
  //      GNU Lesser General Public License, as specified in the LICENSING.txt file.
  // </copyright>
  // <author>Greg Trihus</author>
  // <email>greg_trihus@sil.org</email>
  // Last reviewed:
  //
  // <remarks>
  // </remarks>
  // --------------------------------------------------------------------------------------------
  #endregion
  
  #region using
  
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Globalization;
  using System.IO;
  using System.Text;
  using System.Xml;
  using SIL.PublishingSolution;
  using SIL.Tool;
  
  #endregion using
  
  namespace epubConvert
  {
      public class EpubManifest
      {
          #region Setup & Class data
          private readonly Exportepub _parent;
          private readonly EpubFont _epubFont;
  
          public string Title { get; private set; }
          private string Creator { get; set; }
          private string Description { get; set; }
          private string Publisher { get; set; }
          private string Coverage { get; set; }
          private string Rights { get; set; }
          private string Format { get; set; }
          private string Source { get; set; }
  
          public EpubManifest(Exportepub exportepub, EpubFont epubFont)
          {
              _parent = exportepub;
              _epubFont = epubFont;
          }
          #endregion Setup & Class data
  
          #region void CreateOpf(PublicationInformation projInfo, string contentFolder, Guid bookId)
          /// <summary>
          /// Generates the manifest and metadata information file used by the .epub reader
          /// (content.opf). For more information, refer to <see cref="http://www.idpf.org/doc_library/epub/OPF_2.0.1_draft.htm#Section2.0"/>
          /// </summary>
          /// <param name="projInfo">Project information</param>
          /// <param name="contentFolder">Content folder (.../OEBPS)</param>
          /// <param name="bookId">Unique identifier for the book we're generating.</param>
          public void CreateOpf(PublicationInformation projInfo, string contentFolder, Guid bookId)
          {
              XmlWriter opf = XmlWriter.Create(Common.PathCombine(contentFolder, "content.opf"));
              opf.WriteStartDocument();
              // package name
              opf.WriteStartElement("package", "http://www.idpf.org/2007/opf");
  
              opf.WriteAttributeString("version", "2.0");
              opf.WriteAttributeString("unique-identifier", "BookId");
  
              // metadata - items defined by the Dublin Core Metadata Initiative:
              Metadata(projInfo, bookId, opf);
              StartManifest(opf);
              if (_parent.EmbedFonts)
              {
                  ManifestFontEmbed(opf);
              }
              string[] files = Directory.GetFiles(contentFolder);
              ManifestContent(opf, files, "epub2");
  			opf.WriteEndElement(); // manifest
  			Spine(opf, files);
  			Guide(projInfo, opf, files, "epub2");
              opf.WriteEndElement(); // package
              opf.WriteEndDocument();
              opf.Close();
          }
  
          /// <summary>
          /// Generates the manifest and metadata information file used by the .epub reader
          /// (content.opf). For more information, refer to <see cref="http://www.idpf.org/doc_library/epub/OPF_2.0.1_draft.htm#Section2.0"/>
          /// </summary>
          /// <param name="projInfo">Project information</param>
          /// <param name="contentFolder">Content folder (.../OEBPS)</param>
          /// <param name="bookId">Unique identifier for the book we're generating.</param>
          public void CreateOpfV3(PublicationInformation projInfo, string contentFolder, Guid bookId)
          {
              LoadPropertiesFromSettings();
              XmlWriter opf = XmlWriter.Create(Common.PathCombine(contentFolder, "content.opf"));
              opf.WriteStartDocument();
              // package name
              opf.WriteStartElement("package", "http://www.idpf.org/2007/opf");
              opf.WriteAttributeString("version", "3.0");
              opf.WriteAttributeString("xml", "lang", null, "en");
              opf.WriteAttributeString("unique-identifier", "pub-id");
              opf.WriteAttributeString("prefix", "rendition: http://www.idpf.org/vocab/rendition/#");
  
              // metadata - items defined by the Dublin Core Metadata Initiative:
              MetadataV3(projInfo, bookId, opf);
              StartManifestV3(opf);
              if (_parent.EmbedFonts)
              {
                  ManifestFontEmbed(opf);
              }
              string[] files = Directory.GetFiles(contentFolder);
              ManifestContent(opf, files, "epub3");
  	        var avFolder = Path.Combine(contentFolder, "AudioVisual");
  	        if (Directory.Exists(avFolder))
  	        {
  		        var avFiles = Directory.GetFiles(avFolder);
  				ManifestAvContent(opf, avFiles);
  			}
  			opf.WriteEndElement(); // manifest
  			SpineV3(opf, files);
              Guide(projInfo, opf, files, "epub3");
              opf.WriteEndElement(); // package
              opf.WriteEndDocument();
              opf.Close();
          }
  
          private void Metadata(PublicationInformation projInfo, Guid bookId, XmlWriter opf)
          {
              // (http://dublincore.org/documents/2004/12/20/dces/)
              opf.WriteStartElement("metadata");
              opf.WriteAttributeString("xmlns", "dc", null, "http://purl.org/dc/elements/1.1/");
  
              opf.WriteAttributeString("xmlns", "opf", null, "http://www.idpf.org/2007/opf");
  
              opf.WriteElementString("dc", "title", null,
                                     (Title == "") ? (Common.databaseName + " " + projInfo.ProjectName) : Title);
  
  
              opf.WriteStartElement("dc", "creator", null); //<dc:creator opf:role="aut">[author]</dc:creator>
  
              opf.WriteAttributeString("opf", "role", null, "aut");
  
              opf.WriteValue((Creator == "") ? Environment.UserName : Creator);
              opf.WriteEndElement();
  
              opf.WriteElementString("dc", "subject", null, _parent.InputType.ToLower() == "dictionary" ? "Reference" : "Religion & Spirituality");
  
              if (Description.Length > 0)
                  opf.WriteElementString("dc", "description", null, Description);
  
              if (Publisher.Length > 0)
                  opf.WriteElementString("dc", "publisher", null, Publisher);
  
              opf.WriteStartElement("dc", "contributor", null); // authoring program as a "contributor", e.g.:
              opf.WriteAttributeString("opf", "role", null, "bkp");
  
              opf.WriteValue(Common.GetProductName());
              opf.WriteEndElement();
              opf.WriteElementString("dc", "date", null, DateTime.Today.ToString("yyyy-MM-dd"));
              // .epub standard date format (http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html#Section2.2.7)
              opf.WriteElementString("dc", "type", null, "Text"); //
              if (Format.Length > 0)
                  opf.WriteElementString("dc", "format", null, Format);
              if (Source.Length > 0)
                  opf.WriteElementString("dc", "source", null, Source);
  
              if (_epubFont.LanguageCount == 0)
              {
                  opf.WriteElementString("dc", "language", null, "en");
              }
  
              foreach (var lang in _epubFont.LanguageCodes())
              {
                  opf.WriteElementString("dc", "language", null, lang);
              }
  
  
              if (Coverage.Length > 0)
                  opf.WriteElementString("dc", "coverage", null, Coverage);
              if (Rights.Length > 0)
                  opf.WriteElementString("dc", "rights", null, Rights);
              opf.WriteStartElement("dc", "identifier", null); // <dc:identifier id="BookId">[guid]</dc:identifier>
              opf.WriteAttributeString("id", "BookId");
              opf.WriteValue("http://software.sil.org/pathway/"); // bookId.ToString()
              opf.WriteEndElement();
              // cover image (optional)
              if (Param.GetMetadataValue(Param.CoverPage).ToLower().Equals("true"))
              {
                  opf.WriteStartElement("meta");
                  opf.WriteAttributeString("name", "cover");
                  opf.WriteAttributeString("content", "cover-image");
                  opf.WriteEndElement(); // meta
              }
              opf.WriteEndElement(); // metadata
          }
  
          private void MetadataV3(PublicationInformation projInfo, Guid bookId, XmlWriter opf)
          {
              // (http://dublincore.org/documents/2004/12/20/dces/)
              opf.WriteStartElement("metadata");
              opf.WriteAttributeString("xmlns", "dc", null, "http://purl.org/dc/elements/1.1/");
              opf.WriteStartElement("dc", "title", null);
              opf.WriteAttributeString("id", "title");
  
              if (!string.IsNullOrEmpty(Title))
              {
                  opf.WriteValue(Title);
              }
              else
              {
                  opf.WriteValue(Common.databaseName + " " + projInfo.ProjectName);
              }
              opf.WriteEndElement();
  
              opf.WriteStartElement("dc", "creator", null); //<dc:creator opf:role="aut">[author]</dc:creator>
              opf.WriteAttributeString("id", "creator");
              if (!string.IsNullOrEmpty(Creator.Trim()))
              {
                  opf.WriteValue(Creator);
              }
              else
              {
                  opf.WriteValue(Environment.UserName);
              }
  
              opf.WriteEndElement();
  
              if (_epubFont != null && _epubFont.LanguageCount == 0)
              {
                  opf.WriteElementString("dc", "language", null, "en");
              }
  
              if (_epubFont != null)
              {
                  foreach (var lang in _epubFont.LanguageCodes())
                  {
                      opf.WriteElementString("dc", "language", null, lang);
                  }
              }
  
              opf.WriteStartElement("dc", "identifier", null);
              opf.WriteAttributeString("id", "pub-id");
              opf.WriteValue("http://software.sil.org/pathway/");
              //opf.WriteValue(bookId.ToString());
              opf.WriteEndElement();
  
              if (!string.IsNullOrEmpty(Source.Trim()))
                  opf.WriteElementString("dc", "source", null, Source);
              else
                  opf.WriteElementString("dc", "source", null, "Epub3 Source");
  
              opf.WriteStartElement("meta");
              opf.WriteAttributeString("property", "dcterms:modified");
              opf.WriteValue(DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"));
              opf.WriteEndElement();
  
              opf.WriteElementString("dc", "date", null, DateTime.Now.Year.ToString(CultureInfo.InvariantCulture));
  
              if (!string.IsNullOrEmpty(Publisher.Trim()))
  
gt:                 opf.WriteElementString("dc", "publisher", null, Publisher);