using System.Globalization;
using System.IO.Abstractions;
using System.Linq;
using System.Threading;
using Valg.EVA.Skanning.UserInterfaceElements.UserSettings;

namespace Valg.EVA.Skanning.Scan.Startup
{
    public class LanguageResourceInitializer
    {
        public void SetCurrentCulture()
        {
            var userSettings = new UserSettingsProvider(new FileSystem());
            var language = userSettings.GetUserSettings().Language;
            if (!string.IsNullOrEmpty(language) && IsValidCultureInfoName(language))
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
            }
        }

        private static bool IsValidCultureInfoName(string name)
        {
            return
                CultureInfo
                .GetCultures(CultureTypes.SpecificCultures)
                .Any(c => c.Name == name);
        }
    }
}
