using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;

namespace BellwetherBackend.Utility
{
    public class GlobalFunction
    {
        //圖片規格
        public static string[] imgDimList = { ".jpg", ".png", ".bmp", ".jpeg" };
        //影片規格
        public static string[] mediaDimList = { ".avi", ".mp4", "mpeg", ".mpg", ".wmv" };
        //簡報
        public static string[] pptDimList = { ".ppt", ".pptx" };

        public static ImageSource GetBitMap(string imgpath)
        {
            BitmapImage bitmapImg = new BitmapImage();

            if (File.Exists(imgpath))
            {
                bitmapImg.BeginInit();
                bitmapImg.UriSource = new Uri(imgpath, UriKind.RelativeOrAbsolute);
                //忽略原始圖檔的色盤設定,使用預設值,可縮短載入圖檔的時間
                //如有遇到色彩不正確的問題,或者原圖有特殊的自定義色盤,請不要使用此設定
                bitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                //使用以下的預先載入設定,在BitmapImage創建完成之後,可立刻釋放其所占用或鎖住的資源,以免咬住檔案
                bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
                //根據欲顯示的區域長或寬,來設定 DecodePixelHeight 或 DecodePixelWidth 的值
                //bitmapImg.DecodePixelHeight = THUMB_HEIGHT;
                //bitmapImg.DecodePixelWidth = THUMB_WIDTH;
                bitmapImg.EndInit();
                bitmapImg.Freeze();
            }
            return bitmapImg;
        }

        public static ImageSource GetBitMap(string imgpath, double resolutionWidth, double resolutionHeight)
        {
            BitmapImage bitmapImg = new BitmapImage();

            if (File.Exists(imgpath))
            {
                bitmapImg.BeginInit();
                bitmapImg.UriSource = new Uri(imgpath, UriKind.RelativeOrAbsolute);
                //忽略原始圖檔的色盤設定,使用預設值,可縮短載入圖檔的時間
                //如有遇到色彩不正確的問題,或者原圖有特殊的自定義色盤,請不要使用此設定
                bitmapImg.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                //使用以下的預先載入設定,在BitmapImage創建完成之後,可立刻釋放其所占用或鎖住的資源,以免咬住檔案
                bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
                //根據欲顯示的區域長或寬,來設定 DecodePixelHeight 或 DecodePixelWidth 的值
                BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imgpath), BitmapCreateOptions.None, BitmapCacheOption.None);
                BitmapFrame frame = decoder.Frames[0];

                //ShellFile shellFile = ShellFile.FromFilePath(imgpath);
                var imageSourceHeight = frame.PixelHeight; //shellFile.Properties.System.Image.VerticalSize.Value;
                var imageSourceWidth = frame.PixelWidth;//shellFile.Properties.System.Image.HorizontalSize.Value;
                var imageSourceRatio = imageSourceWidth / (imageSourceHeight == 0 ? 1 : imageSourceHeight);

