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

namespace Ropin.Inspection.Common.Helper
{
    public class DateHelper
    {
        public static DateTime GetDateTime(long timestamp)
        {
            DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            long lTime = long.Parse(timestamp + "0000000"); // 如果时间戳是秒级,需要转换为毫秒级
            TimeSpan timeSpan = new TimeSpan(lTime);
            DateTime targetDt = dtStart.Add(timeSpan);
            return targetDt;
        }
        public static DateTime GetDateTime(long timestamp, TimeZoneInfo targetTimeZone)
        {
            DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            long lTime = long.Parse(timestamp + "0000000"); // 如果时间戳是秒级,需要转换为毫秒级
            TimeSpan timeSpan = new TimeSpan(lTime);
            DateTime targetDt = dtStart.Add(timeSpan);
            return TimeZoneInfo.ConvertTimeFromUtc(targetDt, targetTimeZone);
        }
        public static DateTime StampToDateTime(string time)
        {
            time = time.Substring(0, 10);
            double timestamp = Convert.ToInt64(time);
            System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
            dateTime = dateTime.AddSeconds(timestamp).ToLocalTime();
            return dateTime;
        }

        public static string TimeSubTract(DateTime time1, DateTime time2)
        {
            TimeSpan subTract = time1.Subtract(time2);
            return $"{subTract.Days} 天 {subTract.Hours} 时 {subTract.Minutes} 分 ";
        }
        /// <summary>
        ///  时间戳转本地时间-时间戳精确到秒
        /// </summary> 
        public static DateTime ToLocalTimeDateBySeconds(long unix)
        {
            var dto = DateTimeOffset.FromUnixTimeSeconds(unix);
            return dto.ToLocalTime().DateTime;
        }

        /// <summary>
        ///  时间转时间戳Unix-时间戳精确到秒
        /// </summary> 
        public static long ToUnixTimestampBySeconds(DateTime dt)
        {
            DateTimeOffset dto = new DateTimeOffset(dt);
            return dto.ToUnixTimeSeconds();
        }


        /// <summary>
        ///  时间戳转本地时间-时间戳精确到毫秒
        /// </summary> 
        public static DateTime ToLocalTimeDateByMilliseconds(long unix)
        {
            var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
            return dto.ToLocalTime().DateTime;
        }

        /// <summary>
        ///  时间转时间戳Unix-时间戳精确到毫秒
        /// </summary> 
        public static long ToUnixTimestampByMilliseconds(DateTime dt)
        {
            DateTimeOffset dto = new DateTimeOffset(dt);
            return dto.ToUnixTimeMilliseconds();
        }
        /// <summary>
        /// 声明期间类型枚举
        /// </summary>
        public enum Period { Day, Week, Month, Year };
        /// <summary>
        /// 获取指定期间的起止日期
        /// </summary>
        /// <param name="period">期间类型</param>
        /// <param name="beginDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        public static void GetPeriod(Period period, out DateTime beginDate, out DateTime endDate)
        {
            int year = DateTime.Today.Year;
            int month = DateTime.Today.Month;
            switch (period)
            {
                case Period.Year: //年
                    beginDate = new DateTime(year, 1, 1);
                    endDate = new DateTime(year, 12, 31);
                    break;
                case Period.Month: //月
                    beginDate = new DateTime(year, month, 1);
                    endDate = beginDate.AddMonths(1).AddDays(-1);
                    break;
                case Period.Week: //周
                    int week = (int)DateTime.Today.DayOfWeek;
                    if (week == 0) week = 7; //周日
                    beginDate = DateTime.Today.AddDays(-(week - 1));
                    endDate = beginDate.AddDays(6);
                    break;
                default: //日
                    beginDate = DateTime.Today;
                    endDate = DateTime.Today;
                    break;
            }
        }
    }
}