using System;
using System.Diagnostics;
using ET;
namespace GFGGame
{
    public partial class TimeUtil
    {
        /// 
        /// 
        /// 
        /// 毫秒
        /// 毫秒
        /// 
        public static string FormattingTime(long curTime, long endTime)
        {
            long time = (endTime / 1000) - (curTime / 1000);
            int days = (int)Math.Floor((decimal)time / TimeUtil.SECOND_PER_DAY);
            if (days >= 1)
            {
                return string.Format("{0}天", days);
            }
            int hours = (int)Math.Floor((decimal)time / TimeUtil.SECOND_PER_HOUR);
            if (hours >= 1)
            {
                return string.Format("{0}小时", hours);
            }
            int minutes = (int)Math.Floor((decimal)time / TimeUtil.SECOND_PER_MUNITE);
            if (minutes >= 1)
            {
                return string.Format("{0}分钟", minutes);
            }
            return string.Format("{0}秒", time);
        }
        public static string FormattingTimeDetail(long curTime, long endTime)
        {
            long time = (endTime / 1000) - (curTime / 1000);
            int days = (int)Math.Floor((decimal)time / TimeUtil.SECOND_PER_DAY);
            int hours = (int)Math.Floor((decimal)time / TimeUtil.SECOND_PER_HOUR);
            if (days >= 1)
            {
                return string.Format("{0}天{0}小时", days, hours);
            }
            int minutes = (int)Math.Floor((decimal)time / TimeUtil.SECOND_PER_MUNITE);
            if (hours >= 1)
            {
                return string.Format("{0}小时{0}分钟", hours, minutes);
            }
            if (minutes >= 1)
            {
                return string.Format("{0}分钟{0}秒", minutes, time);
            }
            return string.Format("{0}秒", time);
        }
        public static int FormattingTime11(long curTime, long endTime)
        {
            long time = (endTime / 1000) - (curTime / 1000);
            int days = (int)Math.Ceiling((decimal)time / TimeUtil.SECOND_PER_DAY);
            return days;
        }
        /// 
        /// 
        /// 
        /// 毫秒
        /// 毫秒
        /// 
        /// 
        public static void FormattingTime(long curTime, long endTime, out int num, out string str)
        {
            long time = (endTime / 1000) - (curTime / 1000);
            int days = (int)Math.Floor((decimal)time / TimeUtil.SECOND_PER_DAY);
            if (days >= 1)
            {
                num = days;
                str = "天";
                return;
            }
            int hours = (int)Math.Floor((decimal)time / TimeUtil.SECOND_PER_HOUR);
            if (hours >= 1)
            {
                num = hours;
                str = "小时";
                return;
            }
            int minutes = (int)Math.Floor((decimal)time / TimeUtil.SECOND_PER_MUNITE);
            if (minutes >= 1)
            {
                num = minutes;
                str = "分钟";
                return;
            }
            num = minutes;
            str = "秒";
        }
        /// 
        /// 将时间戳转换成yyyy-mm-dd hh-mm-ss格式
        /// 
        /// 毫秒
        public static string FormattingTime1(long timeMsec, char split = '-')
        {
            DateTime date = TimeInfo.Instance.ToDateTime(timeMsec);
            string str = date.ToString(string.Format("yyyy{0}MM{0}dd HH:mm:ss", split));
            return str;
        }
        /// 
        /// 将时间戳转换成yyyy-mm-dd hh-mm-ss格式
        /// 
        /// 毫秒
        public static string FormattingTimeMinute(long timeMsec, char split = '-')
        {
            DateTime date = TimeInfo.Instance.ToDateTime(timeMsec);
            string str = date.ToString(string.Format("yyyy{0}MM{0}dd HH:mm", split));
            return str;
        }
        /// 
        /// 将时间戳转换成yyyy/MM/dd格式
        /// 
        /// 毫秒
        public static string FormattingTimeTo_yyyMMdd0(long timeMsec)
        {
            DateTime date = TimeInfo.Instance.ToDateTime(timeMsec);
            string str = date.ToString("yyyy/MM/dd ");
            return str;
        }
        /// 
        /// 将时间戳转换成yyyy.MM.dd格式
        /// 
        /// 毫秒
        public static string FormattingTimeTo_yyyMMdd1(long timeMSec)
        {
            DateTime date = TimeInfo.Instance.ToDateTime(timeMSec);
            string str = date.ToString("yyyy.MM.dd");
            return str;
        }
        /// 
        /// 将时间戳转换成yyyy-MM-dd格式
        /// 
        /// 
        public static string FormattingTimeTo_yyyMMdd2(long timeMsec)
        {
            DateTime date = TimeInfo.Instance.ToDateTime(timeMsec);
            string str = date.ToString("yyyy-MM-dd ");
            return str;
        }
        /// 
        /// 将时间戳转换成MM/dd格式
        /// 
        /// 
        public static string FormattingTimeTo_MMdd(long timeSec)
        {
            DateTime date = TimeInfo.Instance.ToDateTime(timeSec);
            string str = date.ToString("MM/dd");
            return str;
        }
        /// 
        /// 输入一段时间(毫秒秒),将时间转换成HH:mm:ss格式
        /// 
        /// 
        public static string FormattingTimeTo_HHmmss(long timeMsec)
        {
            TimeSpan ts = new TimeSpan(0, 0, (int)(timeMsec / 1000));
            string strH = ts.Hours.ToString().Length == 1 ? "0" + ts.Hours : ts.Hours.ToString();
            string strM = ts.Minutes.ToString().Length == 1 ? "0" + ts.Minutes : ts.Minutes.ToString();
            string strS = ts.Seconds.ToString().Length == 1 ? "0" + ts.Seconds : ts.Seconds.ToString();
            return string.Format("{0}:{1}:{2}", strH, strM, strS);
        }
        /// 
        /// 输入一段时间(毫秒),将时间转换成DD天HH小时mm分格式
        /// 
        /// 
        public static string FormattingTimeTo_DDHHmm(long timeMsec)
        {
            TimeSpan ts = new TimeSpan(0, 0, (int)(timeMsec / 1000));
            // string strD = ts.Days.ToString().Length == 1 ? "0" + ts.Days : ts.Days.ToString();
            // string strH = ts.Hours.ToString().Length == 1 ? "0" + ts.Hours : ts.Hours.ToString();
            // string strM = ts.Minutes.ToString().Length == 1 ? "0" + ts.Minutes : ts.Minutes.ToString();
            string strD = ts.Days.ToString();
            string strH = ts.Hours.ToString();
            string strM = ts.Minutes.ToString();
            return string.Format("{0}天{1}时{2}分", strD, strH, strM);
        }
        /// 
        /// 输入一段时间(毫秒),将时间转换成DD:HH:mm格式
        /// 
        /// 
        public static string FormattingTimeTo_DD_HH_mm(long timeMsec)
        {
            TimeSpan ts = new TimeSpan(0, 0, (int)(timeMsec / 1000));
            string strD = ts.Days.ToString();
            string strH = ts.Hours.ToString();
            string strM = ts.Minutes.ToString();
            return string.Format("{0}:{1}:{2}", strD, strH, strM);
        }
        /// 
        /// 
        /// 将时间戳转换成HH:mm格式
        /// 
        /// 毫秒
        public static string FormattingTimeTo_HHmm(long timeMsec)
        {
            DateTime date = TimeInfo.Instance.ToDateTime(timeMsec);
            string str = date.ToString("HH:mm");
            return str;
        }
        /// 
        /// 
        /// 将时间戳转换成mm:ss格式
        /// 
        /// 毫秒
        public static string FormattingTimeTo_mmss(long timeMsec)
        {
            DateTime date = TimeInfo.Instance.ToDateTime(timeMsec);
            string str = date.ToString("mm:ss");
            return str;
        }
        /// 
        /// 计算两个时间戳之间的时间差,返回格式为: 1天1小时1分1秒,如果时间差小于1天,则返回格式为: 03:59:30
        /// 
        /// 开始时间戳毫秒级,一般是服务器当前时间
        /// 结束时间戳毫秒级
        /// 
        public static string GetTimeLeft(long startTimeStamp, long endTimeStamp)
        {
            DateTime dtStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(startTimeStamp);
            DateTime dtEnd = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(endTimeStamp);
            TimeSpan timeLeft = dtEnd.Subtract(DateTime.UtcNow).Add(DateTime.UtcNow.Subtract(dtStart));
            string timeLeftFormatted;
            if (timeLeft.TotalHours < 24)
            {
                timeLeftFormatted = timeLeft.ToString(@"hh\:mm\:ss");
            }
            else
            {
                timeLeftFormatted = string.Format("{0}天{1}时{2}分", timeLeft.Days, timeLeft.Hours, timeLeft.Minutes);
            }
            return timeLeftFormatted;
        }
        /// 
        /// 测试函数耗时
        /// 
        /// 
        public static void CalculateExecutionTime(Action function)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            // Invoke the provided function
            function.Invoke();
            stopwatch.Stop();
            ET.Log.Warning("Execution time: " + stopwatch.Elapsed.TotalMilliseconds + " ms");
        }
    }
}