using System;

namespace RMYY_CallCenter_Api.Utility
{
    /// <summary>
    ///DateTimeConvert 
    /// </summary>
    public class DateTimeConvert
    {
        private static readonly DateTime StartedDateTime = new DateTime(1970, 1, 1,0,0,0,DateTimeKind.Utc);

        /// <summary>
        /// 根据阿拉伯数字返回月份的名称(可更改为某种语言)
        /// </summary>	
        public static string[] Monthes
        {
            get
            {
                return new[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
            }
        }


        /// <summary>
        /// 判断是不是当月的最后一天，是返回true，否返回flase
        /// </summary>
        /// <param name="Dt"></param>
        /// <returns></returns>
        public bool IsLastMonthDay(DateTime Dt)
        {
            bool Flag = false;
            DateTime MonthClearTime = Dt.AddDays(1 - Dt.Day).AddMonths(1).AddDays(-1);
            if (DateDiff("D", Dt, MonthClearTime) == 0)
            {
                Flag = true;
            }
            return Flag;
        }



        #region 日期时间方法
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = null;
            TimeSpan ts = DateTime1.Subtract(DateTime2).Duration();
            string days = "";string hours = "";
            if (ts.Days.ToString() != "0")
                days = ts.Days.ToString() + "天";
            if(ts.Hours.ToString() !="0")
                hours = ts.Hours.ToString() + "时";
            dateDiff = days + hours + ts.Minutes.ToString() + "分";    // + ts.Seconds.ToString() + "秒";
            return dateDiff;
        }

        /// <summary>
        /// 日期时间相减方法 M 月，D 天，H小时，T分，S秒
        /// </summary>
        /// <param name="dtype"></param>
        /// <param name="D1"></param>
        /// <param name="D2"></param>
        /// <returns></returns>
        public static int DateDiff(string dtype, DateTime D1, DateTime D2)
        {
            int d = 0;
            int h = 0;
            int t = 0;
            switch (dtype.ToUpper())
            {
                case "M":
                    d = D2.Year - D1.Year;
                    h = D2.Month - D1.Month;
                    if (d > 0)
                    {
                        return d * 12 + h + 1;
                    }
                    if (d == 0)
                    {
                        if (h > 0)
                        {
                            return h + 1;
                        }
                        if (h == 0)
                        {
                            return 1;
                        }
                        return d * 12 + h - 1;
                    }
                    return d * 12 + h - 1;
                case "D":
                    TimeSpan Num = Convert.ToDateTime(D2.ToShortDateString()) - Convert.ToDateTime(D1.ToShortDateString());
                    return Num.Days;
                case "H":
                    d = DateDiff("D", D1, D2);
                    h = D2.Hour - D1.Hour;
                    return h + (d * 24);
                case "T":
                    d = Convert.ToInt32(D2.Subtract(D1).Days);
                    h = Convert.ToInt32(D2.Subtract(D1).Hours);
                    t = Convert.ToInt32(D2.Subtract(D1).Minutes);
                    return ((h + (d * 24)) * 60) + t;
                case "S":
                    d = Convert.ToInt32(D2.Subtract(D1).Days);
                    h = Convert.ToInt32(D2.Subtract(D1).Hours);
                    t = Convert.ToInt32(D2.Subtract(D1).Minutes);
                    return ((((h + (d * 24)) * 60) + t) * 60) + Convert.ToInt32(D2.Subtract(D1).Seconds);
                default:
                    return -1;
            }
        }

        #endregion

        /// <summary>
        /// 将时间戳转换为DateTime类型
        /// </summary>
        /// <param name="timetag"></param>
        /// <param name="toLocalTime"></param>
        /// <returns></returns>
        public static DateTime FromTimetag(long timetag,bool toLocalTime = true)
        {
            var time = new DateTime(StartedDateTime.Ticks + timetag*10000);
            return toLocalTime ? time.ToLocalTime() : time;
        }

        /// <summary>
        /// 将Datetime类型转换为时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static long ToTimetag(DateTime dateTime)
        { 
            return (dateTime.ToUniversalTime().Ticks - StartedDateTime.Ticks)/10000;
        }

        ///<summary>
        ///由秒数得到日期几天几小时。。。
        ///</summary
        ///<param name="t">秒数</param>
        ///<param name="type">0：转换后带秒，1:转换后不带秒</param>
        ///<returns>几天几小时几分几秒</returns>
        public static string parseTimeSeconds(int t, int type)
        {
            string r = "";
            int day, hour, minute, second;
            if (t >= 86400) //天,
            {
                day = Convert.ToInt32(t / 86400);
                hour = Convert.ToInt32((t % 86400) / 3600);
                minute = Convert.ToInt32((t % 86400 % 3600) / 60);
                second = Convert.ToInt32(t % 86400 % 3600 % 60);
                if (type == 0)
                    r = day + ("天") + hour + ("小时") + minute + ("分") + second + ("秒");
                else
                    r = day + ("天") + hour + ("小时") + minute + ("分");

            }
            else if (t >= 3600)//时,
            {
                hour = Convert.ToInt32(t / 3600);
                minute = Convert.ToInt32((t % 3600) / 60);
                second = Convert.ToInt32(t % 3600 % 60);
                if (type == 0)
                    r = hour + ("小时") + minute + ("分") + second + ("秒");
                else
                    r = hour + ("小时") + minute + ("分");
            }
            else if (t >= 60)//分
            {
                minute = Convert.ToInt32(t / 60);
                second = Convert.ToInt32(t % 60);
                r = minute + ("分") + second + ("秒");
            }
            else
            {
                second = Convert.ToInt32(t);
                r = second + ("秒");
            }
            return r;
        }




