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 DateUtil
    {
        #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

        /// <summary>
        /// 获取时间
        /// </summary>
        /// <param name="timestamp">时间戳</param>
        /// <returns></returns>
        public static DateTime GetDateTime(long timestamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timestamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

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

        #region 比较两个时间大小
        /// <summary>
        /// 比较两个时间大小
        /// </summary>
        /// <param name="dt1">时间1</param>
        /// <param name="dt2">时间2</param>
        /// <returns>TRUE 时间1 > 时间2 ，FALSE 时间1< 时间2</returns>
        public static bool CompareDate(DateTime dt1, DateTime dt2)
        {
            return DateTime.Compare(dt1, dt2) > 0;
        }
        #endregion

        #region 获取两个时间差
        /// <summary>
        /// 获取两个时间差
        /// </summary>
        /// <param name="dt1">时间1</param>
        /// <param name="dt2">时间2</param>
        /// <param name="dateDiff">时间差的类型 枚举 EnumDateDiff</param>
        /// <returns>时间差值</returns>
        public static int GetDateDiff(DateTime dt1, DateTime dt2, EnumDateDiff dateDiff)
        {
            int diffValue = 0;
            TimeSpan ts1 = new TimeSpan(dt1.Ticks);
            TimeSpan ts2 = new TimeSpan(dt2.Ticks);
            TimeSpan diff = ts1.Subtract(ts2);
            switch (dateDiff)
            {
                case EnumDateDiff.秒: diffValue = Convert.ToInt32(diff.TotalSeconds); break;
                case EnumDateDiff.分钟: diffValue = Convert.ToInt32(diff.TotalMinutes); break;
                case EnumDateDiff.小时: diffValue = Convert.ToInt32(diff.TotalHours); break;
                case EnumDateDiff.天: diffValue = Convert.ToInt32(diff.TotalDays); break;
                default: break;
            }
            return diffValue;
        }
        #endregion

        #region 获取年龄

        public static string GetAge(DateTime dtBirthday, DateTime dtNow)
        {
            string strAge = string.Empty;                         // 年龄的字符串表示
            int intYear = 0;                                    // 岁
            int intMonth = 0;                                    // 月
            int intDay = 0;                                    // 天

            //// 如果没有设定出生日期, 返回空
            //if (DataType.DateTime_IsNull(ref dtBirthday) == true)
            //{
            //    return string.Empty;
            //}

            // 计算天数
            intDay = dtNow.Day - dtBirthday.Day;
            if (intDay < 0)
            {
                dtNow = dtNow.AddMonths(-1);
                intDay += DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
            }

            // 计算月数
            intMonth = dtNow.Month - dtBirthday.Month;
            if (intMonth < 0)
            {
                intMonth += 12;
                dtNow = dtNow.AddYears(-1);
            }

            // 计算年数
            intYear = dtNow.Year - dtBirthday.Year;

            // 格式化年龄输出
            if (intYear >= 1)                                            // 年份输出
            {
                strAge = intYear.ToString() + "岁";
            }

            if (intMonth > 0 && intYear <= 5)                           // 五岁以下可以输出月数
            {
                strAge += intMonth.ToString() + "月";
            }

            if (intDay >= 0 && intYear < 1)                              // 一岁以下可以输出天数
            {
                if (strAge.Length == 0 || intDay > 0)
                {
                    strAge += intDay.ToString() + "日";
                }
            }

            return strAge;
        }

        #endregion

        #region 计算两个日期之间相隔周数
        /// <summary>
        /// 计算两个日期之间相隔周数
        /// </summary>
        /// <param name="start">开始日期</param>
        /// <param name="end">结束日期</param>
        /// <returns></returns>
        public static int GetWeekSpace(DateTime start, DateTime end)
        {
            if (start > end) return -GetWeekSpace(end, start);
            else
            {
                int count = 0;
                DateTime p = start;
                while (p < end)
                {
                    if (p.DayOfWeek == DayOfWeek.Sunday) count++;
                    p = p.AddDays(1);
                }
                return count;
            }
        }
        #endregion

        /// <summary>
        /// 时间差类型枚举
        /// </summary>
        public enum EnumDateDiff
        {
            秒 = 1,
            分钟 = 2,
            小时 = 3,
            天 = 4
        }
    }
}
