using System;

namespace InkFx.Express.Utils
{
    internal partial class Tools
    {
        #region  数 据 转 换

        public static int ToFInt(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToInt(value);
        }
        public static double ToFDouble(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToDouble(value);
        }
        public static float ToFFloat(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToFloat(value);
        }
        public static string ToFString(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToString(value);
        }
        public static DateTime ToFDateTime(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToDateTime(value);
        }
        public static Guid ToFGuid(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToGuid(value);
        }
        public static byte ToFByte(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToByte(value);
        }
        public static bool ToFBoolean(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToBoolean(value);
        }
        public static long ToFLong(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToLong(value);
        }
        public static char ToFChar(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToChar(value);
        }
        public static decimal ToFDecimal(object obj, string property)
        {
            object value = GetValue(obj, property);
            return Tools.ToDecimal(value);
        }

        public static int ToFInt(object obj, string property, int @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToInt(value, @default);
        }
        public static double ToFDouble(object obj, string property, double @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToDouble(value, @default);
        }
        public static float ToFFloat(object obj, string property, float @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToFloat(value, @default);
        }
        public static string ToFString(object obj, string property, string @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToString(value, @default);
        }
        public static DateTime ToFDateTime(object obj, string property, DateTime @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToDateTime(value, @default);
        }
        public static Guid ToFGuid(object obj, string property, Guid @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToGuid(value, @default);
        }
        public static byte ToFByte(object obj, string property, byte @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToByte(value, @default);
        }
        public static bool ToFBoolean(object obj, string property, bool @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToBoolean(value, @default);
        }
        public static long ToFLong(object obj, string property, long @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToLong(value, @default);
        }
        public static char ToFChar(object obj, string property, char @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToChar(value, @default);
        }
        public static decimal ToFDecimal(object obj, string property, decimal @default)
        {
            object value = GetValue(obj, property);
            return Tools.ToDecimal(value, @default);
        }

        private static readonly DateTime m_DefaultTime = new DateTime(1900, 01, 01);
        public static int ToInt(object obj)
        {
            return ToInt(obj, 0);
        }
        public static double ToDouble(object obj)
        {
            return ToDouble(obj, 0);
        }
        public static float ToFloat(object obj)
        {
            return ToFloat(obj, 0);
        }
        public static string ToString(object obj)
        {
            return ToString(obj, string.Empty);
        }
        public static DateTime ToDateTime(object obj)
        {
            return ToDateTime(obj, m_DefaultTime);
        }
        public static Guid ToGuid(object obj)
        {
            return ToGuid(obj, Guid.Empty);
        }
        public static byte ToByte(object obj)
        {
            return ToByte(obj, 0);
        }
        public static bool ToBoolean(object obj)
        {
            return ToBoolean(obj, false);
        }
        public static long ToLong(object obj)
        {
            return ToLong(obj, 0);
        }
        public static char ToChar(object obj)
        {
            return ToChar(obj, char.MinValue);
        }
        public static decimal ToDecimal(object obj)
        {
            return ToDecimal(obj, 0);
        }

