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

namespace BellwetherBackend.Utility
{
    public class ReduceImageResolution
    {
        private static ImageCodecInfo imageCodecInfo;
        private static System.Drawing.Imaging.Encoder myEncoder;
        private static EncoderParameter myEncoderParameter;
        private static EncoderParameters myEncoderParameters;

        //所要縮圖的目標寬高~
        private static int TargetWidth = 950;
        private static int TargetHeight = 620;
        private static int TransRateWidth = 0;
        private static int TransRateHeight = 0;

        private static float GetImageResolutionHeight(Bitmap bit)
        {
            return bit.HorizontalResolution;
        }

        private static float GetImageResolutionWidth(Bitmap bit)
        {
            return bit.VerticalResolution;
        }

        private static long GetImageDepth(Bitmap bit)
        {
            long result = 0;

            switch (bit.PixelFormat)
            {
                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:
                    {
                        result = 8;
                        break;
                    }
                case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                    {
                        result = 24;
                        break;
                    }
                case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
                    {
                        result = 32;
                        break;
                    }
            }

            return result;
        }

        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();

            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                {
                    return encoders[j];
                }
            }

            return null;
        }

        private static ImageCodecInfo GetImageCodec(string path)
        {
            string str = "jpeg";

            switch (System.IO.Path.GetExtension(path).ToLower())
            {
                case ".bmp":
                    {
                        str = "bmp";
                        break;
                    }
                case ".gif":
                    {
                        str = "gif";
                        break;
                    }
                case ".tiff":
                    {
                        str = "tiff";
                        break;
                    }
                case ".png":
                    {
                        str = "png";
                        break;
                    }
            }
            return GetEncoderInfo("image/" + str);
        }

        private void CheckMediaResolution(string fileFrom, string fileTo)
        {
            File.Copy(fileFrom, fileTo);
        }

        public static void CheckImageResolution(string fileFrom, string fileTo, int resolutionWidth, int resolutionHeight)
        {
            TargetWidth = resolutionWidth;
            TargetHeight = resolutionHeight;

            myEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
            myEncoderParameters = new EncoderParameters(1);

            try
            {
                if (File.Exists(fileFrom))
                {
                    Bitmap bit = new Bitmap(fileFrom);
                    //先將原本圖片的影像深度及 Dpi 存起來
                    float xDpi = GetImageResolutionWidth(bit);
                    float yDpi = GetImageResolutionHeight(bit);
                    long depth = GetImageDepth(bit);
                    imageCodecInfo = GetImageCodec(fileFrom);

                    TransRateWidth = bit.Width;
                    TransRateHeight = bit.Height;

                    if (bit.Width >= bit.Height)
                    {
                        if (TransRateWidth > TargetWidth || TransRateHeight > TargetHeight)
                        {
                            GetTargetSize(bit);
                        }
                    }
                    else
                    {
                        if (TransRateWidth > TargetHeight || TransRateHeight > TargetWidth)
                        {
                            GetTargetSize(bit);
                        }
                    }
                    //取得新的 bitmap
                    bit = KiResizeImage(bit, TransRateWidth, TransRateHeight);
                    //依照原本的影像深度及DPI來重新設定新圖示的影像深度 及 DPI
                    bit.SetResolution(xDpi, yDpi);
                    myEncoderParameter = new EncoderParameter(myEncoder, depth);
                    myEncoderParameters.Param[0] = myEncoderParameter;
                    //產生圖片
                    bit.Save(fileTo, imageCodecInfo, myEncoderParameters);

                    bit.Dispose();
                }
            }
            catch (Exception)
            {

            }
        }

        private static void GetTargetSize(Bitmap bmp)
        {
            //必須考慮兩種狀況 (橫或者直)
            if (bmp.Width >= bmp.Height)
            {
                TransRateWidth = TargetWidth;
                TransRateHeight = TargetHeight;
                //橫
                //第二考慮 : 往哪個方向縮
                if (bmp.Width >= TransRateWidth)
                {
                    decimal result = Math.Round((decimal)TransRateWidth / bmp.Width, 5);
                    //寬比高大~代表為橫式~縮圖為 : 設定的寬, 設定的高 / 寬的縮圖比例
                    TransRateHeight = (int)(bmp.Height * result);
                }
                else if (bmp.Height >= TransRateHeight)
                {
                    decimal result = Math.Round((decimal)TransRateHeight / bmp.Height, 5);
                    //高比寬大~代表為直式~縮圖為 : 設定的寬 / 高的縮圖比例, 設定的高 
                    TransRateWidth = (int)(bmp.Width * result);
                }
            }
            else if (bmp.Height > bmp.Width)
            {
                TransRateWidth = TargetHeight;
                TransRateHeight = TargetWidth;
                //直
                //第二考慮 : 往哪個方向縮
                if (bmp.Width >= TransRateWidth)
                {
                    decimal result = Math.Round((decimal)TransRateWidth / bmp.Width, 5);
                    //寬比高大~代表為橫式~縮圖為 : 設定的寬, 設定的高 / 寬的縮圖比例
                    TransRateHeight = (int)(bmp.Height * result);
                }
                else if (bmp.Height >= TransRateHeight)
                {
                    decimal result = Math.Round((decimal)TransRateHeight / bmp.Height, 5);
                    //高比寬大~代表為直式~縮圖為 : 設定的寬 / 高的縮圖比例, 設定的高 
                    TransRateWidth = (int)(bmp.Width * result);
                }
            }
        }

        public static Bitmap KiResizeImage(Bitmap bmp, int newW, int newH)
        {
            try
            {
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                // 插值算法的質量              
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.CompositingMode = CompositingMode.SourceCopy;
                g.Clear(Color.Transparent);
                g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, newW, newH), new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);

                g.Dispose();
                bmp.Dispose();
                bmp = null;
                return b;
            }
            catch
            {
                return null;
            }
        }
    }
}