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

namespace PushPlatform.Common.Util
{
    public static class StringUtil
    {
        public static bool IsNullOrEmpty(this object obj)
        {
            if (obj == null)
                return true;

            return string.IsNullOrEmpty(obj.ToString());
        }

        public static string ToIntStr(this bool obj)
        {
            return Convert.ToInt32(obj).ToString();
        }


        /// <summary>
        /// 字符串组数转字符
        /// </summary>
        /// <param name="arr">数组</param>
        /// <param name="decollator">分割符</param>
        /// <param name="fix">前后添加内容</param>
        /// <returns></returns>
        public static string ListToString(List<string> list, string decollator = ",", string fix = "")
        {
            string str = string.Empty;

            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    string temp = string.Empty;

                    if (!string.IsNullOrEmpty(fix))
                        temp = fix + item + fix;
                    else
                        temp = item;

                    str += temp + decollator;
                }
                str = str.Remove(str.LastIndexOf(decollator), 1);
            }

            return str;
        }

        /// <summary>
        /// 去空格、换行、单引号、双引号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string CleanString(string str)
        {
            return str.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace(" ", "").Replace("'", "‘").Replace("\"", "“").Replace("\\r\\n", "");
        }

        /// <summary>
        /// 去除换行符
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ClearEnter(string str)
        {
            return str.Replace("\r", "").Replace("\n", "").Replace("\t", "").Replace("\\r\\n", "").Replace("\\", "/");
        }

        /// <summary>
        /// 千分号
        /// </summary>
        /// <param name="value">值</param>
        /// <returns></returns>
        public static string FormatValue(decimal value)
        {
            string tempStr = value.ToString();
            Match m = Regex.Match(tempStr, @"(\d+)(\.\d+)?");
            string before_str = m.Groups[1].Value;
            string after_str = m.Groups[2].Value;
            before_str = string.Join("", before_str.Reverse());
            before_str = Regex.Replace(before_str, @"(?<=\G\d{3})(?=(?!$).)", ",");
            before_str = string.Join("", before_str.Reverse());
            return before_str + after_str;
        }

        /// <summary>
        /// 计算同比怎长
        /// </summary>
        /// <param name="current"></param>
        /// <param name="last"></param>
        /// <returns></returns>
        public static int GetIncrease(int current, int last)
        {
            if (last <= 0)
                return 100;
            else
                return Convert.ToInt32(((double)current - (double)last) / (double)last * 100);
        }

        /// <summary>
        /// 实现数据的四舍五入法
        　　 /// </summary>
        /// <param name="v">要进行处理的数据</param>
        /// <param name="x">保留的小数位数</param>
        /// <returns>四舍五入后的结果</returns>
        #region
        //public static double Round(double v, int x)
        //{
        //    bool isNegative = false;
        //    //如果是负数
        //    if (v < 0)
        //    {
        //        isNegative = true;
        //        v = -v;
        //    }

        //    int IValue = 1;
        //    for (int i = 1; i <= x; i++)
        //    {
        //        IValue = IValue * 10;
        //    }
        //    double Int = Math.Round(v * IValue + 0.5, 0);
        //    v = Int / IValue;

        //    if (isNegative)
        //    {
        //        v = -v;
        //    }

        //    return v;
        //}

        //public static double Roundex(double d, int i)
        //{
        //    if (d >= 0)
        //    {
        //        d += 5 * Math.Pow(10, -(i + 1));
        //    }
        //    else
        //    {
        //        d += -5 * Math.Pow(10, -(i + 1));
        //    }
        //    string str = d.ToString();
        //    string[] strs = str.Split('.');
        //    int idot = str.IndexOf('.');
        //    string prestr = strs[0];
        //    string poststr = strs[1];
        //    if (poststr.Length > i)
        //    {
        //        poststr = str.Substring(idot + 1, i);
        //    }
        //    string strd = prestr + "." + poststr;
        //    d = Double.Parse(strd);
        //    return d;
        //}
        #endregion
        public static double Round(double value, int decimals)
        {
            if (value < 0)
            {
                return Math.Round(value + 5 / Math.Pow(10, decimals + 1), decimals, MidpointRounding.AwayFromZero);
            }
            else
            {
                return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
            }
        }

        #region 正则表达式提取字符
        /// <summary>
        /// 正则表达式提取字符
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="regExpression">要提取的字符串正则表达式</param>
        /// <returns></returns>
        public static string[] Extract(this string input, string regExpression)
        {
            Regex reg = new Regex(regExpression, RegexOptions.IgnoreCase);

            MatchCollection matchCollection = reg.Matches(input);

            if (matchCollection.Count > 0)
            {
                string[] matchStr = new string[matchCollection.Count];

                for (int i = 0; i < matchCollection.Count; i++)
                {
                    matchStr[i] = matchCollection[i].Value;
                }

                return matchStr;
            }
            else
                return null;
        }
        #endregion
    }
}
