using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using A = DocumentFormat.OpenXml.Drawing;

namespace Service;

public class SlideData
{
    public string Title { get; set; } = "";
    public string Content { get; set; }="";
}

public class PowerPointGenerator
{
    public static void CreatePpt(string filePath, List<SlideData> slides)
    {
        if (File.Exists(filePath)) File.Delete(filePath);

        using (PresentationDocument presentationDoc =
            PresentationDocument.Create(filePath, PresentationDocumentType.Presentation))
        {
            // Partie principale de la présentation
            PresentationPart presentationPart = presentationDoc.AddPresentationPart();
            presentationPart.Presentation = new Presentation();

            // SlideMaster de base
            SlideMasterPart slideMasterPart = presentationPart.AddNewPart<SlideMasterPart>("rId1");
            slideMasterPart.SlideMaster = new SlideMaster(new CommonSlideData(new ShapeTree()));
            SlideLayoutPart layoutPart = slideMasterPart.AddNewPart<SlideLayoutPart>("rId2");
            layoutPart.SlideLayout = new SlideLayout(new CommonSlideData(new ShapeTree()));

            presentationPart.Presentation.SlideMasterIdList = new SlideMasterIdList(
                new SlideMasterId() { Id = 1U, RelationshipId = "rId1" });

            // Liste des slides
            SlideIdList slideIdList = new SlideIdList();
            presentationPart.Presentation.SlideIdList = slideIdList;

            uint slideId = 256;

            foreach (var slideData in slides)
            {
                SlidePart slidePart = presentationPart.AddNewPart<SlidePart>();
                slidePart.Slide = new Slide(new CommonSlideData(new ShapeTree()));

                var shapeTree = slidePart.Slide.CommonSlideData.ShapeTree;

                // Titre
                var titleShape = CreateTextShape(1U, "Title", slideData.Title, 200, 100);
                shapeTree.Append(titleShape);

                // Contenu
                var contentShape = CreateTextShape(2U, "Content", slideData.Content, 200, 200);
                shapeTree.Append(contentShape);

                SlideId newSlideId = new SlideId()
                {
                    Id = slideId++,
                    RelationshipId = presentationPart.GetIdOfPart(slidePart)
                };
                slideIdList.Append(newSlideId);
            }

            presentationPart.Presentation.Save();
        }
    }

    private static Shape CreateTextShape(uint id, string name, string text, int x, int y)
    {
        return new Shape(
            new NonVisualShapeProperties(
                new NonVisualDrawingProperties() { Id = id, Name = name },
                new NonVisualShapeDrawingProperties(),
                new ApplicationNonVisualDrawingProperties()
            ),
            new ShapeProperties(
                new A.Transform2D(
                    new A.Offset() { X = x * 9525, Y = y * 9525 }, // position en EMUs
                    new A.Extents() { Cx = 6000000, Cy = 2000000 } // taille approximative
                )
            ),
            new TextBody(
                new A.BodyProperties(),
                new A.ListStyle(),
                new A.Paragraph(new A.Run(new A.Text(text)))
            )
        );
    }
}




