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

namespace PushPlatform.Common.Util
{
    /// <summary>
    /// 时间辅助类
    /// </summary>
    public static class DateTimeHelper
    {
        #region 时间格式化

        public static string ToDateString(this DateTime time)
        {
            return time.ToString("yyyy-MM-dd");
        }

        public static string ToDateTimeString(this DateTime time)
        {
            return time.ToString("yyyy-MM-dd hh:mm:ss");
        }

        public static string ToTimeString(this DateTime time)
        {
            return time.ToString("hh:mm:ss");
        }

        #endregion

        #region 修正错误的时间字符串
        /// <summary>
        /// 修正错误的时间字符串
        /// </summary>
        /// <param name="timestr">传入的错误时间字符串</param>
        /// <returns></returns>
        public static string ToDateString(string timestr)
        {
            DateTime date = DateTime.Now;
            if (DateTime.TryParse(timestr, out date))
            {
                return date.ToString("yyyy-MM-dd");
            }
            return timestr;
        }
        #endregion

        #region 获得时间戳
        /// <summary>
        /// 获得时间戳，datetime扩展方法
        /// </summary>
        /// <param name="datetime">时间值</param>
        /// <returns>时间戳</returns>
        public static long GetTimestamp(this DateTime datetime)
        {
            return (datetime.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks) / 10000000;
        }

        /// <summary>
        /// 获得当前时间戳
        /// </summary>
        /// <returns>当前时间戳</returns>
        public static long GetCurrentTimestamp()
        {
            return (DateTime.UtcNow.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks) / 10000000;
            //return (DateTime.UtcNow.Ticks - DateTime.MinValue.Ticks) / 10000000;
        }
        #endregion

        #region 比较两个时间大小，datetime扩展方法
        /// <summary>
        /// 比较两个时间大小，datetime扩展方法
        /// </summary>
        /// <param name="datetime1">要比较的值</param>
        /// <param name="datetime2">被比较的值</param>
        /// <returns>返回true表示大于，返回false表示小于</returns>
        public static bool Compare(this DateTime datetime1, DateTime datetime2)
        {
            return DateTime.Compare(datetime1, datetime2) > 0;
        }
        #endregion

        #region 获取两个时间的差,datetime扩展方法
        /// <summary>
        /// 获取两个时间的差,datetime扩展方法
        /// </summary>
        /// <param name="datetime1">要比较的时间</param>
        /// <param name="datetime2">被比较的时间</param>
        /// <param name="diffType">获得比较值的枚举类型</param>
        /// <returns>相差值</returns>
        public static Double GetDateTimeDiff(this DateTime datetime1, DateTime datetime2, EnumDateTimeDiff diffType)
        {
            TimeSpan ts1 = new TimeSpan(datetime1.Ticks);
            TimeSpan ts2 = new TimeSpan(datetime2.Ticks);
            TimeSpan diff = ts1.Subtract(ts2);
            switch (diffType)
            {
                case EnumDateTimeDiff.Seconds: return diff.TotalSeconds;
                case EnumDateTimeDiff.Minutes: return diff.TotalMinutes;
                case EnumDateTimeDiff.Hours: return diff.TotalHours;
                case EnumDateTimeDiff.Days: return diff.TotalDays;
                case EnumDateTimeDiff.Weeks: return Math.Round(diff.TotalDays / 7, 0);
                case EnumDateTimeDiff.Months: return Math.Round(diff.TotalDays / 30, 0);
                case EnumDateTimeDiff.Years: return Math.Round(diff.TotalDays / 365, 0);
                default: return 0;
            }
        }

        /// <summary>
        /// 获得比较值的枚举类型
        /// </summary>
        public enum EnumDateTimeDiff
        {
            Seconds = 1,
            Minutes = 2,
            Hours = 3,
            Days = 4,
            Weeks = 5,
            Months = 6,
            Years = 7
        }
        #endregion

        #region 根据身份证获取出生年月
        public static DateTime? GetBirthDayById_Card(string id_card)
        {
            string Sub_str = id_card.Substring(6, 8).Insert(4, "-").Insert(7, "-");
            DateTime return_date = new DateTime();

            if (DateTime.TryParse(Sub_str, out return_date))
                return return_date;
            else

                return null;
        }
        #endregion


        #region 根据随访配置ID，计算下次随访时间
        /// <summary>
        /// 根据随访配置ID，计算下次随访时间
        /// </summary>
        /// <param name="configId">配置ID</param>
        /// <param name="recentVisitTime">最近一次随访时间</param>
        /// <returns></returns>
        public static DateTime GetNextVisitTime(string configId, DateTime? recentVisitTime)
        {
            switch (configId)
            {
                //case "b604dcb4-1c20-4346-89dc-6761db00faa9":
                //    return recentVisitTime.GetValueOrDefault().AddMonths(0);
                //case "84d70937-edd9-4758-ac6a-cad7cf8b73f5":
                //    return recentVisitTime.GetValueOrDefault().AddMonths(0);
                //case "6c6af0d9-a017-4200-84fd-182c1933dfd0":
                //    return recentVisitTime.GetValueOrDefault().AddMonths(3);
                //case "2157601f-f68f-469f-9fab-603cd76d9ce9":
                //    return recentVisitTime.GetValueOrDefault().AddMonths(6);
                default:
                    return recentVisitTime.GetValueOrDefault().AddMonths(6);
            }

        }
        #endregion
    }
}
