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

namespace Knoks.Framework.DataAccess
{
    public static class Helper
    {
        public static long? GetNullableInt64(object value)
        {
            return value == DBNull.Value ? (long?)null : Convert.ToInt64(value);
        }

        public static int? GetNullableInt32(object value)
        {
            return value == DBNull.Value ? (int?)null : Convert.ToInt32(value);
        }

        public static DateTime? GetNullableDateTime(object value)
        {
            return value == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(value);
        }

        public static short? GetNullableInt16(object value)
        {
            return value == DBNull.Value ? (short?)null : Convert.ToInt16(value);
        }

        public static string GetNullableString(object value)
        {
            return value == DBNull.Value ? null : Convert.ToString(value);
        }

        public static double? GetNullableDouble(object value)
        {
            return value == DBNull.Value ? (double?)null : Convert.ToDouble(value);
        }

        public static byte? GetNullableByte(object value)
        {
            return value == DBNull.Value ? (byte?)null : Convert.ToByte(value);
        }

        public static bool? GetNullableBoolean(object value)
        {
            return value == DBNull.Value ? (bool?)null : Convert.ToBoolean(value);
        }

        public static bool GetBoolean(object value)
        {
            return value != DBNull.Value && !value.ToString().Equals("0");
        }

        public static DateTime GetUtcDateTime(object value)
        {
            return DateTime.SpecifyKind(Convert.ToDateTime(value), DateTimeKind.Utc);
        }

        public static DateTime? GetNullableUtcDateTime(object value)
        {
            return value == DBNull.Value ? (DateTime?)null : GetUtcDateTime(value);
        }
        public static TimeSpan? GetNullableTimeSpan(object value)
        {
            return value == DBNull.Value ? (TimeSpan?)null : (TimeSpan)value;
        }

        public static T GetNullableEnum<T>(object value)
        {
            return value == DBNull.Value ? default(T) : (T)value;
        }

        public static Dictionary<string, object> ToSqlParamsDictionary(this IDictionary<string, object> rawDict)
        {
            if (rawDict == null) return null;
            return rawDict.ToDictionary(p => '@' + p.Key, p => (object)((p.Value != null && p.Value is string) ? ((string)p.Value).Trim() : p.Value));
        }

        //public static void ExecuteBullkCopy(string tableName, DataTable dt, string dbName = null)
        //{
        //    Database db = string.IsNullOrEmpty(dbName) ? DatabaseDao.CreateDatabase() : DatabaseDao.CreateDatabase(dbName);

        //    using (var connection = (SqlConnection)db.CreateConnection())
        //    {
        //        connection.Open();

        //        using (var bulkCopy = new SqlBulkCopy(connection))
        //        {
        //            bulkCopy.DestinationTableName = tableName;
        //            bulkCopy.WriteToServer(dt);
        //        }
        //    }
        //}
    }
}
