using Hotline.Share.Dtos.CallCenter;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.Order;
using Novacode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Hotline.Share.Tools;
public static class DateTimeExtensions
{
    /// <summary>
    /// 获取两个时间之间的差
    /// </summary>
    /// <param name="beginDateTime">开始时间</param>
    /// <param name="endDateTime">结束时间</param>
    /// <returns>相距秒数</returns>
    public static int GetDifferenceSeconds(this DateTime beginDateTime, DateTime endDateTime)
    {
        return (int)(beginDateTime - endDateTime).TotalSeconds;
    }

    public static (DateTime, DateTime) DayStartAndEndTime(this DateTime dateTime)
    {
        var start = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);
        var end = start.AddDays(1).AddTicks(-1);
        return (start, end);
    }

    public static string ToLongDateTimeString(this DateTime dateTime)
    { 
        return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
    }

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

    public static long ToUnixTimeMilliseconds(this DateTime value)
    {
        return new DateTimeOffset(value).ToUnixTimeMilliseconds();
    }

    public static EExpiredStatus? CalculateExpiredState(this DateTime? filedTime, EOrderStatus status, DateTime? expiredTime, DateTime? nearlyExpiredTime, DateTime? nearlyExpiredTimeOne)
    {
        DateTime? dateTime = DateTime.Now;
        if (status >= EOrderStatus.Filed)
        {
            dateTime = filedTime;
        }

        if (expiredTime.HasValue)
        {
            if (nearlyExpiredTimeOne.HasValue && dateTime < nearlyExpiredTimeOne)
            {
                return EExpiredStatus.Normal;
            }
            else if (!nearlyExpiredTimeOne.HasValue && dateTime < nearlyExpiredTime)
            {
                return EExpiredStatus.Normal;
            }
            else if (nearlyExpiredTimeOne.HasValue && dateTime > nearlyExpiredTimeOne && dateTime < nearlyExpiredTime)
            {
                return EExpiredStatus.GoingToExpiredOne;
            }
            else if (dateTime > nearlyExpiredTime && dateTime < expiredTime)
            {
                return EExpiredStatus.GoingToExpired;
            }
            else
            {
                return EExpiredStatus.Expired;
            }
        }

        return null;
    }


    /// <summary>
    /// 获取本周的开始时间（周一 00:00:00）和结束时间（周日 23:59:59）
    /// </summary>
    /// <param name="currentDate">当前时间</param>
    /// <returns>本周的开始时间和结束时间</returns>
    public static (DateTime StartOfWeek, DateTime EndOfWeek) GetWeekRange(this DateTime currentDate)
    {
        // 获取当前是本周的第几天（周一是1，周日是7）
        int diff = (int)currentDate.DayOfWeek - (int)DayOfWeek.Monday;

        // 如果当前是星期天（DayOfWeek.Sunday），调整 diff
        if (diff < 0)
            diff += 7;

        // 计算本周周一 00:00:00
        DateTime startOfWeek = currentDate.Date.AddDays(-diff);

        // 计算本周周日 23:59:59
        DateTime endOfWeek = startOfWeek.AddDays(6).AddHours(23).AddMinutes(59).AddSeconds(59);

        return (startOfWeek, endOfWeek);
    }

    /// <summary>
    /// 获取当前年的开始时间（1月1日 00:00:00）和结束时间（12月31日 23:59:59）
    /// </summary>
    /// <param name="currentDate">当前时间</param>
    /// <returns>当前年的开始时间和结束时间</returns>
    public static (DateTime StartOfYear, DateTime EndOfYear) GetYearRange(this DateTime currentDate)
    {
        // 当年第一天 00:00:00
        DateTime startOfYear = new DateTime(currentDate.Year, 1, 1);

        // 当年最后一天 23:59:59
        DateTime endOfYear = new DateTime(currentDate.Year, 12, 31, 23, 59, 59);

        return (startOfYear, endOfYear);
    }

    /// <summary>
    /// 获取当前月份的开始时间（1号 00:00:00）和结束时间（最后一天 23:59:59）
    /// </summary>
    /// <param name="currentDate">当前时间</param>
    /// <returns>当前月的开始时间和结束时间</returns>
    public static (DateTime StartOfMonth, DateTime EndOfMonth) GetMonthRange(this DateTime currentDate)
    {
        // 当月第一天 00:00:00
        DateTime startOfMonth = new DateTime(currentDate.Year, currentDate.Month, 1);

        // 当月最后一天 23:59:59
        DateTime endOfMonth = startOfMonth.AddMonths(1).AddDays(-1).AddHours(23).AddMinutes(59).AddSeconds(59);

        return (startOfMonth, endOfMonth);
    }

}