        ///<summary>
        ///由秒数得到日期几天几小时。。。
        ///</summary
        ///<param name="t">秒数</param>
        ///<param name="type">0：转换后带秒，1:转换后不带秒</param>
        ///<returns>几天几小时几分几秒</returns>
        public static string parseTimeSeconds(double t, double type)
        {
            string r = "";
            double day, hour, minute, second;
            if (t >= 86400) //天,
            {
                day = Convert.ToInt32(Math.Floor(t / 86400));
                hour = Convert.ToInt32(Math.Floor((t % 86400) / 3600));
                minute = Convert.ToInt32(Math.Floor((t % 86400 % 3600) / 60));
                second = Convert.ToInt32(Math.Floor(t % 86400 % 3600 % 60));
                if (type == 0)
                    r = day + ("天") + hour + ("小时") + minute + ("分") + second + ("秒");
                else
                    r = day + ("天") + hour + ("小时") + minute + ("分");

            }
            else if (t >= 3600)//时,
            {
                hour = Convert.ToInt32(Math.Floor(t / 3600));
                minute = Convert.ToInt32(Math.Floor((t % 3600) / 60));
                second = Convert.ToInt32(Math.Floor(t % 3600 % 60));
                if (type == 0)
                    r = hour + ("小时") + minute + ("分") + second + ("秒");
                else
                    r = hour + ("小时") + minute + ("分");
            }
            else if (t >= 60)//分
            {
                minute = Convert.ToInt32(Math.Floor(t / 60));
                second = Convert.ToInt32(Math.Floor(t % 60));
                r = minute + ("分") + second + ("秒");
            }
            else
            {
                second = Convert.ToInt32(t);
                r = second + ("秒");
            }
            return r;
        }



        /// <summary>
        /// 当前日期时间，转换为秒
        /// </summary>
        /// <returns>秒数</returns>
        public static string xDateSeconds()
        {
            long xdateseconds = 0;
            DateTime xdatenow = DateTime.UtcNow; //当前UTC时间


            long xminute = 60; //一分种60秒
            long xhour = 3600;
            long xday = 86400;
            long byear = 1970;//从1970-1-1 0：00：00开始到现在所过的秒
            long[] xmonth = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
            long[] xyear = { 365, 366 };
            long num = 0;

            xdateseconds += xdatenow.Second; //算秒
            xdateseconds += xdatenow.Minute * xminute; //算分
            xdateseconds += xdatenow.Hour * xhour; //算时
            xdateseconds += (xdatenow.Day - 1) * xday; //算天

            //算月(月换成天算)
            if (DateTime.IsLeapYear(xdatenow.Year))
            {
                xdateseconds += (xmonth[xdatenow.Month - 1] + 1) * xday;
            }
            else
            {
                xdateseconds += (xmonth[xdatenow.Month - 1]) * xday;
            }

            //算年（年换成天算）
            long lyear = xdatenow.Year - byear;

            for (int i = 0; i < lyear; i++)
            {
                if (DateTime.IsLeapYear((int)byear + i))
                {
                    num++;
                }
            }

            xdateseconds += ((lyear - num) * xyear[0] + num * xyear[1]) * xday;

            return xdateseconds.ToString();
        }

        /// <summary>
        /// 秒数转日期
        /// </summary>
        /// <param name="Value">秒数</param>
        /// <returns>日期</returns>
        public static DateTime GetGMTDateTime(int Value)
        {
            //秒数转时间日期
            //GMT时间从2000年1月1日开始，先把它作为赋为初值
            long Year = 2000, Month = 1, Day = 1;
            long Hour = 0, Min = 0, Sec = 0;
            //临时变量
            long iYear = 0, iDay = 0;
            long iHour = 0, iMin = 0, iSec = 0;
            //计算文件创建的年份
            iYear = Value / (365 * 24 * 60 * 60);
            Year = Year + iYear;
            //计算文件除创建整年份以外还有多少天
            iDay = (Value % (365 * 24 * 60 * 60)) / (24 * 60 * 60);
            //把闰年的年份数计算出来
            int RInt = 0;
            for (int i = 2000; i < Year; i++)
            {
                if ((i % 4) == 0)
                    RInt = RInt + 1;
            }
            //计算文件创建的时间(几时)
            iHour = ((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) / (60 * 60);
            Hour = Hour + iHour;
            //计算文件创建的时间(几分)
            iMin = (((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) % (60 * 60)) / 60;
            Min = Min + iMin;
            //计算文件创建的时间(几秒)
            iSec = (((Value % (365 * 24 * 60 * 60)) % (24 * 60 * 60)) % (60 * 60)) % 60;
            Sec = Sec + iSec;
            DateTime t = new DateTime((int)Year, (int)Month, (int)Day, (int)Hour, (int)Min, (int)Sec);
            DateTime t1 = t.AddDays(iDay - RInt);
            return t1;
        }
    }
}
