using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;

namespace InABox.Avalonia
{
    public static class MobileDocumentExtensions
    {
        public static void ConvertToPDF(this MobileDocument document)
        {
            using (var img = new MemoryStream(document.Data))
            {
                var image = new PdfBitmap(img);

                var pdfDoc = new PdfDocument();

                var section = pdfDoc.Sections.Add();
                section.PageSettings.Margins.All = 0;
                section.PageSettings.Width = image.Width;
                section.PageSettings.Height = image.Height;

                var page = section.Pages.Add();
                page.Graphics.DrawImage(image, 0, 0, page.Size.Width, page.Size.Height);

                using (var ms = new MemoryStream())
                {
                    pdfDoc.Save(ms);
                    document.Data = ms.GetBuffer();
                    document.FileName = Path.ChangeExtension(document.FileName, "pdf");                
                }
            }

        }
        
        public static bool IsPDF(this MobileDocument document) => document.FileName.ToUpper().EndsWith(".PDF"); 
    }
}