using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Windows;

namespace FLY.Thick.Base.UI
{
    public class Culture : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public string Language { get; set; } = "zh";

        public string filePath = "culture.json";
        string fileFullPath = "culture.json";


        public bool Save()
        {
            return CultureJsonDb.Save(fileFullPath, this);
        }

        public bool Load()
        {
            //记录加载时的路径。 就算用  System.Environment.CurrentDirectory = rootDir; 改变后， 也能保存回加载时的位置 
            string path = System.IO.Directory.GetCurrentDirectory();

            fileFullPath = Path.Combine(path, filePath);

            return CultureJsonDb.Load(fileFullPath, this);
        }

        public void LoadCulture(Type appType)
        {
            Assembly asm = Assembly.GetAssembly(appType);
            string assemblyName = asm.GetName().Name;


            string language = Language;
            if (language == "zh")
                return;

            //List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
            //foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
            //{
            //    dictionaryList.Add(dictionary);
            //}
            string requestedCulture = $@"pack://application:,,,/{assemblyName};component/Themes/StringResources/{language}/StringResource.xaml";

            ResourceDictionary resourceDictionary;
            try
            {
                resourceDictionary = new ResourceDictionary() { Source = new Uri(requestedCulture) };
            }
            catch
            {
                return;
            }

            //if(resourceDictionary == null)
            //{
            //    requestedCulture = @"Resources\StringResource.xaml";
            //    resourceDictionary = dictionaryList.FirstOrDefault(d =< d.Source.OriginalString.Equals(requestedCulture));
            //}
            if (resourceDictionary != null)
            {

                //Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }
    }

    public class CultureJsonDb
    {
        public static bool Save(string filePath, Culture src)
        {


            CultureJsonDb jsonDb = new CultureJsonDb()
            {
                Language = src.Language
            };
            try
            {
                //string dirPath = Path.GetDirectoryName(filePath);

                //if (!Directory.Exists(dirPath))
                //    Directory.CreateDirectory(dirPath);

                var json = Newtonsoft.Json.JsonConvert.SerializeObject(jsonDb);
                File.WriteAllText(filePath, json);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool Load(string filePath, Culture src)
        {
            if (!File.Exists(filePath))
                return false;
            try
            {
                string json = File.ReadAllText(filePath);
                var jsonDb = Newtonsoft.Json.JsonConvert.DeserializeObject<CultureJsonDb>(json);
                src.Language = jsonDb.Language;
                return true;
            }
            catch
            {

                return false;
            }
        }
        public string Language { get; set; } = "zh";
    }
}
