// Created with MonoDevelop
//
//    Copyright (C) 2023 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
// 

using System;
using System.Linq;
using System.Reflection;

namespace Hiperscan.Common.I18n
{
    public static class I18nHelper
    {
        public static string GetI18nConstString(Type type, object value)
        {
            FieldInfo[] field_infos = type.GetFields(
            BindingFlags.Public |
                BindingFlags.Static |
            BindingFlags.FlattenHierarchy);

            foreach (FieldInfo field_info in field_infos)
            {
                if (field_info.IsLiteral && !field_info.IsInitOnly)
                {
                    I18NConstGetStringAttribute[] attrs = field_info.GetCustomAttributes(typeof(I18NConstGetStringAttribute), false) as I18NConstGetStringAttribute[];
                    if (attrs != null && attrs.Length > 0)
                    {
                        if (field_info.GetValue(null).ToString() == value.ToString())
                            return attrs[0].TranslatedStringValue;
                    }

                }
            }

            throw new Exception(String.Format("Could not find any translation for given value {0}", value.ToString()));
        }
    }
}
