using System;
using System.Collections.Generic;
using Hiperscan.ApoIdentCommon.ApoConfig.Enums;
using Hiperscan.Common;
using Hiperscan.ApoIdentCommon;
using Hiperscan.Unix;
using Hiperscan.Common.I18n;

namespace Hiperscan.ApoIdentCommon.ApoConfig.Internationalization

{
    public static class I18n
    {
        public static ReportCountryTypes GetCountrySpecificReportCountry(ReportCountryTypes report_country)
        {
            if (I18n.GetRelevantReportCountries().Contains(report_country))
                return report_country;

            return I18n.GetDefaultReportCountry();
        }

        public static List<string> GetRelevantReportCountryNames()
        {
            List<string> sorted_countries = new List<string>();

            foreach (ReportCountryTypes report_country in I18n.GetRelevantReportCountries())
                sorted_countries.Add(I18n.GetReportCountryName(report_country));

            sorted_countries.Sort();
            return sorted_countries;
        }

        public static ReportCountryTypes GetDefaultReportCountry()
        {
            foreach (ReportCountryTypes report_country in I18n.GetRelevantReportCountries())
            {
                if (I18n.IsDefaultLanguage(report_country))
                    return report_country;
            }

            string message = String.Format(Catalog.GetString("There is no definition of a default ReportCountryTypes for language {0}."),
                                           I18n.AppicationLanguage);

            throw new NotImplementedException(message);
        }

        public static List<string> GetCurrencies()
        {
            List<string> currencies = new List<string>();

            foreach (ReportCountryTypes report_country in I18n.GetRelevantReportCountries())
                currencies.Add(I18n.GetCurrency(report_country));
                
            return currencies;
        }

        public static string GetReportCountryName(ReportCountryTypes report_country)
        {
            return report_country.xGetI18nString();
        }

        private static List<ReportCountryTypes> GetRelevantReportCountries()
        {
            List<ReportCountryTypes> countries = new List<ReportCountryTypes>();
            string language = I18n.AppicationLanguage;

            foreach (Enum field in Enum.GetValues(typeof(ReportCountryTypes)))
            {
                ReportCountryTypes value = (ReportCountryTypes)field;
                if (I18n.GetLanguage(value) == language)
                    countries.Add(value);

            }

            return countries;
        }

        private static string GetLanguage(ReportCountryTypes report_country)
        {
            return I18n.GetInfos(report_country)[0];
        }

        public static string GetCurrency(ReportCountryTypes report_country)
        {
            return I18n.GetInfos(report_country)[1];
        }

        private static bool IsDefaultLanguage(ReportCountryTypes report_country)
        {
            return I18n.GetInfos(report_country).Length == 3;
        }

        private static string[] GetInfos(ReportCountryTypes report_country)
        {
            StringEnum lang = new StringEnum(typeof(ReportCountryTypes));
            return lang.GetStringValue(report_country.ToString()).Split(new char[] { '_' });
        }

        private static string AppicationLanguage
        {
            get
            {
                string value = CatalogLocale.Language;
                if (value == CatalogLocale.C)
                    value = CatalogLocale.DEFAULT_LANGUAGE;

                return value;
            }
        }
    }
}
