using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Windows.Controls;
using System.Drawing;
using System.Windows;
using System.Drawing.Drawing2D;

namespace HistoryWallEditTool.ControlsForData
{
    public partial class DetailDataViewer : UserControl
    {
        ImageCodecInfo imageCodecInfo;
        System.Drawing.Imaging.Encoder myEncoder;
        EncoderParameter myEncoderParameter;
        EncoderParameters myEncoderParameters;

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

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

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

        private 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 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);
        }

        private void CheckImageResolution(string fileFrom, string fileTo, bool is4K)
        {
            if (is4K)
            {
                TargetWidth *= 2;
                TargetHeight *= 2;
            }

            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;
                    // 20150316 降低 Dpi 的比率
                    float dpiRate = 1;

                    if (bit.Width >= bit.Height)
                    {
                        if (TransRateWidth > TargetWidth || TransRateHeight > TargetHeight)
                        {
                            GetTargetSize(bit);
                        }
                        // 20150316 根據寬被降低的比率來設定 Dpi 也必須降低的比率
                        dpiRate = bit.Width / TransRateWidth;
                    }
                    else
                    {
                        if (TransRateWidth > TargetHeight || TransRateHeight > TargetWidth)
                        {
                            GetTargetSize(bit);
                        }
                        // 20150316 根據高被降低的比率來設定 Dpi 也必須降低的比率
                        dpiRate = bit.Height / TransRateHeight;
                    }
                    // 20150316 取得新的 Dpi (總結 : MyDB\簡易碼\Dpi 設定說明.txt)
                    xDpi /= dpiRate;
                    yDpi /= dpiRate;
                    //取得新的 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 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;
            }
        }
    }
}
