using System;
using System.Globalization;

namespace Infrastructure
{
    public static class DateTimeExtension
    {
        public static long ConvertToTimestamp(this DateTime value)
        {
            long epoch = (value.Ticks - 621355968000000000) / 10000000;
            return epoch;
        }
        public static DateTime? ConvertShamsiToMiladi(string date)
        {
            try
            {
                date = date.Replace("-", "/").Replace(".", "/");
                var persianYear = GetPersianYear(DateTime.Now);
                var parts = date.Split('/');
                var year = Convert.ToInt16(parts[0]);
                var month = Convert.ToInt16(parts[1]);
                var day = Convert.ToInt16(parts[2]);
                if (day > 1300)
                {
                    var temp = year;
                    year = day;
                    day = temp;
                }
                if (persianYear < 1410 && year < 10)
                    year += 1400;
                else if (persianYear < 1410 && year > 10 && year < 100)
                    year += 1300;
                else if (year + 1400 <= persianYear)
                    year += 1400;
                else if (year < 1300)
                    year += 1300;

                PersianCalendar persianCalendar = new PersianCalendar();
                return persianCalendar.ToDateTime(year, month, day, 0, 0, 0, 0);
            }
            catch
            {

                return null;
            }
        }
        public static string ComputedDateTime(this DateTime InsertDateTime)
        {
            PersianCalendar persianCalendar = new PersianCalendar();
            int year = persianCalendar.GetYear(InsertDateTime);
            int month = persianCalendar.GetMonth(InsertDateTime);
            int dayOfMonth = persianCalendar.GetDayOfMonth(InsertDateTime);
            int hour = persianCalendar.GetHour(InsertDateTime);
            int minute = persianCalendar.GetMinute(InsertDateTime);
            int second = persianCalendar.GetSecond(InsertDateTime);
            object[] objArray = new object[] { year, null, null, null, null, null };
            objArray[1] = (month < 10 ? string.Format("0{0}", month) : month.ToString());
            objArray[2] = (dayOfMonth < 10 ? string.Format("0{0}", dayOfMonth) : dayOfMonth.ToString());
            objArray[3] = (hour < 10 ? string.Format("0{0}", hour) : hour.ToString());
            objArray[4] = (minute < 10 ? string.Format("0{0}", minute) : minute.ToString());
            objArray[5] = (second < 10 ? string.Format("0{0}", second) : second.ToString());
            return string.Format("{0}-{1}-{2}-{3}{4}{5}", objArray);
        }

        public static long ConvertToTimeStamp(DateTime value)
        {
            DateTime dateTime = TimeZoneInfo.ConvertTime(value, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
            TimeZone.CurrentTimeZone.GetDaylightChanges(dateTime.Year);
            dateTime = dateTime.AddHours(8);
            DateTime dateTime1 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            DateTime localTime = dateTime1.ToLocalTime();
            return (long)Convert.ToDouble((dateTime - localTime).TotalSeconds);
        }

        public static string GetDayName(this DayOfWeek day)
        {
            switch (day)
            {
                case DayOfWeek.Sunday:
                    {
                        return "یک شنبه";
                    }
                case DayOfWeek.Monday:
                    {
                        return "دوشنبه";
                    }
                case DayOfWeek.Tuesday:
                    {
                        return "سه شنبه";
                    }
                case DayOfWeek.Wednesday:
                    {
                        return "چهار شنبه";
                    }
                case DayOfWeek.Thursday:
                    {
                        return "پنج شنبه";
                    }
                case DayOfWeek.Friday:
                    {
                        return "جمعه";
                    }
                case DayOfWeek.Saturday:
                    {
                        return "شنبه";
                    }
            }
            return "";
        }

        public static string GetMonthName(this int month)
        {
            switch (month)
            {
                case 1:
                    {
                        return "فروردین";
                    }
                case 2:
                    {
                        return "اردیبهشت";
                    }
                case 3:
                    {
                        return "خرداد";
                    }
                case 4:
                    {
                        return "تیر";
                    }
                case 5:
                    {
                        return "امرداد";
                    }
                case 6:
                    {
                        return "شهریور";
                    }
                case 7:
                    {
                        return "مهر";
                    }
                case 8:
                    {
                        return "آبان";
                    }
                case 9:
                    {
                        return "آذر";
                    }
                case 10:
                    {
                        return "دی";
                    }
                case 11:
                    {
                        return "بهمن";
                    }
                case 12:
                    {
                        return "اسفند";
                    }
            }
            return "";
        }

        public static string GetPersianDate(this DateTime value, bool showDayName, bool showMonthName, bool showTime, bool fromTripCreationTime = false)
        {
            PersianCalendar persianCalendar = new PersianCalendar();
            int year = persianCalendar.GetYear(value);
            int month = persianCalendar.GetMonth(value);
            int dayOfMonth = persianCalendar.GetDayOfMonth(value);
            string str = string.Format((showMonthName ? "{2} {1} {0}" : "{0}/{1}/{2}"), year, (showMonthName ? month.GetMonthName() : month.ToString("")), dayOfMonth);
            if (fromTripCreationTime)
                ;
            if (showDayName)
            {
                str = string.Format("{0} {1}", persianCalendar.GetDayOfWeek(value).GetDayName(), str);
            }
            if (showTime)
            {
                str = string.Format("{0} ساعت {1}:{2}", str, value.Hour, value.Minute);
            }
            return str;
        }
        public static int GetPersianYear(this DateTime value)
        {
            PersianCalendar persianCalendar = new PersianCalendar();
            int year = persianCalendar.GetYear(value);
            return year;
        }
        public static string GetPersianDate(this DateTime value)
        {
            if (value == DateTime.MinValue)
                return "";
            try
            {
                return value.GetPersianDate(false, false, false);

            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string GetPersianDate(this DateTime value, bool showDayName)
        {
            return value.GetPersianDate(showDayName, false, false);
        }

        public static string GetPersianDate(this DateTime value, bool showDayName, bool showTime)
        {
            return value.GetPersianDate(showDayName, false, showTime);
        }
        public static int GetPersianMonth(this DateTime value)
        {
            PersianCalendar persianCalendar = new PersianCalendar();
            int month = persianCalendar.GetMonth(value);
            return month;
        }
        public static int GetPersianMonthLenght(this DateTime value)
        {
            PersianCalendar persianCalendar = new PersianCalendar();
            int month = persianCalendar.GetMonth(value);
            if (month <= 6)
                return 31;
            if (month == 12)
                return 29;
            return 30;
        }
        public static int GetPersianDay(this DateTime value)
        {
            PersianCalendar persianCalendar = new PersianCalendar();
            int d = persianCalendar.GetDayOfMonth(value);
            return d;
        }
    }
}
