using InABox.Core;
using PDFtoImage;
using SkiaSharp;

namespace InABox.Avalonia.Platform.Desktop;

public class Desktop_PdfRenderer : IPdfRenderer
{
    public byte[]? RenderPdf(byte[]? pdf, int page, int dpi)
    {
        if (pdf?.Any() != true)
            return null;
        
        var result = Conversion.ToImage(pdf, page, options: new RenderOptions(Dpi: dpi));
        using var ms = new MemoryStream();
        result.Encode(ms, SKEncodedImageFormat.Jpeg, 65);
        return ms.ToArray();
    }

    public Task<byte[]?> RenderPdfAsync(byte[]? pdf, int page, int dpi)
        => Task.Run(() => RenderPdf(pdf, page, dpi));

    public Logger? Logger { get; set; }
}