                if (imageSourceHeight > resolutionHeight && imageSourceWidth > resolutionWidth)
                {
                    if (imageSourceRatio > resolutionWidth / resolutionHeight)
                    {
                        bitmapImg.DecodePixelWidth = Convert.ToInt32(resolutionWidth);
                    }
                    else
                    {
                        bitmapImg.DecodePixelHeight = Convert.ToInt32(resolutionHeight);
                    }
                }
                else if (imageSourceHeight > resolutionHeight)
                {
                    bitmapImg.DecodePixelHeight = Convert.ToInt32(resolutionHeight);
                }
                else if (imageSourceWidth > resolutionWidth)
                {
                    bitmapImg.DecodePixelWidth = Convert.ToInt32(resolutionWidth);
                }
                bitmapImg.EndInit();
                bitmapImg.Freeze();
            }
            return bitmapImg;
        }

        public static bool CheckFileIsImage(string file)
        {
            if (GlobalFunction.imgDimList.Contains(System.IO.Path.GetExtension(file.ToLower())))
            {
                return true;
            }

            return false;
        }

        public static bool CheckFileIsVideo(string file)
        {
            if (GlobalFunction.mediaDimList.Contains(System.IO.Path.GetExtension(file.ToLower())))
            {
                return true;
            }

            return false;
        }

        public static string GetFolderFileName(string rootPath, string filename)
        {
            string result = "";
            Random Counter = new Random(Guid.NewGuid().GetHashCode());

            do
            {
                result = (Counter.Next(0, 10000)).ToString() + System.IO.Path.GetExtension(filename).ToLower();
            }
            while (File.Exists(Path.Combine(rootPath, result)));

            return result;
        }

        #region icon
        public static Geometry AddBtnData;
        public static Geometry DeleteBtnData;
        public static Geometry MoveUpBtnData;
        public static Geometry MoveDownBtnData;
        public static Geometry MapBtnData;
        public static Geometry ImgDeleteData;
        public static Geometry ConnectBtnData;
        public static Geometry DisconnectBtnData;
        public static Geometry UpdateBtnData;

        public GlobalFunction()
        {
            LoadAllIconData();
            //FillBaseFileSystem();
        }

        private Geometry LoadIconDataFromResource(string xamlName)
        {
            Geometry iconData = null;
            var assembly = GetType().Assembly;

            using (var stream = assembly.GetManifestResourceStream(xamlName))
            {
                var doc = XDocument.Load(stream);
                var path = doc.Root.Element("{http://schemas.microsoft.com/winfx/2006/xaml/presentation}Path");
                if (path != null)
                {
                    var data = (string)path.Attribute("Data");
                    iconData = PathGeometry.Parse(data);
                }
                return iconData;
            }
        }

        private void LoadAllIconData()
        {
            AddBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.add.xaml");
            DeleteBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.delete.xaml");
            MoveUpBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.upward.xaml");
            MoveDownBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.downward.xaml");
            MapBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.map.xaml");
            ImgDeleteData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.imgdelete.xaml");
            ConnectBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.connect.xaml");
            DisconnectBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.disconnect.xaml");
            UpdateBtnData = LoadIconDataFromResource("BellwetherBackend.Assets.appbar.control.update.xaml");
        }
        #endregion
    }

    public class ScrollViewerUtilities
    {
        #region HorizontalOffset

        /// <summary>
        /// HorizontalOffset Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty HorizontalOffsetProperty =
            DependencyProperty.RegisterAttached("HorizontalOffset", typeof(double), typeof(ScrollViewerUtilities),
                new FrameworkPropertyMetadata((double)0.0,
                    new PropertyChangedCallback(OnHorizontalOffsetChanged)));

        /// <summary>
        /// Gets the HorizontalOffset property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static double GetHorizontalOffset(DependencyObject d)
        {
            return (double)d.GetValue(HorizontalOffsetProperty);
        }

        /// <summary>
        /// Sets the HorizontalOffset property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static void SetHorizontalOffset(DependencyObject d, double value)
        {
            d.SetValue(HorizontalOffsetProperty, value);
        }

        /// <summary>
        /// Handles changes to the HorizontalOffset property.
        /// </summary>
        private static void OnHorizontalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var viewer = (ScrollViewer)d;
            viewer.ScrollToHorizontalOffset((double)e.NewValue);
        }

        #endregion

        #region VerticalOffset

        /// <summary>
        /// VerticalOffset Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty VerticalOffsetProperty =
            DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerUtilities),
                new FrameworkPropertyMetadata((double)0.0,
                    new PropertyChangedCallback(OnVerticalOffsetChanged)));

        /// <summary>
        /// Gets the VerticalOffset property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static double GetVerticalOffset(DependencyObject d)
        {
            return (double)d.GetValue(VerticalOffsetProperty);
        }

        /// <summary>
        /// Sets the VerticalOffset property.  This dependency property 
        /// indicates ....
        /// </summary>
        public static void SetVerticalOffset(DependencyObject d, double value)
        {
            d.SetValue(VerticalOffsetProperty, value);
        }

        /// <summary>
        /// Handles changes to the VerticalOffset property.
        /// </summary>
        private static void OnVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var viewer = (ScrollViewer)d;
            viewer.ScrollToVerticalOffset((double)e.NewValue);
        }

        #endregion
    }
}