using System;

namespace QYZH.InteractiveMagazine.Common.Extensions
{
    /// <summary>
    /// 日期时间扩展方法
    /// </summary>
    public static class DateTimeExtension
    {
        /// <summary>
        /// 时间戳起始日期
        /// </summary>
        public static readonly DateTime TimestampStart = new(1970, 1, 1, 0, 0, 0, 0);
        /// <summary>
        /// 转换为Unix时间戳(秒)
        /// </summary>
        /// <param name="dateTime">日期时间</param>
        /// <returns>Unix时间戳</returns>
        public static long ToTimestamp(this DateTime dateTime)
        {
            return new DateTimeOffset(dateTime.ToUniversalTime()).ToUnixTimeSeconds();
        }

        /// <summary>
        /// 从Unix时间戳(秒)转换为DateTime
        /// </summary>
        /// <param name="timestamp">Unix时间戳</param>
        /// <returns>日期时间</returns>
        public static DateTime FromTimestamp(long timestamp)
        {
            return DateTimeOffset.FromUnixTimeSeconds(timestamp).LocalDateTime;
        }

        /// <summary>
        /// 转换为指定格式的日期时间字符串
        /// </summary>
        /// <param name="dateTime">日期时间</param>
        /// <param name="format">格式字符串,默认为"yyyy-MM-dd HH:mm:ss"</param>
        /// <returns>格式化的日期时间字符串</returns>
        public static string ToDateTimeString(this DateTime dateTime, string format = "yyyy-MM-dd HH:mm:ss")
        {
            return dateTime.ToString(format);
        }

        /// <summary>
        /// 从生日计算年龄
        /// </summary>
        /// <param name="birthday">生日日期</param>
        /// <returns>年龄</returns>
        public static int GetAge(this DateTime birthday)
        {
            int age = DateTime.Now.Year - birthday.Year;

            if (DateTime.Now.DayOfYear < birthday.DayOfYear)
            {
                age--;
            }

            return age;
        }

        /// <summary>
        /// 判断是否是今天
        /// </summary>
        /// <param name="dateTime">日期时间</param>
        /// <returns>是否是今天</returns>
        public static bool IsToday(this DateTime dateTime)
        {
            return dateTime.Date == DateTime.Today;
        }

        /// <summary>
        /// 获取当天开始时间(00:00:00)
        /// </summary>
        /// <param name="dateTime">日期时间</param>
        /// <returns>当天开始时间</returns>
        public static DateTime ToStartOfDay(this DateTime dateTime)
        {
            return dateTime.Date;
        }

        /// <summary>
        /// 获取当天结束时间(23:59:59.999)
        /// </summary>
        /// <param name="dateTime">日期时间</param>
        /// <returns>当天结束时间</returns>
        public static DateTime ToEndOfDay(this DateTime dateTime)
        {
            return dateTime.Date.AddDays(1).AddTicks(-1);
        }

        public static long ToTimestamp(this DateTime dateTime, bool milliseconds = false)
        {
            var timestamp = dateTime.ToUniversalTime() - TimestampStart;
            return (long)(milliseconds ? timestamp.TotalMilliseconds : timestamp.TotalSeconds);
        }
        public static long ToMillisecondsTimestamp(this DateTime dateTime)
        {
            return ToTimestamp(dateTime, true);
        }
    }
}
