using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CatalogVbLib;

namespace CatalogLib
{
    public class ImageCreator2 : IImageProcessor
    {
        private PicSettings _picSettings = PicSettings.Instance;

        private FileInfo _workFile;

        private Image _workingImage;

        private Rotazione _rotation;

        private ImageFormat _currentFormat;

        private Size _newImageSize;

        private Bitmap _outputImage;

        private DirectoryInfo _destDir;

        private string _nomeFileBig;

        public void Start(FileInfo workFile)
        {
            _workFile = workFile;

            _workingImage = Image.FromFile(workFile.FullName);

            _destDir = new DirectoryInfo(_picSettings.DirectoryDestinazione);
            _nomeFileBig = _workFile.Name;

            CalculateImageSize();

            ElaborazioneTesto();

            ElaborazioneRotazione();

            SetImageFormat();


            FinalElaboration();

            //CreaMiniature
        }

        private void FinalElaboration()
        {
            _outputImage = new Bitmap(_workingImage, _newImageSize.Width, _newImageSize.Height);
            _outputImage.SetResolution(_workingImage.HorizontalResolution, _workingImage.VerticalResolution);


            SavePic(_outputImage, Path.Combine(_destDir.FullName, _nomeFileBig));
        }

        private void SavePic(Bitmap imageToSave, string fileName)
        {
            var selectedEncoder = GetEncoder(_currentFormat);
            var encoder = System.Drawing.Imaging.Encoder.Quality;
            var encoderParameters = new EncoderParameters(1);

            
            if (Equals(_currentFormat, ImageFormat.Jpeg))
            {
                var encoderParameter = new EncoderParameter(encoder, _picSettings.CompressioneJpeg);
                encoderParameters.Param[0] = encoderParameter;
            }

            imageToSave.Save(fileName, selectedEncoder,encoderParameters);
            imageToSave.Dispose();
        }

        private ImageCodecInfo GetEncoder(ImageFormat format)
        {
            var codecs = ImageCodecInfo.GetImageDecoders();

            return codecs.FirstOrDefault(c => c.FormatID == format.Guid);
        }

        private void ElaborazioneTesto()
        {
            if (_picSettings.EnableText)
            {
                // todo: elaborazione testo
            }
        }

        private void CalculateImageSize()
        {
            _newImageSize = new Size(_workingImage.Width, _workingImage.Height);
        }

        private void ElaborazioneRotazione()
        {
            
            _rotation = Rotazione.Normale;

            if (_picSettings.GeneraleRotazioneAutomatica)
            {
                // ci sono dati exif
                if (_workingImage.PropertyIdList.Length > 0)
                {
                    ExifReader DatiExif = new ExifReader((Bitmap)_workingImage);

                    switch (DatiExif.Orientation)
                    {
                        case ExifReader.Orientations.BottomLeft:

                            break;
                        case ExifReader.Orientations.BottomRight:

                            break;
                        case ExifReader.Orientations.LeftTop:

                            break;
                        case ExifReader.Orientations.LftBottom:
                            //FotoRuotaASinistra = true;
                            _rotation = Rotazione.Sinistra;
                            break;
                        case ExifReader.Orientations.RightBottom:

                            break;
                        case ExifReader.Orientations.RightTop:

                            break;
                        case ExifReader.Orientations.TopLeft:

                            break;
                        case ExifReader.Orientations.TopRight:

                            break;
                    }
                }
            }

            if (_rotation == Rotazione.Sinistra)
            {
                _workingImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
            }
            else
            if (_rotation == Rotazione.Destra)
            {
                _workingImage.RotateFlip(RotateFlipType.Rotate90FlipNone);
            }
        }

        private void SetImageFormat()
        {
            _currentFormat = _workingImage.RawFormat;
            if (_picSettings.GeneraleForzaJPG)
            {
                _currentFormat = ImageFormat.Jpeg;
            }
        }

    }
}
