using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Markup;
using InABox.Core;

namespace InABox.WPF;

public class EnumFormatterExtension : MarkupExtension
{
    private readonly Type _type;

    public EnumFormatterExtension(Type type)
    {
        _type = type;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Enum.GetValues(_type)
            .Cast<object>()
            .Select(e => new { Value = (int)e, DisplayName = $"{e}".SplitCamelCase() });
    }
}

public class EnumToStringConverter : AbstractConverter<object?,string>
{
    private string GetDescription(Type enumType, object? enumValue)
    {
        if (enumValue == null)
            return string.Empty;

        return
            enumType
                .GetField($"{enumValue}")?
                .GetCustomAttributes(typeof(DescriptionAttribute), false)
                .FirstOrDefault() is DescriptionAttribute _descriptionAttribute
                ? _descriptionAttribute.Description
                : $"{enumValue}".SplitCamelCase() ?? "";
    }

    public override string Convert(object? value)
    {
        if (value == null)
            return String.Empty;

        Type _type = value.GetType();

        return
            _type.IsEnum
                ? GetDescription(_type, value)
                : String.Empty;
    }
}