namespace Atomx.Utils.Extension
{
    public static class DateTimeExtension
    {
        /// <summary>
        /// 时间距离现在多久
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string DateFromNow(this DateTime time)
        {
            if (time == DateTime.MinValue)
            {
                return string.Empty;
            }
            TimeSpan timeSpan = DateTime.Now - time;
            if (Math.Floor(timeSpan.TotalDays) > 365)
            {
                return Math.Floor(timeSpan.TotalDays) / 365 + "年前";
            }
            else if (Math.Floor(timeSpan.TotalDays) > 30)
            {
                return Math.Floor(timeSpan.TotalDays) / 30 + "月前";
            }
            else if (Math.Floor(timeSpan.TotalDays) > 7)
            {
                return Math.Floor(timeSpan.TotalDays) / 7 + "周前";
            }
            else if (Math.Floor(timeSpan.TotalDays) > 1)
            {
                return Math.Floor(timeSpan.TotalDays) + "天前";
            }
            else if (Math.Floor(timeSpan.TotalHours) > 1)
            {
                return Math.Floor(timeSpan.TotalHours) + "小时前";
            }
            else if (Math.Floor(timeSpan.TotalMinutes) > 1)
            {
                return Math.Floor(timeSpan.TotalMinutes) + "分钟前";
            }
            else
            {
                return Math.Floor(timeSpan.TotalSeconds) + "秒前";
            }
        }

        /// <summary>
        /// 字符串时间转换DateTime
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string text)
        {
            DateTime.TryParse(text, out DateTime time);
            return time;
        }

        /// <summary>
        /// 字符串时间转换DateTime
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static DateTime ToUtcDateTime(this string text)
        {
            var state = DateTime.TryParse(text, out DateTime time);
            if (state)
            {
                return DateTime.SpecifyKind(time, DateTimeKind.Utc);
            }
            return DateTime.MinValue;

        }

        /// <summary>
        /// 获取当月第一天0时0分0秒
        /// </summary>
        /// <returns></returns>
        public static DateTime GetMonthStartDate()
        {
            return DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;
        }

        /// <summary>
        /// 获取当月最后一天23时59分59秒
        /// </summary>
        /// <returns></returns>
        public static DateTime GetMonthEndDate()
        {
            return DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1);
        }

        /// <summary>
        /// 获取当前时间的周一开始时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetWeekStartDate()
        {
            DateTime today = DateTime.Today;
            return today.AddDays(-(int)today.DayOfWeek + (int)DayOfWeek.Monday).Date;
        }

        /// <summary>
        /// 获取当前时间所在的周的最后一天结束时间
        /// </summary>
        /// <returns></returns>
        public static DateTime GetWeekEndDate()
        {
            DateTime today = DateTime.Today;
            return today.AddDays(-(int)today.DayOfWeek + (int)DayOfWeek.Monday).AddDays(7).AddSeconds(-1);
        }

        /// <summary>
        /// 转换成每日开始时间
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime ConvertStartDateTime(this DateTime time)
        {
            return time.Date;
        }

        /// <summary>
        /// 转换成每日开始UTC时间
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime ConvertStartDateUTCTime(this DateTime time)
        {
            return DateTime.SpecifyKind(time.Date, DateTimeKind.Utc);
        }


        /// <summary>
        /// 转换成每日结束时间
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime ConvertEndDateTime(this DateTime time)
        {
            return time.Date.AddDays(1).AddSeconds(-1);
        }

        /// <summary>
        /// 转换成每日结束UTC时间
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static DateTime ConvertEndDateUTCTime(this DateTime time)
        {
            return DateTime.SpecifyKind(time.Date.AddDays(1).AddSeconds(-1), DateTimeKind.Utc);
        }

        /// <summary>
        /// long --> DateTime
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static DateTime ConvertLongToDateTime(this long d)
        {
            DateTime dtStart = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1));
            long lTime = long.Parse(d + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow);
            return dtResult;
        }

        public static DateTime NumberToDateTime(this string number)
        {
            var year = number.Substring(0, 4);
            //Console.WriteLine($"年：{year}");
            var month = number.Substring(4, 2);
            //Console.WriteLine($"月：{month}");
            var day = number.Substring(6, 2);
            //Console.WriteLine($"日：{day}");
            var hour = number.Substring(8, 2);
            //Console.WriteLine($"时：{hour}");
            var minute = number.Substring(10, 2);
            //Console.WriteLine($"分：{minute}");
            var second = number.Substring(12);
            //Console.WriteLine($"秒：{second}");

            var datetime = $"{year}-{month}-{day} {hour}:{minute}:{second}";

            DateTime date = DateTime.Parse(datetime);
            return date;
        }
    }
}
