// Created with MonoDevelop
//
//    Copyright (C) 2022 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Reflection;
using Hiperscan.Unix;

namespace Hiperscan.Common.I18n
{
    public static class I18NEnumExtension
    {
        public const string NOT_TRANSLATED = "NOT_TRANSLATED";

        public static string xGetI18nString(this Enum enumeration, bool plural = false)
        {
            FieldInfo fi = enumeration.GetType().GetField(enumeration.ToString());

            if (plural == false)
            {
                I18NEnumGetStringAttribute[] attrs = fi.GetCustomAttributes(typeof(I18NEnumGetStringAttribute), false) as I18NEnumGetStringAttribute[];
                if (attrs != null && attrs.Length > 0)
                    return attrs[0].TranslatedStringValue;
            }
            else
            {
                I18NEnumGetPluralStringAttribute[] plural_attrs = fi.GetCustomAttributes(typeof(I18NEnumGetPluralStringAttribute), false) as I18NEnumGetPluralStringAttribute[];

                if (plural_attrs != null && plural_attrs.Length > 0)
                    return plural_attrs[0].TranslatedPluralStringValue;
            }

            return I18NEnumExtension.NOT_TRANSLATED;
        }

        public static Enum xParseToI18nEnum(this string value, Type enum_type)
        {
            foreach (Enum enumeration in enum_type.GetEnumValues())
            {
                FieldInfo fi = enumeration.GetType().GetField(enumeration.ToString());

                I18NEnumGetStringAttribute[] attrs = fi.GetCustomAttributes(typeof(I18NEnumGetStringAttribute), false) as I18NEnumGetStringAttribute[];
                if (attrs != null && attrs.Length > 0)
                {
                    if (attrs[0].TranslatedStringValue == value)
                        return enumeration;
                }

                I18NEnumGetPluralStringAttribute[] plural_attrs = fi.GetCustomAttributes(typeof(I18NEnumGetPluralStringAttribute), false) as I18NEnumGetPluralStringAttribute[];
                if (plural_attrs != null && plural_attrs.Length > 0)
                {
                    if (plural_attrs[0].TranslatedPluralStringValue == value)
                        return enumeration;
                }
            }

            string message = String.Format(Catalog.GetString("Cannot parse given i18n-string {0} to enum."), value);
            throw new Exception(message);
        }
    }
}
