using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fuel.Infrastructure.Extension
{
    public static class TimeExtension
    {
        /// <summary>
        /// 返回 yyyy-dd-mm hh:mm:ss
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToDateTimeString(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 返回 yyyy-dd-mm hh:mm:ss
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string ToDateTimeString(this DateTime? dateTime)
        {

            if (dateTime == null)
            {
                return "";
            }
            return ((DateTime)dateTime).ToString("yyyy-MM-dd HH:mm:ss");

        }

        /// <summary>
        /// 获取本地时间的世界时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetNowTimeStamp(bool isInMillisecond = false)
        {
            int unit = isInMillisecond ? 10000 : 10000000;
            var TimeStamps = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / unit;
            return TimeStamps;
        }


        /// <summary>
        /// 将时间转换为时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static long ToTimeStamp(DateTime dateTime, bool isInMillisecond = false)
        {
            int unit = isInMillisecond ? 10000 : 10000000;
            var TimeStamps = (dateTime.Ticks - 621355968000000000) / unit;
            return TimeStamps;
        }

        /// <summary>
        /// 获取本地时间的世界时间的偏移量
        /// </summary>
        /// <returns></returns>
        public static long GetUtcOffsets()
        {
            var UtcOffsets = TimeZone.CurrentTimeZone.GetUtcOffset(new DateTime()).Ticks / 10000;
            return UtcOffsets;
        }

        /// <summary>
        /// 将时间戳转换为时间
        /// </summary>
        /// <returns></returns>
        public static DateTime ToDateTime(this long TimeStamps, bool millisecond = true)
        {
            if (millisecond)
            {
                return new DateTime(1970, 1, 1).AddMilliseconds(TimeStamps);
            }
            else
            {
                return new DateTime(1970, 1, 1).AddSeconds(TimeStamps);
            }
        }

        public static DateTime ToDateTime(long TimeStamps)
        {
            var date = new DateTime(1970, 1, 1).AddMilliseconds(TimeStamps);
            return date;
        }

        /// <summary>
        /// 将时间戳转换为时间
        /// </summary>
        /// <returns></returns>
        public static DateTime ToTime(this string TimeStamps)
        {
            var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            try
            {
                if (TimeStamps.Length >= 13)
                {
                    return start.AddMilliseconds(Convert.ToInt64(TimeStamps)).AddHours(8);
                }
                else
                {
                    return start.AddSeconds(Convert.ToInt64(TimeStamps)).AddHours(8);
                }
            }
            catch { }
            return DateTime.Now.AddYears(-1);
        }

        /// <summary>
        /// 将时间戳转换为时间
        /// </summary>
        /// <returns></returns>
        public static DateTime ToUTCTime(this string TimeStamps)
        {
            var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            try
            {
                if (TimeStamps.Length >= 13)
                {
                    return start.AddMilliseconds(Convert.ToInt64(TimeStamps));
                }
                else
                {
                    return start.AddSeconds(Convert.ToInt64(TimeStamps));
                }
            }
            catch { }
            return DateTime.Now.AddYears(-1);
        }


        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的秒数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double GetTotalSeconds(this in DateTime dt) => new DateTimeOffset(dt).ToUnixTimeSeconds();

        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的毫秒数
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static double GetTotalMilliseconds(this in DateTime dt) => new DateTimeOffset(dt).ToUnixTimeMilliseconds();

        /// <summary>
        /// 获取该时间相对于1970-01-01 00:00:00的微秒时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long GetTotalMicroseconds(this in DateTime dt) => new DateTimeOffset(dt).Ticks / 10;

    }
}
