using AIStudio.Tools.OpenXml;
namespace AIStudio.Tools.PowerPoint;
///
/// Creates a presentation file from a template and a slide deck.
///
///
/// The presentation comes about in a TemplateCopy, and only the complete presentation is streamed
/// into the file the user chose; TemplateCopy says why.
///
internal static class PresentationFile
{
///
/// The folder below the temporary folder of the system, where the presentations come about.
///
private const string TEMPORARY_DIRECTORY = "mindwork-ai-studio-presentations";
///
/// Writes the slide deck into a copy of the template, and the result into the target.
///
///
/// The template is read once more rather than reused: it may have changed since the slides were
/// planned, and the writer has to see the layouts of the very file it edits.
///
/// The path of the template, a .pptx or a .potx.
/// The path of the file the user chose. It may exist; the save dialog asked about overwriting it.
/// The slide deck, as checked against the template when it was planned.
/// What the deck was planned on, as PresentationTemplate.DescribeBasisOf told it then.
/// What the user chose for the presentation as a whole.
/// Where the pictures the deck refers to come from.
/// Where to report a temporary file which cannot be removed.
/// The cancellation token.
/// Whether the presentation was written, and which pictures it lacks.
public static async Task CreateAsync(string templatePath, string targetPath, SlideDeckSpec deck, IReadOnlyDictionary basis, PresentationWriteOptions options, IPictureSource pictures, ILogger logger, CancellationToken token)
{
using var copy = await TemplateCopy.CreateAsync(templatePath, TEMPORARY_DIRECTORY, ".pptx", logger, token);
PresentationWriteResult written;
await using (var presentationFile = copy.OpenForEditing())
{
var currentTemplate = PresentationTemplate.Read(presentationFile);
presentationFile.Position = 0;
if (!SlideDeckSpecValidator.StillFits(deck, currentTemplate, basis))
return PresentationFileResult.TEMPLATE_CHANGED;
written = await PresentationWriter.WriteAsync(presentationFile, currentTemplate, deck, options, pictures, token);
}
await copy.WriteIntoAsync(targetPath, token);
return new PresentationFileResult(true, written.SkippedPictures);
}
}