// Copyright © Selmo Technology GmbH. All Rights Reserved

using SelmoStudio.Hmi.Server.Languages.Models;
using System.Globalization;

namespace SelmoStudio.Hmi.Server.Languages.Factory
{
    internal static class HmiLanguageFactory
    {
        internal static IReadOnlyList<HmiLanguage> CreateFromLcid(IEnumerable<string> lcidCodes)
        {
            var languages = new List<HmiLanguage>();

            if (lcidCodes != null)
            {
                foreach (var lcid in lcidCodes)
                {
                    languages.Add(CreateFromLcid(lcid));
                }
            }

            return languages;
        }

        private static HmiLanguage CreateFromLcid(string lcid)
        {
            try
            {
                if (lcid.Equals("1"))
                {
                    lcid = "1033";
                }

                var culture = new CultureInfo(int.Parse(lcid));

                return new HmiLanguage(culture.NativeName, lcid);
            }
            catch (CultureNotFoundException)
            {
                throw new ArgumentException($"Invalid LCID: {lcid}");
            }
        }
    }
}