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

namespace ryCommon
{
    /// <summary>
    /// 对变量的拓展
    /// </summary>
    public static class VarExtension
    {
        /// <summary>
        /// 替换文本(忽略大小写)
        /// </summary>
        /// <param name="original"></param>
        /// <param name="pattern">需要替换的内容</param>
        /// <param name="replacement">替换后的内容</param>
        /// <returns></returns>
        static public string ReplaceEx(this string original, string pattern, string replacement)
        {
            int count, position0, position1;
            count = position0 = position1 = 0;
            string upperString = original.ToUpper();
            string upperPattern = pattern.ToUpper();
            int inc = (original.Length / pattern.Length) * (replacement.Length - pattern.Length);
            char[] chars = new char[original.Length + Math.Max(0, inc)];
            while ((position1 = upperString.IndexOf(upperPattern, position0)) != -1)
            {
                for (int i = position0; i < position1; ++i) chars[count++] = original[i];
                for (int i = 0; i < replacement.Length; ++i) chars[count++] = replacement[i];
                position0 = position1 + pattern.Length;
            }
            if (position0 == 0) return original;
            for (int i = position0; i < original.Length; ++i) chars[count++] = original[i];
            return new string(chars, 0, count);
        }
        /// <summary>
        /// 替换文本(忽略大小写),可支持通配符
        /// </summary>
        /// <param name="original"></param>
        /// <param name="pattern"></param>
        /// <param name="replacement"></param>
        /// <param name="isLike">是否是通配符表达式,不是，则普通替换，忽略大小写，是，则启用通配符替换</param>
        /// <returns></returns>
        static public string ReplaceEx(this string original, string pattern, string replacement,bool isLike)
        {
            if(isLike)
            {
                return Strings.ReplaceByMatch(original, pattern, replacement);
            }
            else
            {
                return ReplaceEx(original, pattern, replacement);
            }
        }
        /// <summary>
        /// 支持忽略大小写的替换文本功能
        /// </summary>
        /// <param name="original"></param>
        /// <param name="oldValue"></param>
        /// <param name="newValue"></param>
        /// <param name="IgnoreCase">是否忽略大小写</param>
        /// <returns></returns>
        static public string Replace(this string original, string oldValue, string newValue, bool IgnoreCase)
        {
            return Strings.Replace(original, oldValue, newValue, IgnoreCase);
        }
        /// <summary>
        /// 获取符合要求的第一个结果
        /// </summary>
        /// <param name="input"></param>
        /// <param name="value"></param>
        /// <param name="isRegex">是否是正则</param>
        /// <returns></returns>
        static public string GetMatchStr(this string input, string value, bool isRegex)
        {
            return Strings.GetMatchStr(input,value,isRegex);
        }
        /// <summary>
        /// 查找字符串,忽略大小写
        /// </summary>
        /// <param name="input"></param>
        /// <param name="value"></param>
        /// <param name="startindex"></param>
        /// <returns></returns>
        static public int IndexOfEx(this string input, string value, int startindex)
        {
            return input.IndexOf(value,startindex,StringComparison.OrdinalIgnoreCase);
        }
        /// <summary>
        /// 查找字符串,忽略大小写
        /// </summary>
        /// <param name="input"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        static public int IndexOfEx(this string input, string value)
        {
            return input.IndexOf(value, StringComparison.OrdinalIgnoreCase);
        }
        /// <summary>
        /// 判断字符串是否是数字或英文
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public bool IsEngOrNum(this string input)
        {
            return Strings.IsEngOrNum(input);
        }
        /// <summary>
        /// 判断字符串是否是英文
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public bool IsEng(this string input)
        {
            return Strings.IsEng(input);
        }
        /// <summary>
        /// 是否在指定范围内
        /// </summary>
        /// <param name="input"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        static public bool IsInRange(this decimal input, decimal min, decimal max)
        {
            return (input >= min && input <= max);
        }
        /// <summary>
        /// 是否在指定范围内
        /// </summary>
        /// <param name="input"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        static public bool IsInRange(this int input, int min, int max)
        {
            return (input >= min && input <= max);
        }
        /// <summary>
        /// 转换指定字符串为Int类型
        /// </summary>
        /// <param name="input"></param>
        /// <param name="minValue">最小值</param>
        /// <param name="maxValue">最大值</param>
        /// <param name="defValue">如果字符串不在范围内，则使用本默认值</param>
        /// <returns></returns>
        static public int ToInt(this object input, int minValue, int maxValue, int defValue)
        {
            try
            {
                int tmpI = Convert.ToInt32(input);
                if (tmpI < minValue || tmpI > maxValue)
                {
                    return defValue;
                }
                else
                {
                    return tmpI;
                }
            }
            catch { return defValue; }
        }
        /// <summary>
        /// 转换指定布尔类型到Int类型,true为1，false为0
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public int ToInt(this bool input)
        {
           return input?1:0;
        }
        /// <summary>
        /// 将指定类型转换成整型
        /// </summary>
        /// <param name="input"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        static public int ToInt(this object input, int defValue)
        {
            try
            {
                return Convert.ToInt32(input);
            }
            catch
            { return defValue; }
        }
        /// <summary>
        /// 将指定类型转换成整型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public int ToInt(this object input)
        {
            try
            {
                if(input is string)
                {
                    if(input.IsDouble())
                    {
                        return Convert.ToInt32(Math.Round(Convert.ToDouble(input)));
                    }
                }
                return Convert.ToInt32(input);
            }
            catch(Exception)
            { return 0; }
        }
        /// <summary>
        /// 将指定类型转换成长整型,如果时间时间类型,则转换为Unix时间戳
        /// </summary>
        /// <param name="input"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        static public Int64 ToInt64(this object input, Int64 defValue)
        {
            try
            {
                if (input == null) { return defValue; }
                if (input is DateTime time)
                {
                    return time.ToTimeStamp();
                }
                return Convert.ToInt64(input);
            }
            catch
            { return defValue; }
        }
        /// <summary>
        /// 转换为Unix时间戳
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public long ToInt64(this DateTime input)
        {
            return input.ToTimeStamp();
        }
        /// <summary>
        /// 将指定类型转换成长整型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public Int64 ToInt64(this object input)
        {
            return ToInt64(input,0);
        }
        /// <summary>
        /// 转换指定类型为Double类型
        /// </summary>
        /// <param name="input"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        static public double ToDouble(this object input, double defValue)
        {
            try
            {
                return Convert.ToDouble(input);
            }
            catch { return defValue; }
        }
        /// <summary>
        /// 转换指定类型为Double类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public double ToDouble(this object input)
        {
            try
            {
                return Convert.ToDouble(input);
            }
            catch { return 0d; }
        }
        /// <summary>
        /// 转换DateTime类型到日期时间字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public string ToDateTimeStr(this DateTime input)
        {
            return input.ToString("yyyy-MM-dd HH:mm:ss");
        }
        /// <summary>
        /// 转换DateTime类型到日期字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public string ToDateStr(this DateTime input)
        {
            return input.ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 转换DateTime类型到日期星期字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public string ToDateWeekStr(this DateTime input)
        {
            return input.ToString("yyyy-MM-dd dddd");
        }
        /// <summary>
        /// 转换DateTime类型到Unix时间戳
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public Int64 ToTimeStamp(this DateTime input)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            long timeStamp = (long)(input - startTime).TotalSeconds; // 相差秒数
            return timeStamp;
        }
        /// <summary>
        /// 转换Unix时间戳到DateTime类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public DateTime ToDateTime(this Int64 input)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
            return startTime.AddSeconds(input);
        }
        /// <summary>
        /// 判断字符串是否与内容匹配
        /// </summary>
        /// <param name="input"></param>
        /// <param name="likestr"></param>
        /// <returns></returns>
        static public bool IsMatchStr(this string input, string likestr)
        {
            return Strings.IsMatchStr(input,likestr);
        }
        /// <summary>
        /// 将变量值转换成Double类型,如果不在指定区域范围内，则使用默认值
        /// </summary>
        /// <param name="input"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        static public double ToDouble(this object input, double minValue, double maxValue, double defValue)
        {
            try
            {
                double tmpI = Convert.ToDouble(input);
                if (tmpI < minValue || tmpI > maxValue)
                {
                    return defValue;
                }
                else
                {
                    return tmpI;
                }
            }
            catch { return defValue; }
        }
        /// <summary>
        /// 转换到文件大小字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public string ToFileSizeStr(this Int64 input)
        {
          return RyFiles.GetFileSizeStr(input);
        }
        /// <summary>
        /// 转换到文件大小字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public string ToFileSizeStr(this UInt64 input)
        {
            return RyFiles.GetFileSizeStr(input.ToInt64(0));
        }
        /// <summary>
        /// 转换指定类型为Decimal类型
        /// </summary>
        /// <param name="input"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        static public decimal ToDecimal(this object input, decimal defValue)
        {
            try
            {
                return Convert.ToDecimal(input);
            }
            catch { return defValue; }
        }
        /// <summary>
        /// 将变量值转换成Decimal类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public decimal ToDecimal(this object input)
        {
            try
            {
                return Convert.ToDecimal(input);
            }
            catch { return 0; }
        }
        /// <summary>
        /// 将变量值转换成Decimal类型,如果不在指定区域范围内，则使用默认值
        /// </summary>
        /// <param name="input"></param>
        /// <param name="minValue"></param>
        /// <param name="maxValue"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        static public decimal ToDecimal(this object input, decimal minValue, decimal maxValue, decimal defValue)
        {
            try
            {
                decimal tmpI = Convert.ToDecimal(input);
                if (tmpI < minValue || tmpI > maxValue)
                {
                    return defValue;
                }
                else
                {
                    return tmpI;
                }
            }
            catch { return defValue; }
        }
        /// <summary>
        /// 转换指定类型为DateTime类型
        /// </summary>
        /// <param name="input"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        static public DateTime ToDateTime(this object input, DateTime defValue)
        {
            try
            {
                return Convert.ToDateTime(input);
            }
            catch { return defValue; }
        }
        /// <summary>
        /// 将变量值转换成DateTime类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public DateTime ToDateTime(this object input)
        {
            try
            {
                return Convert.ToDateTime(input);
            }
            catch { return Convert.ToDateTime("2000-1-1"); }
        }
        /// <summary>
        /// 转换指定类型为布尔类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public bool ToBool(this object input)
        {
            return (input.ToString() == "1" || input.ToString().ToLower() == "true" || input is true);
        }
        /// <summary>
        /// 追加字符
        /// </summary>
        /// <param name="input"></param>
        /// <param name="addstr"></param>
        /// <returns></returns>
        static public string AppendStr(this string input,string addstr)
        {
            if (input == "")
            {
                return addstr;
            }
            else
            {
                if (addstr == "")
                {
                    return input;
                }
                else
                {
                    return input + "\r\n" + addstr;
                }
            }
        }
        /// <summary>
        /// 获取指定字符串之间的内容
        /// </summary>
        /// <param name="input"></param>
        /// <param name="point1"></param>
        /// <param name="point2"></param>
        /// <returns></returns>
        static public string GetStr(this string input, string point1, string point2)
        {
            return GetStr(input, point1, point2, 0,out _, "");
        }
        /// <summary>
        /// 获取指定字符串之间的内容
        /// </summary>
        /// <param name="input"></param>
        /// <param name="point1"></param>
        /// <param name="point2"></param>
        /// <param name="iPos1"></param>
        /// <param name="endPos"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        static public string GetStr(this string input, string point1, string point2, int iPos1, out int endPos, string defValue)
        {
            endPos = -1;
            if (input == null) { return defValue; }
            int iIndex = iPos1;
            if (iPos1 < 0) { return defValue; }
            if (iPos1>input.Length-1) { return defValue; }
            if (point1 != "")
            { iIndex = input.IndexOf(point1, iPos1); }
            if (iIndex == -1) { return defValue; }
            int iIndex2;
            if (point2 == "") { iIndex2 = input.Length; }
            else { iIndex2 = input.IndexOf(point2, iIndex + point1.Length); }
            string tmpStr;
            if (iIndex < iIndex2 && iIndex >= 0)
            {
                tmpStr = input.Substring(iIndex + point1.Length, iIndex2 - iIndex - point1.Length);
                endPos = iIndex2 + point2.Length;
            }
            else
            {
                return defValue;
            }
            return tmpStr;
        }
        /// <summary>
        /// 判断指定字符串是否是Int类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public bool IsInt(this object input)
        {
            try
            {
                Convert.ToInt64(input);
                return input.ToString().IndexOf(".") < 0;
            }
            catch { return false; }
        }
        /// <summary>
        /// 判断指定字符串是否是Double类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public bool IsDouble(this object input)
        {
            try
            {
                Convert.ToDouble(input);
                return true;
            }
            catch { return false; }
        }
        /// <summary>
        /// 判断是否是布尔类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public bool IsBool(this object input)
        {
            try
            {
                Convert.ToBoolean(input);
                return true;
            }
            catch { return false; }
        }
        /// <summary>
        /// 将字符串转换为Base64类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public string ToBase64(this string input)
        {
            System.Text.Encoding encode = System.Text.Encoding.Default;
            byte[] bytedata = encode.GetBytes(input);
            return Convert.ToBase64String(bytedata, 0, bytedata.Length);
        }
        /// <summary>
        /// 判断字符串是否在指定长度
        /// </summary>
        /// <param name="input"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        static public bool InLength(this string input,int min,int max)
        {
            if (input.Length >= min && input.Length <= max)
            { return true; }
            else
                return false;
        }
        /// <summary>
        /// 判断能否转换成日期格式
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static public bool IsDateTime(this object input)
        {
            try
            {
                Convert.ToDateTime(input);
                return true;
            }
            catch { return false; }
        }
    }
}
