using Hotline.Share.Dtos.Bigscreen;
using PanGu;
using System.ComponentModel;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace Hotline.Share.Tools;
public static class StringExtensions
{
    public static bool IsNullOrEmpty(this string? str)
    {
        return string.IsNullOrEmpty(str);
    }

    public static bool NotNullOrEmpty(this string? str)
    {
        return !string.IsNullOrEmpty(str);
    }

    /// <summary>
    /// 将字符串转换为指定的枚举类型。
    /// </summary>
    /// <typeparam name="TEnum">枚举类型</typeparam>
    /// <param name="value">要转换的字符串</param>
    /// <returns>枚举值</returns>
    public static TEnum ToEnum<TEnum>(this string value) where TEnum : struct
    {
        if (!typeof(TEnum).IsEnum)
        {
            throw new ArgumentException("TEnum must be an enumerated type");
        }

        // 尝试解析字符串为枚举
        return (TEnum)Enum.Parse(typeof(TEnum), value);
    }

    /// <summary>
    /// 将字符串转换为指定的枚举类型。
    /// </summary>
    /// <typeparam name="TEnum">枚举类型</typeparam>
    /// <param name="description">要转换的字符串</param>
    /// <returns>枚举值</returns>
    public static TEnum ToEnumByDesc<TEnum>(this string description) where TEnum : struct
    {
        if (!typeof(TEnum).IsEnum)
        {
            throw new ArgumentException("TEnum must be an enumerated type");
        }
        var fieldInfos = typeof(TEnum).GetFields();

        foreach(var fieldInfo in fieldInfos)
        {
            var desc = fieldInfo.GetCustomAttribute<DescriptionAttribute>();

            if (desc == null) continue;

            if(desc.Description == description)
            {
                return (TEnum)fieldInfo.GetValue(null);
            }
        }
       

        // 尝试解析字符串为枚举
        return default(TEnum);
    }

    /// <summary>
    /// 是否手机号码
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IsPhoneNumber(this string value)
    {
        if (string.IsNullOrWhiteSpace(value))
            return false;

        var phoneNumberPattern = @"^1[3-9]\d{9}$";
        return Regex.IsMatch(value, phoneNumberPattern);
    }

    /// <summary>
    /// 通过 PanGu 进行分词
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static IList<string> GetSegment(this string value)
    {
        return new Segment()
            .DoSegment(value)
            .Where(word => word.WordType == WordType.SimplifiedChinese && word.Word.Length > 1)
            .Select(word => word.Word)
            .ToList();
    }

    public static IList<string> SplitKeywords(this string value)
    {
        var regex = new Regex(@"[ ,，]+");

        return regex.Split(value)
                    .Where(s => !string.IsNullOrWhiteSpace(s))
                    .Select(s => s.Trim())
                    .ToList();
    }

    /// <summary>
    /// 获取字符串的 md5 
    /// </summary>
    /// <param name="value"> 字符串 </param>
    /// <returns> MD5 后的字符串<see cref="string"/>.  </returns>
    public static string GetMD5(this string value)
    {
        using (var md5 = MD5.Create())
        {
            return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(value))).Replace("-", string.Empty);
        }
    }

    /// <summary>
    /// 将 Stream 读取为 byte[]
    /// </summary>
    /// <param name="stream">要读取的 Stream</param>
    /// <returns>byte[]</returns>
    public static async Task<byte[]> ToByteArrayAsync(this Stream stream)
    {
        using (var memoryStream = new MemoryStream())
        {
            await stream.CopyToAsync(memoryStream);
            return memoryStream.ToArray();
        }
    }

    public static string GetHtmlImgSrc(this string value)
    {
        string pattern = @"<img\s[^>]*?src\s*=\s*[""'](?<src>[^""']+)[""']";
        Match match = Regex.Match(value, pattern, RegexOptions.IgnoreCase);

        if (match.Success)
        {
            return match.Groups["src"].Value;
        }
        return string.Empty;
    }

    /// <summary>
    /// 移除字符串中的所有 HTML 标签
    /// </summary>
    /// <param name="input">包含 HTML 的字符串</param>
    /// <returns>去除 HTML 标签后的字符串</returns>
    public static string RemoveHtmlTags(this string input)
    {
        if (string.IsNullOrWhiteSpace(input))
            return string.Empty;

        return Regex.Replace(input, "<.*?>", string.Empty);
    }
}
