using Common.system;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace XHWK.WKTool.Skin
{
    /// <summary>
    /// 皮肤配置
    /// 创建人：赵耀
    /// 创建时间：2021年6月18日
    /// </summary>
    public class SkinConfig
    {
        /// <summary>
        /// 皮肤列表  0白 1蓝 2黑色
        /// </summary>
        static List<ResourceDictionary> SkinList = new List<ResourceDictionary>();
        /// <summary>
        /// 初始化皮肤
        /// </summary>
        public static void InitSkin()
        {
            SkinList.Add(new ResourceDictionary()
            {
                Source = new Uri("pack://application:,,,/Skin/SkinDictionary_White.xaml", UriKind.Absolute)
            });
            SkinList.Add(new ResourceDictionary()
            {
                Source = new Uri("pack://application:,,,/Skin/SkinDictionary_TechnologyBlue.xaml", UriKind.Absolute)
            });
            int Index = 0;
            try
            {
                Index = int.Parse(App.SkinStyle);
            }
            catch (Exception) { }
            ChangeSkinResource(SkinList[int.Parse(App.SkinStyle)]);
        }

        /// <summary>
        /// 切换皮肤并保存  0白 1蓝 2黑色
        /// </summary>
        /// <param name="Index"> 0白 1蓝 2黑色</param>
        public static void SwitchSkin(int Index)
        {
            if (Index < SkinList.Count)
            {
                App.SkinStyle = Index.ToString();
                FileToolsCommon.SetConfigValue("SkinStyle", App.SkinStyle);
                ChangeSkinResource(SkinList[int.Parse(App.SkinStyle)]);
            }

        }

        /// <summary>
        /// 更换皮肤资源
        /// </summary>
        /// <param name="skin"></param>
        private static void ChangeSkinResource(ResourceDictionary skin)
        {
            if(Application.Current.Resources.MergedDictionaries.Count<1)
            {
                Application.Current.Resources.MergedDictionaries.Add(skin);//[0] = new ResourceDictionary();
                //Application.Current.Resources.MergedDictionaries[0]=skin;
            }
            if (Application.Current.Resources.MergedDictionaries[0].Source.IsAbsoluteUri)
            {
                if (Application.Current.Resources.MergedDictionaries[0].Source.OriginalString != skin.Source.OriginalString)
                {
                    Application.Current.Resources.MergedDictionaries[0] = skin;
                }
            }
            else
            {
                if (Application.Current.Resources.MergedDictionaries[0].Source.OriginalString.ToString('\\') != skin.Source.OriginalString.ToString('/'))
                {
                    Application.Current.Resources.MergedDictionaries[0] = skin;
                }
            }
        }
    }
    /// <summary>
    /// 扩展方法
    /// </summary>
    public static class ExtensionMethod
    {
        public static string ToString(this string content, char split)
        {
            return content.Split(split).Last();
        }
    }
}
