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

namespace OfficeAppHelp.Help
{
    public static class StringHelp
    {

        /// <summary>
        /// 得到编号 如 "1."或"  2. ";
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="isTrim">是否移除前后空格 返回如："1."或"2."</param>
        /// <returns></returns>
        public static string GetSerialNumber(this string txt, bool isTrim = true)
        {
            if (string.IsNullOrWhiteSpace(txt))
                return string.Empty;

            char[] empty = new char[] { ' ', '\t' };
            string regexSerialNumber = @"^[\d]+[\.\,\:\-\．\，\：\－\、]";

            string newTxt = txt.TrimStart(empty);

            ////^[\d]+\.|^[a-zA-Z]+\.
            //StringBuilder splitReguler = new StringBuilder();
            ////需要匹配的序列 符号
            //string mChar = string.Join("\\",
            //    new string[]
            //    {
            //        ".", ",", ":", "-",//半角
            //        "．","，","：","－",//全角
            //        "、",//中文
            //    });


            ////匹配数字序号 1. 10.
            //splitReguler.AppendFormat(@"^[\d]+[\{0}]", mChar);
            ////匹配字母序号 a. A.
            ////splitReguler.AppendFormat(@"|^[a-zA-Z][\{0}]", mChar);

            //string splitChar = Regex.Match(newTxt, splitReguler.ToString()).Value;
            string splitChar = Regex.Match(newTxt, regexSerialNumber).Value;
            if (string.IsNullOrEmpty(splitChar))
                return string.Empty;

            if (isTrim)
            {
                return splitChar;
            }
            else
            {
                //取 开头的空格
                string te = txt.Substring(0, txt.Length - newTxt.Length);
                //取 去掉编号后的空格
                string newTxt2 = newTxt.Substring(splitChar.Length);
                string newTxt3 = newTxt2.TrimStart(empty);
                string te2 = newTxt2.Substring(0, newTxt2.Length - newTxt3.Length);

                return te + splitChar + te2;
            }

        }

        /// <summary>
        /// 移除符号（只包含：字母，数字，中文）
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static string RemoveSymbol(this string txt)
        {
            return string.Join(null, txt.RegexMatche("[A-Za-z0-9\u4e00-\u9fa5-]+"));
        }

        /// <summary>
        /// 在指定的输入字符串中搜索指定的正则表达式的所有匹配项
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="pattern">正则表达式</param>
        /// <returns></returns>
        public static List<string> RegexMatche(this string txt, string pattern)
        {
            List<string> strRet = new List<string>();
            MatchCollection results = Regex.Matches(txt, pattern);
            foreach (var v in results)
            {
                strRet.Add(v.ToString());
            }
            return strRet;
        }

        /// <summary>
        /// 移除转义字符
        /// </summary>
        /// <param name="sourceString">待处理字符串</param>
        /// <returns>清空转义字符后的字符串</returns>
        public static string RemoveESC(this string txt)
        {
            string strRet = "";
            for (int i = 0; i < txt.Length; i++)
            {
                int Unicode = txt[i];
                if (Unicode > 31 && Unicode != 127)
                {
                    strRet += txt[i].ToString();
                }
            }
            return strRet;
        }

        /// <summary>
        /// 得到所有的大写字母
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static List<string> GetUpperLetter(this string txt)
        {
            return txt.RegexMatche("[A-Z]");
        }

        /// <summary>
        /// 得到所有的大写字母
        /// </summary>
        /// <param name="txt"></param>
        /// <param name="separator">分隔符</param>
        /// <returns></returns>
        public static string GetUpperLetter(this string txt, string separator)
        {
            return string.Join(separator, txt.RegexMatche("[A-Z]"));
        }

        /// <summary>
        /// 字符串转为Base64字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToBase64(this string str)
        {
            byte[] b = Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(b, Base64FormattingOptions.None);
        }

        /// <summary>
        /// Base64字符串转为字符串
        /// </summary>
        /// <param name="base64str">Base64字符串</param>
        /// <returns></returns>
        public static string Base64ToString(this string base64str)
        {
            byte[] c = Convert.FromBase64String(base64str);
            return Encoding.UTF8.GetString(c);
        }

        /// <summary>
        /// Base64字符串转为字符串
        /// </summary>
        /// <param name="base64str"></param>
        /// <param name="isOk">是否成功</param>
        /// <returns>成功返回结果，不成功返回Empty</returns>
        public static string Base64ToStringTry(this string base64str, out bool isOk)
        {
            try
            {
                isOk = true;
                return Base64ToString(base64str);
            }
            catch
            {
                isOk = false;
                return string.Empty;
            }
        }

    }
}
