using System;

namespace Nirvana.Common
{
    public static partial class Ext
    {
        #region 数值转换

        /// <summary>
        /// 转换为整型
        /// </summary>
        /// <param name="data">数据</param>
        public static int ToInt(this object data)
        {
            if (data == null)
                return 0;
            int result;
            var success = int.TryParse(data.ToString(), out result);
            if (success)
                return result;
            try
            {
                return Convert.ToInt32(ToDouble(data, 0));
            }
            catch (Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// 转换成长整形
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static long ToLong(this object data)
        {
            if (data == null)
                return 0;
            long result;
            return long.TryParse(data.ToString(), out result) ? result : 0;
        }

        /// <summary>
        /// 转换为可空整型
        /// </summary>
        /// <param name="data">数据</param>
        public static int? ToIntOrNull(this object data)
        {
            if (data == null)
                return null;
            int result;
            bool isValid = int.TryParse(data.ToString(), out result);
            if (isValid)
                return result;
            return null;
        }

        /// <summary>
        /// 转换为双精度浮点数
        /// </summary>
        /// <param name="data">数据</param>
        public static double ToDouble(this object data)
        {
            if (data == null)
                return 0;
            double result;
            return double.TryParse(data.ToString(), out result) ? result : 0;
        }

        /// <summary>
        /// 转换为float
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static float ToFloat(this object data)
        {
            if (data == null)
                return 0;
            float result;
            return float.TryParse(data.ToString(), out result) ? result : 0;
        }

        /// <summary>
        /// 转换为双精度浮点数,并按指定的小数位4舍5入
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="digits">小数位数</param>
        public static double ToDouble(this object data, int digits)
        {
            var result = ToDouble(data);
            string numToString = result.ToString();
            int index = numToString.IndexOf(".");
            int length = numToString.Length;
            if (index != -1)
            {
                numToString = string.Format("{0}.{1}",
                    numToString.Substring(0, index),
                    numToString.Substring(index + 1, Math.Min(length - index - 1, digits)));
            }
            return ToDouble(numToString);
        }

        /// <summary>
        /// 转换为可空双精度浮点数
        /// </summary>
        /// <param name="data">数据</param>
        public static double? ToDoubleOrNull(this object data)
        {
            if (data == null)
                return null;
            double result;
            bool isValid = double.TryParse(data.ToString(), out result);
            if (isValid)
                return result;
            return null;
        }

        /// <summary>
        /// 转换为高精度浮点数
        /// </summary>
        /// <param name="data">数据</param>
        public static decimal ToDecimal(this object data)
        {
            if (data == null)
                return 0;
            decimal result;
            return decimal.TryParse(data.ToString(), out result) ? result : 0;
        }

        /// <summary>
        /// 转换为高精度浮点数,并按指定的小数位4舍5入
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="digits">小数位数</param>
        public static decimal ToDecimal(this object data, int digits)
        {
            var result = ToDecimal(data);
            string numToString = result.ToString();
            int index = numToString.IndexOf(".");
            int length = numToString.Length;
            if (index != -1)
            {
                numToString = string.Format("{0}.{1}",
                    numToString.Substring(0, index),
                    numToString.Substring(index + 1, Math.Min(length - index - 1, digits)));
            }
            return ToDecimal(numToString);
        }

        /// <summary>
        /// 转换为可空高精度浮点数
        /// </summary>
        /// <param name="data">数据</param>
        public static decimal? ToDecimalOrNull(this object data)
        {
            if (data == null)
                return null;
            decimal result;
            bool isValid = decimal.TryParse(data.ToString(), out result);
            if (isValid)
                return result;
            return null;
        }

        /// <summary>
        /// 转换为可空高精度浮点数,并按指定的小数位4舍5入
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="digits">小数位数</param>
        public static decimal? ToDecimalOrNull(this object data, int digits)
        {
            var result = ToDecimalOrNull(data);
            if (result == null)
                return null;
            return Math.Round(result.Value, digits);
        }

        #endregion 数值转换

        #region 日期转换

        /// <summary>
        /// 转换为日期
        /// </summary>
        /// <param name="data">数据</param>
        public static DateTime ToDate(this object data)
        {
            if (data == null)
                return DateTime.MinValue;
            DateTime result;
            return DateTime.TryParse(data.ToString(), out result) ? result : DateTime.MinValue;
        }

        /// <summary>
        /// 转换为可空日期
        /// </summary>
        /// <param name="data">数据</param>
        public static DateTime? ToDateOrNull(this object data)
        {
            if (data == null)
                return null;
            DateTime result;
            bool isValid = DateTime.TryParse(data.ToString(), out result);
            if (isValid)
                return result;
            return null;
        }

        /// <summary>
        /// 时间格式化,带时分,如果同一年则不显示年,否则显示年份
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ToYearDateTime(this object data)
        {
            if (data == null)
                return string.Empty;
            DateTime result;
            bool isValid = DateTime.TryParse(data.ToString(), out result);
            if (!isValid)
                return string.Empty;
            if (result.Year == DateTime.Now.Year)
                return result.ToString("M-d HH:mm");
            return result.ToString("yyyy-M-d HH:mm");
        }

        /// <summary>
        /// 时间格式化,不带时分,如果同一年则不显示年,否则显示年份
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ToYearDate(this object data)
        {
            if (data == null)
                return string.Empty;
            DateTime result;
            bool isValid = DateTime.TryParse(data.ToString(), out result);
            if (!isValid)
                return string.Empty;
            if (result.Year == DateTime.Now.Year)
                return result.ToString("M-d");
            return result.ToString("yyyy-M-d");
        }

        #endregion 日期转换

        #region 布尔转换

        /// <summary>
        /// 转换为布尔值
        /// </summary>
        /// <param name="data">数据</param>
        public static bool ToBool(this object data)
        {
            if (data == null)
                return false;
            bool? value = GetBool(data);
            if (value != null)
                return value.Value;
            bool result;
            return bool.TryParse(data.ToString(), out result) && result;
        }

        /// <summary>
        /// 获取布尔值
        /// </summary>
        private static bool? GetBool(this object data)
        {
            switch (data.ToString().Trim().ToLower())
            {
                case "0":
                    return false;

                case "1":
                    return true;

                case "是":
                    return true;

                case "否":
                    return false;

                case "yes":
                    return true;

                case "no":
                    return false;

                default:
                    return null;
            }
        }

        /// <summary>
        /// 转换为可空布尔值
        /// </summary>
        /// <param name="data">数据</param>
        public static bool? ToBoolOrNull(this object data)
        {
            if (data == null)
                return null;
            bool? value = GetBool(data);
            if (value != null)
                return value.Value;
            bool result;
            bool isValid = bool.TryParse(data.ToString(), out result);
            if (isValid)
                return result;
            return null;
        }

        #endregion 布尔转换

        #region Guid转换

        /// <summary>
        /// 转换为Guid
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Guid ToGuid(this object data)
        {
            if (data == null)
            {
                return Guid.Empty;
            }
            Guid result;
            bool isValid = Guid.TryParse(data.ToString(), out result);
            if (isValid)
                return result;
            return Guid.Empty;
        }

        /// <summary>
        /// 转换为Guid
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Guid ToGuid(this Guid? data)
        {
            if (data == null)
            {
                return Guid.Empty;
            }
            return data.Value;
        }

        #endregion Guid转换

        #region 字符串转换

        /// <summary>
        /// 转换为字符串
        /// </summary>
        /// <param name="data">数据</param>
        public static string ToString(this object data)
        {
            return data == null ? string.Empty : data.ToString().Trim();
        }

        /// <summary>
        /// 针对可能为空的字符串处理
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ToStr(this string data)
        {
            return string.IsNullOrEmpty(data) ? string.Empty : data.Replace(" ", "");
        }

        public static string ToStrEmpty(this string data)
        {
            return string.IsNullOrEmpty(data) ? string.Empty : data.Trim();
        }

        #endregion 字符串转换

        /// <summary>
        /// 安全返回值
        /// </summary>
        /// <param name="value">可空值</param>
        public static T SafeValue<T>(this T? value) where T : struct
        {
            return value ?? default(T);
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="value">值</param>
        public static bool IsEmpty(this string value)
        {
            return string.IsNullOrWhiteSpace(value);
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="value">值</param>
        public static bool IsEmpty(this Guid? value)
        {
            if (value == null)
                return true;
            return IsEmpty(value.Value);
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="value">值</param>
        public static bool IsEmpty(this Guid value)
        {
            if (value == Guid.Empty)
                return true;
            return false;
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <param name="value">值</param>
        public static bool IsEmpty(this object value)
        {
            if (value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}