using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace YGNT.Infrastructure.Helper
{
    public static class StringHelp
    {

        public static readonly List<string> prefixVal = new List<string>()
        {
            "一","二","三","四","五","六","七","八","九","十",
            "1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30",
            "A","B","C","D","E","F","G","H","I","J",
            "a","b","c","d","e","f","g","h","i","j",
        };

        /// <summary>
        /// 字母 A-J
        /// </summary>
        public static readonly List<string> prefixLetter = new List<string>()
        {
            "A","B","C","D","E","F","G","H","I","J",
        };

        public static readonly List<string> prefixSymbol = new List<string>()
        {
            ",","，","`","·","、","."," "," ","．"
        };

        public static readonly List<string> prefixSum = new List<string>();

        /// <summary>
        /// 移除前缀
        /// </summary>
        /// <param name="text">文本</param>
        /// <param name="prefix">移除的前缀</param>
        /// <returns>移除后的字符串</returns>
        public static string RemovePrefix(string text, out string prefix)
        {
            prefix = string.Empty;
            string textp = text.Trim();

            if (string.IsNullOrWhiteSpace(text) || text.Length <= 2)
                return textp;

            if (!prefixSum.Any())
            {
                foreach (var item in prefixVal)
                {
                    foreach (var item2 in prefixSymbol)
                    {
                        prefixSum.Add(item + item2);
                    }
                }
            }

            foreach (var item in prefixSum)
            {
                if (textp.StartsWith(item))
                {
                    prefix = item.Substring(0, item.Length - 1);
                    return textp.Substring(item.Length);
                }
            }

            return textp;
        }

        /// <summary>
        /// 移除头加一个冒号或空格
        /// 如：答案：A，B，C   返回：A，B，C
        /// </summary>
        /// <returns></returns>
        public static string RemoveHeadPrefix(string text, string head)
        {
            if (text.StartsWith(head) && text.Length - head.Length > 0)
            {
                string e = text.Substring(head.Length, 1);

                List<string> ge = new List<string>() { ":", "：", " ", " " };

                if (ge.Contains(e))
                {
                    return text.Substring(head.Length + 1).Trim();
                }
            }
            return text;
        }

        /// <summary>
        /// 是否为手机号
        /// </summary>
        /// <param name="text">需要验证的文本</param>
        /// <returns></returns>
        public static bool IsPhone(string text)
        {
            Regex reger = new Regex(@"(1[3-9][0-9])\d{8}$");
            return reger.IsMatch(text);
        }


        /// <summary>
        /// 是否为身份证
        /// </summary>
        /// <param name="text">需要验证的文本</param>
        /// <returns></returns>
        public static bool IsIdCard(string text)
        {
            int l = text.Length;
            if (l == 18)
            {
                Regex reger = new Regex(@"[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$");
                return reger.IsMatch(text);
            }
            else if (l == 15)
            {
                Regex reger = new Regex(@"[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$");
                return reger.IsMatch(text);
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到身份证中的生日
        /// </summary>
        /// <param name="IdCard"></param>
        /// <returns></returns>
        public static DateTime? IdCardInBirthday(string IdCard)
        {
            try
            {
                //处理18位的身份证号码从号码中得到生日和性别代码
                if (IdCard.Length == 18)
                {
                    DateTime dateTime = new DateTime(int.Parse(IdCard.Substring(6, 4)), int.Parse(IdCard.Substring(10, 2)), int.Parse(IdCard.Substring(12, 2)));
                    return dateTime;
                }
                else if (IdCard.Length == 15)
                {
                    DateTime dateTime = new DateTime(int.Parse("19" + IdCard.Substring(6, 2)), int.Parse(IdCard.Substring(8, 2)), int.Parse(IdCard.Substring(10, 2)));
                    return dateTime;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 得到身份证中的性别
        /// </summary>
        /// <param name="IdCard"></param>
        /// <returns>男，女，null</returns>
        public static string IdCardInGender(string IdCard)
        {
            try
            {
                int? strSex = null;

                //处理18位的身份证号码从号码中得到生日和性别代码
                if (IdCard.Length == 18)
                {
                    strSex = int.Parse(IdCard.Substring(14, 3));
                }
                else if (IdCard.Length == 15)
                {
                    strSex = int.Parse(IdCard.Substring(12, 3));
                }

                if (strSex == null)
                    return string.Empty;

                //性别代码为偶数是女性奇数为男性
                if (strSex % 2 == 0)
                    return "女";
                else
                    return "男";
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 得到身份证中的性别
        /// </summary>
        /// <param name="IdCard"></param>
        /// <returns>1 男，2 女，null</returns>
        public static int? IdCardInGenderInt(string IdCard)
        {
            try
            {
                int? strSex = null;

                //处理18位的身份证号码从号码中得到生日和性别代码
                if (IdCard.Length == 18)
                {
                    strSex = int.Parse(IdCard.Substring(14, 3));
                }
                else if (IdCard.Length == 15)
                {
                    strSex = int.Parse(IdCard.Substring(12, 3));
                }

                if (strSex == null)
                    return null;

                //性别代码为偶数是女性奇数为男性
                if (strSex % 2 == 0)
                    return 2;
                else
                    return 1;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 正则表达式删除指定的HTML 标签
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public static string RemoveSpecifyHtml(string ctx)
        {
            string[] holdTags = { "a", "img", "br", "strong", "b", "span", "li" };//保留的 tag
            // <(?!((/?\s?li\b)|(/?\s?ul\b)|(/?\s?a\b)|(/?\s?img\b)|(/?\s?br\b)|(/?\s?span\b)|(/?\s?b\b)))[^>]+>
            string regStr = string.Format(@"<(?!((/?\s?{0})))[^>]+>", string.Join(@"\b)|(/?\s?", holdTags));
            Regex reg = new Regex(regStr, RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase);

            return reg.Replace(ctx, "");
        }

    }
}
