using System;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
using System.Windows;
using Microsoft.WindowsAPICodePack.Shell;

namespace HistoryWallEditTool
{
    public class GlobalFunction
    {
        public static string rootPath = System.AppDomain.CurrentDomain.BaseDirectory;
        public static bool isSolutionUsing4K = false;

        //Data Path
        private static string applicationDataPath = rootPath + @"Data\";
        public static string historyWalldataFolderName = "HistoryWallPage";
        public static string dataPath => applicationDataPath + historyWalldataFolderName + @"\";
        //Setting Path
        public static string settingPath => dataPath + $"\\Setting\\";
        //Thumb Path
        public static string thumbPath => dataPath + $"\\Thumb\\";

        //json file and path
        public static string JsonFilePath => dataPath + $"\\HistoryWallData\\HistoryFile.ini";
        //config file and path
        public static string ConfigFilePath => settingPath + @"ConfigSetting.ini";

        //setting file 下的 .txt
        public static string CanChangeLangPath => settingPath + @"ChangeLan.txt";
        public static string CanHomeClickPath => settingPath + @"HomeClick.txt";
        public static string AutoRunVelocityPath => settingPath + @"Velocity.txt";
        public static string UsingDisplayOn4KsPath => settingPath + @"UsingViewerConfigFor4K.txt";

        //圖片規格
        public static string[] imgDimList = { ".jpg", ".png", ".bmp", ".jpeg" };
        //影片規格
        public static string[] mediaDimList = { ".avi", ".mp4", "mpeg", ".mpg", ".wmv" };

        public static BitmapSource GetMediaMap(string mediapath)
        {
            ShellFile shellFile = ShellFile.FromFilePath(mediapath);
            BitmapSource shellThumb = shellFile.Thumbnail.ExtraLargeBitmapSource;

            return shellThumb;
        }

        public static ImageSource GetBitMap(string imgpath, bool cusSize = false, int width = 0, int height = 0)
        {
            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 的值
                if (cusSize)
                {
                    if (width != 0)
                    {
                        bitmapImg.DecodePixelWidth = width;
                    }
                    else if (height != 0)
                    {
                        bitmapImg.DecodePixelHeight = height;
                    }
                }

                bitmapImg.EndInit();
                bitmapImg.Freeze();
            }
            return bitmapImg;
        }

        #region icon
        public static Geometry AddBtnData;
        public static Geometry DeleteBtnData;
        public static Geometry MoveUpBtnData;
        public static Geometry MoveDownBtnData;

        public GlobalFunction()
        {
            LoadAllIconData();
        }

        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("HistoryWallEditTool.Assets.appbar.control.add.xaml");
            DeleteBtnData = LoadIconDataFromResource("HistoryWallEditTool.Assets.appbar.control.delete.xaml");
            MoveUpBtnData = LoadIconDataFromResource("HistoryWallEditTool.Assets.appbar.control.upward.xaml");
            MoveDownBtnData = LoadIconDataFromResource("HistoryWallEditTool.Assets.appbar.control.downward.xaml");
        }
        #endregion
    }
}
