using FellowOakDicom.Imaging;
using SixLabors.ImageSharp.Formats.Jpeg;

namespace IRaCIS.Core.Application.Dicom
{
    public static class DicomRenderingHelper
    {
        public static Stream RenderPreviewJpeg(string filePath)
        {
            string jpegPath = filePath + ".preview.jpg";

            if (!File.Exists(jpegPath))
            {
                using (Stream stream = new FileStream(jpegPath, FileMode.Create))
                {
                    DicomImage image = new DicomImage(filePath);
                    //image.ShowOverlays = false;
                    //image.Scale = Math.Min(Math.Min(128.0 / image.Width, 128.0 / image.Height), 1.0);
                    //image.RenderImage().AsClonedBitmap().Save(stream, ImageFormat.Jpeg);

                    var sharpimage = image.RenderImage().AsSharpImage();

                    sharpimage.Save(stream, new JpegEncoder());
                   
                }
            }

            return new FileStream(jpegPath, FileMode.Open);
        }

        public static void RemovePreviewJpeg(string filePath)
        {
            string jpegPath = filePath + ".preview.jpg";
            if (File.Exists(jpegPath)) File.Delete(jpegPath);
        }
    }
}
