using Hotline.Share.Attributes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotline.Share.Tools;
public static class EnumExtensions
{
    /// <summary>
    /// 获取枚举项上的<see cref="FileExtensionAttribute"/>特性的文字描述
    /// </summary>
    /// <param name="value">枚举</param>
    /// <returns>属性值</returns>
    public static string GetFileExtension(this Enum value)
    {
        var type = value.GetType();
        var member = type.GetMember(value.ToString()).FirstOrDefault();
        var desc = member?.GetAttribute<FileExtensionAttribute>();
        if (desc != null)
        {
            return desc.FileExtension;
        }
        return string.Empty;
    }

    /// <summary>
    /// 获取枚举项上的<see cref="ContentTypeAttribute"/>特性的文字描述
    /// </summary>
    /// <param name="value">枚举</param>
    /// <returns>属性值</returns>
    public static string GetContentType(this Enum value)
    {
        var type = value.GetType();
        var member = type.GetMember(value.ToString()).FirstOrDefault();
        var desc = member?.GetAttribute<ContentTypeAttribute>();
        if (desc != null)
        {
            return desc.ContentType;
        }
        return string.Empty;
    }
}
