using System.Globalization;
using System;

namespace JiaZhiQuan.Common.Utils
{
    public static class DateTimeUtils
    {
        /// <summary>
        /// 获取当前日期是第几周，周的起始日期和结束日期
        /// </summary>
        /// <param name="tmpDate"></param>
        /// <returns>(当前周是今年的第几周，周的开始日期，周的结束日期)</returns>
        public static (int, DateTime, DateTime) GetDateWeekRange(DateTime tmpDate)
        {
            var startDateStr = tmpDate.Date.AddDays(-(int)tmpDate.DayOfWeek);//当前周的开始日期
            var endDateStr = tmpDate.Date.AddDays(6 - (int)tmpDate.DayOfWeek);//当前周的结束日期   
            GregorianCalendar gc = new GregorianCalendar();
            // 完整周计算，周的开始是周日
            int weekOfYear = gc.GetWeekOfYear(tmpDate, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday);
            return (weekOfYear, startDateStr, endDateStr);
        }

        /// <summary>
        /// 根据小时开始，返回小时范围
        /// case：2020/01/01 23:00～2020/01/07 00:00
        /// </summary>
        /// <param name="startWeekDate"></param>
        /// <returns></returns>
        public static string GetHourRangeStr(DateTime start, string dateFormat)
        {
            return $"{start.ToString(dateFormat)}～{start.AddHours(1).ToString(dateFormat)}";
        }

        /// <summary>
        /// 根据周开始天，返回周范围
        /// case：2020/01/01～2020/01/07
        /// </summary>
        /// <param name="startWeekDate"></param>
        /// <returns></returns>
        public static string GetWeekRangeStr(string startWeekDate, string dateFormat)
        {
            return $"{startWeekDate}～{DateTime.Parse(startWeekDate).AddDays(6).ToString(dateFormat)}";
        }

        /// <summary>
        /// 根据月开始天，返回月范围
        /// case：2020/01～2020/02
        /// </summary>
        /// <param name="startWeekDate"></param>
        /// <returns></returns>
        public static string GetMonthRangeStr(string startMonthDate)
        {
            var start = DateTime.Parse(startMonthDate);
            return $"{start.ToString("yyyy/MM")}～{start.AddMonths(1).ToString("yyyy/MM")}";
        }

        /// <summary>
        /// 计算两个时间相差几周
        /// </summary>
        /// <param name="startWeekDate"></param>
        /// <returns></returns>
        public static int TwoDateDiffWeeks(DateTime time1, DateTime time2)
        {
            var week1 = GetDateWeekRange(time1).Item2;
            var week2 = GetDateWeekRange(time2).Item2;
            return (int)Math.Floor((week2 - week1).TotalDays / 7.0);
        }

        /// <summary>
        /// 计算两个时间相差几月
        /// </summary>
        /// <param name="startWeekDate"></param>
        /// <returns></returns>
        public static int TwoDateDiffMonths(string time1, string time2)
        {
            var date1 = DateTime.Parse(time1);
            var date2 = DateTime.Parse(time2);
            return (date2.Year - date1.Year) * 12 + (date2.Month - date1.Month);
        }

        /// <summary>
        /// 获取今天是这周的第几天
        /// </summary>
        /// <returns></returns>
        public static int GetDayOfWeek()
        {
            DateTime today = DateTime.Now;
            // 根据国际周的计算方式调整
            int dayOfWeek = ((int)today.DayOfWeek + 6) % 7 + 1;
            return dayOfWeek;
        }
    }
}
