using DocumentFormat.OpenXml.Packaging;
using System.Drawing;

namespace PhotoTkutimes.Models
{
    public class ImageData       
    {
        public const int DPI = 150;
        public string FileName = string.Empty;
        public byte[] BinaryData;
        public Stream DataStream => new MemoryStream(BinaryData);
        public ImagePartType ImageType
        {
            get
            {
                var ext = Path.GetExtension(FileName).TrimStart('.').ToLower();
                switch (ext)
                {
                    case "jpg":
                        return ImagePartType.Jpeg;
                    case "png":
                        return ImagePartType.Png;
                    case "":
                        return ImagePartType.Gif;
                    case "bmp":
                        return ImagePartType.Bmp;
                }
                throw new ApplicationException($"Unsupported image type: {ext}");
            }
        }
        public int SourceWidth;
        public int SourceHeight;
        public decimal Width;
        public decimal Height;
        public long WidthInEMU => Convert.ToInt64(Width * CM_TO_EMU);
        public long HeightInEMU => Convert.ToInt64(Height * CM_TO_EMU);
        private const decimal INCH_TO_CM = 2.54M;
        private const decimal CM_TO_EMU = 360000M;
        public string ImageName;
        public ImageData(string fileName, byte[] data, int dpi = DPI)
        {
            FileName = fileName;
            BinaryData = data;
            Bitmap img = new Bitmap(new MemoryStream(data));
            SourceWidth = img.Width;
            SourceHeight = img.Height;
            Width = ((decimal)SourceWidth) / dpi * INCH_TO_CM;
            Height = ((decimal)SourceHeight) / dpi * INCH_TO_CM;
            ImageName = $"IMG_{Guid.NewGuid().ToString().Substring(0, 8)}";
        }
        public ImageData(string fileName, int dpi = DPI) :
            this(fileName, File.ReadAllBytes(fileName), dpi)
        {
        }
    }
}
