using System;

namespace MECF.Framework.Common.Aitex.Core.RT.EMS
{
    public static class DateTimeHelper
    {
        /// <summary>
        /// result = b - a(b > a) unit = min or hour or day
        /// </summary>
        /// <param name="timea"></param>
        /// <param name="timeb"></param>
        /// <param name="unit"></param>
        /// <returns></returns>
        public static double DateTimeSubtraction(DateTime timea,DateTime timeb, TimeUnit unit)
        {
            switch (unit)
            {
                case TimeUnit.Seconds:
                    return DiffSeconds(timea, timeb);
                case TimeUnit.Minutes:
                    return DiffMinutes(timea, timeb);
                case TimeUnit.Hours:
                    return DiffHours(timea, timeb);
                case TimeUnit.Days:
                    return DiffDays(timea, timeb);
                case TimeUnit.Months:
                case TimeUnit.Years:
                default:
                    return -1;
            }
        }

        public static double DiffSeconds(DateTime startTime, DateTime endTime)
        {
            TimeSpan sSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
            return Math.Round(sSpan.TotalSeconds, 0);
        }

        public static double DiffMinutes(DateTime startTime, DateTime endTime)
        {
            TimeSpan minuteSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
            return Math.Round(minuteSpan.TotalMinutes,2);
        }

        public static double DiffHours(DateTime startTime, DateTime endTime)
        {
            TimeSpan hoursSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
            return Math.Round(hoursSpan.TotalHours, 2);
        }

        public static double DiffDays(DateTime startTime, DateTime endTime)
        {
            TimeSpan daysSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
            return Math.Round(daysSpan.TotalDays,2);
        }

        public static double SecondsTransform(double seconds, TimeUnit unit)
        {
            switch (unit)
            {
                case TimeUnit.Seconds:
                    return seconds;
                case TimeUnit.Minutes:
                    return seconds / 60;
                case TimeUnit.Hours:
                    return seconds / 3600;
                case TimeUnit.Days:
                    return seconds / 86400;
                case TimeUnit.Months:
                    return seconds / 2592000;
                case TimeUnit.Years:
                    return seconds / 946080000;
                default:
                    return -1;
            }
        }

        public static double Transform2Seconds(double source, TimeUnit unit)
        {
            switch (unit)
            {
                case TimeUnit.Seconds:
                    return source;
                case TimeUnit.Minutes:
                    return source * 60;
                case TimeUnit.Hours:
                    return source * 3600;
                case TimeUnit.Days:
                    return source * 86400;
                case TimeUnit.Months:
                    return source * 2592000;
                case TimeUnit.Years:
                    return source * 946080000;
                default:
                    return -1;
            }
        }

    }
}
