using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using System.Resources;
using Valg.EVA.Skanning.Resources.Language;

namespace Valg.EVA.Skanning.Resources
{
    public static class LanguageResource
    {
        private static LanguageString languageString;
        private static ScanLanguageString scanLanguageString;
        private static VerifyLanguageString verifyLanguageString;
        private static SampleLanguageString sampleLanguageString;
        private static JobManagementLanguageString jobManagementLanguageString;
        private static CultureInfo culture;

        public static void Initialize(CultureInfo cultureInfo)
        {
            languageString = null;
            scanLanguageString = null;
            verifyLanguageString = null;
            jobManagementLanguageString = null;
            sampleLanguageString = null;
            culture = cultureInfo;
        }

        public static LanguageString LanguageString
        {
            get { return languageString ?? (languageString = new LanguageString()); }
        }

        public static ScanLanguageString ScanLanguageString
        {
            get { return scanLanguageString ?? (scanLanguageString = new ScanLanguageString()); }
        }

        public static VerifyLanguageString VerifyLanguageString
        {
            get { return verifyLanguageString ?? (verifyLanguageString = new VerifyLanguageString()); }
        }


        public static JobManagementLanguageString JobManagementLanguageString
        {
            get { return jobManagementLanguageString ?? (jobManagementLanguageString = new JobManagementLanguageString()); }
        }

        public static SampleLanguageString SampleLanguageString
        {
            get { return sampleLanguageString ?? (sampleLanguageString = new SampleLanguageString()); }
        }
        public static string GetText(string resourceKey)
        {
            var fileAndKey = resourceKey.Split('.');
            var resourceManager = new ResourceManager(string.Format("Valg.EVA.Skanning.Resources.Language.{0}", fileAndKey[0]), Assembly.GetExecutingAssembly());
            return resourceManager.GetString(fileAndKey[1], culture);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
        public static string GetResourceKey<T>(Expression<Func<T>> property)
        {
            return property.Body.ToString();
        }

        public static string GetEnumString<TEnum>(this ResourceManager resourceManager, TEnum property)
            where TEnum : struct, IConvertible
        {
            if (!typeof(TEnum).IsEnum)
            {
                throw new ArgumentException("T must be an enumerated type");
            }
            var resourceKey = string.Format("{0}_{1}", typeof(TEnum).Name, property);
            return resourceManager
                .GetResourceSet(culture, true, false)
                .GetString(resourceKey);
        }

        public static string GetEnumString<TEnum>(this ResourceManager resourceManager, TEnum property, string resourceKeyPrefix)
            where TEnum : struct, IConvertible
        {
            if (!typeof(TEnum).IsEnum)
            {
                throw new ArgumentException("T must be an enumerated type");
            }
            var parts = new List<string>();
            parts.Add(typeof(TEnum).Name);
            if (!string.IsNullOrWhiteSpace(resourceKeyPrefix))
                parts.Add(resourceKeyPrefix);
            parts.Add(property.ToString());

            var resourceKey = string.Join("_", parts);

            return resourceManager
                .GetResourceSet(culture, true, false)
                .GetString(resourceKey);
        }
    }
}