        public static int ToInt(object obj, int @default)
        {
            if (obj == null) return @default;
            if (obj is int) return (int)obj;
#if (!WindowsCE && !PocketPC)
            double temp;
            if (double.TryParse(obj.ToString(), out temp))
                return (int)temp;
            else
                return @default;
#endif
#if (WindowsCE || PocketPC)
            try { return (int)Convert.ToDouble(obj); }
            catch { return @default; }
#endif
        }
        public static double ToDouble(object obj, double @default)
        {
            if (obj == null) return @default;
            if (obj is double) return (double)obj;
#if (!WindowsCE && !PocketPC)
            double temp;
            if (double.TryParse(obj.ToString(), out temp))
                return temp;
            else return @default;
#endif
#if (WindowsCE || PocketPC)
            try { return Convert.ToDouble(obj); }
            catch { return @default; }
#endif
        }
        public static float ToFloat(object obj, float @default)
        {
            if (obj == null) return @default;
            if (obj is float) return (float)obj;
#if (!WindowsCE && !PocketPC)
            double temp;
            if (double.TryParse(obj.ToString(), out temp))
                return (float)temp;
            else return @default;
#endif
#if (WindowsCE || PocketPC)
            try { return (float)Convert.ToDouble(obj); }
            catch { return @default; }
#endif
        }
        public static string ToString(object obj, string @default)
        {
            if (obj == null) return @default;
            if (obj is string) return (string)obj;
            return obj.ToString();
        }
        public static DateTime ToDateTime(object obj, DateTime @default)
        {
            if (obj == null) return @default;
            if (obj is DateTime) return (DateTime)obj;
#if (!WindowsCE && !PocketPC)
            DateTime temp;
            if (DateTime.TryParse(obj.ToString(), out temp))
                return temp;
            else return @default;
#endif
#if (WindowsCE || PocketPC)
            try { return Convert.ToDateTime(obj); }
            catch { return @default; }
#endif
        }
        public static Guid ToGuid(object obj, Guid @default)
        {
            if (obj == null) return @default;
            if (obj is Guid) return (Guid)obj;
            try { return new Guid(obj.ToString()); }
            catch { return @default; }
        }
        public static byte ToByte(object obj, byte @default)
        {
            if (obj == null) return @default;
            if (obj is byte) return (byte)obj;
#if (!WindowsCE && !PocketPC)
            byte temp;
            if (byte.TryParse(obj.ToString(), out temp))
                return temp;
            else return @default;
#endif
#if (WindowsCE || PocketPC)
            try { return Convert.ToByte(obj); }
            catch { return @default; }
#endif
        }
        public static bool ToBoolean(object obj, bool @default)
        {
            if (obj == null) return @default;
            if (obj is bool) return (bool)obj;
            if (obj is short) return (short)obj > 0;
            if (obj is int) return (int)obj > 0;
            if (obj is long) return (long)obj > 0;
            if (obj is double) return (double)obj > 0;
            if (obj is float) return (float)obj > 0;
            if (obj is decimal) return (decimal)obj > 0;
            if (string.Equals(obj.ToString(), "T", StringComparison.CurrentCultureIgnoreCase)) return true;

#if (!WindowsCE && !PocketPC)
            bool temp;
            if (bool.TryParse(obj.ToString(), out temp)) return temp;
#endif
#if (WindowsCE || PocketPC)
            try { return Convert.ToBoolean(obj); }
            catch { }
#endif

            double number = ToDouble(obj, double.MinValue);
            if (number > 0) return true;

            return false;
        }
        public static long ToLong(object obj, long @default)
        {
            if (obj == null) return @default;
            if (obj is long) return (long)obj;

#if (!WindowsCE && !PocketPC)
            double temp;
            if (double.TryParse(obj.ToString(), out temp))
                return (long)temp;
            else return @default;
#endif
#if (WindowsCE || PocketPC)
            try { return (long)Convert.ToDouble(obj); }
            catch { return @default; }
#endif
        }
        public static char ToChar(object obj, char @default)
        {
            if (obj == null) return @default;
            if (obj is char) return (char)obj;
#if (!WindowsCE && !PocketPC)
            char temp;
            if (char.TryParse(obj.ToString(), out temp))
                return temp;
            else return @default;
#endif
#if (WindowsCE || PocketPC)
            try { return Convert.ToChar(obj); }
            catch { return @default; }
#endif
        }
        public static decimal ToDecimal(object obj, decimal @default)
        {
            if (obj == null) return @default;
            if (obj is decimal) return (decimal)obj;
#if (!WindowsCE && !PocketPC)
            decimal temp;
            if (decimal.TryParse(obj.ToString(), out temp))
                return temp;
            else return @default;
#endif
#if (WindowsCE || PocketPC)
            try { return Convert.ToDecimal(obj); }
            catch { return @default; }
#endif
        }



        #endregion
    }
}
