using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows.Data;

namespace BeWo.Converter
{
	public class EnumToDescriptionConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (value == null)
				return null;

			var EnVal = value as DevExpress.Xpf.Core.EnumHelper.EnumItem;
			var fi2 = EnVal.Id.GetType().GetField(EnVal.Id.ToString());
			var attrs = (DescriptionAttribute[])fi2.GetCustomAttributes(typeof(DescriptionAttribute), true);

			return !attrs.Any() ? value : attrs[0].Description;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return value;
		}
	}
}