using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using OpenCvSharp;
using Tesseract;
using ImageFormat = System.Drawing.Imaging.ImageFormat;

namespace Scan.ReadSoftEmulation
{
    internal class TempScanFiles : IDisposable
    {
        
        private readonly IList<string> _files = new List<string>();
        public TempScanFiles(string tempDir, int dpi, params System.Drawing.Image[] images)
        {
            var ticks = DateTime.Now.Ticks;
            for (var i = 0; i < images.Length; ++i)
            {
                var image = (Bitmap)images[i];
                image.SetResolution(dpi, dpi);
                var fileName = Path.Combine(tempDir, Path.ChangeExtension($"{ticks}_{i + 1}", ".tiff"));
                image.Save(fileName, ImageFormat.Tiff);
                _files.Add(fileName);
            }
        }
        public TempScanFiles(string tempDir, int dpi, params Mat[] images)
        {
            var ticks = DateTime.Now.Ticks;
            for (var i = 0; i < images.Length; ++i)
            {
                var image = images[i];
                var fileName = Path.Combine(tempDir, Path.ChangeExtension($"{ticks}_{i + 1}", ".tiff"));
                image.SaveImage(fileName);
                _files.Add(fileName);
            }
        }

        public TempScanFiles(string tempDir, params Tesseract.Pix[] images)
        {
            var ticks = DateTime.Now.Ticks;
            for (var i = 0; i < images.Length; ++i)
            {
                var image = images[i];
                var fileName = Path.Combine(tempDir, Path.ChangeExtension($"{ticks}_{i + 1}", "tiff")); 
                image.Save(fileName, Tesseract.ImageFormat.TiffLzw);
                _files.Add(fileName);
            }
        }

        public TempScanFiles(string tempDir, params byte[][] buffers)
        {
            var ticks = DateTime.Now.Ticks;
            for (var i = 0; i < buffers.Length; ++i)
            {
                var buffer = buffers[i];
                var fileName = Path.Combine(tempDir, Path.ChangeExtension($"{ticks}_{i + 1}", "tiff")); 
                File.WriteAllBytes(fileName, buffer);
                _files.Add(fileName);
            }
        }

        private static string GetFileNameExtensionForImage(Image image)
        {
            if (image.RawFormat.Equals(ImageFormat.Bmp))
                return "bmp";
            if (image.RawFormat.Equals(ImageFormat.Tiff))
                return "tiff";
            if (image.RawFormat.Equals(ImageFormat.Jpeg))
                return "jpeg";
            if (image.RawFormat.Equals(ImageFormat.Png))
                return "png";
            if (image.RawFormat.Equals(ImageFormat.Gif))
                return "gif";
            return "dat";

        }

        public ICollection<string> Files => new ReadOnlyCollection<string>(_files);

        public void Dispose()
        {
            foreach (var file in _files)
            {
                try
                {
                    System.IO.File.Delete(file);
                }
                catch (System.IO.IOException)
                {

                }
            }
        }
    }
}

