using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Edu.Common
{
    /// <summary>
    /// 类型转换帮助类
    /// </summary>
    public static class ConvertHelper
    {
        /// <summary>
        /// 格式化时间【yyyy-MM-dd HH:mm:ss】
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string FormatTime(this object time)
        {
            string timeStr = "";
            if (time != null)
            {
                try
                {
                    timeStr = Convert.ToDateTime(time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                    if (timeStr == "0001-01-01 00:00:00")
                    {
                        timeStr = "";
                    }
                }
                catch
                {

                }
            }
            return timeStr;
        }

        /// <summary>
        /// 获取时间格式
        /// [HH:mm]
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string FormatTimeStr(this object time)
        {
            string timeStr = "";
            if (time != null)
            {
                try
                {
                    timeStr = Convert.ToDateTime(time.ToString()).ToString("HH:mm");
                }
                catch
                {

                }
            }
            return timeStr;
        }

        /// <summary>
        /// 获取时间格式yyyy-MM-dd HH:mm
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string FormatTimeStr2(this object time)
        {
            string timeStr = "";
            if (time != null)
            {
                try
                {
                    var newTime = Convert.ToDateTime(time.ToString());
                    if (newTime.Year == DateTime.Now.Year)
                    {
                        timeStr = Convert.ToDateTime(time.ToString()).ToString("MM-dd HH:mm");
                    }
                    else
                    {
                        timeStr = Convert.ToDateTime(time.ToString()).ToString("yyyy-MM-dd HH:mm");
                    }
                }
                catch
                {

                }
            }
            return timeStr;
        }

        /// <summary>
        /// 获取时间格式[MM-dd]
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string FormatMonthDay(this object time)
        {
            string timeStr = "";
            if (time != null)
            {
                try
                {
                    timeStr = Convert.ToDateTime(time.ToString()).ToString("MM-dd");
                }
                catch
                {

                }
            }
            return timeStr;
        }

        /// <summary>
        /// 格式化时间【yyyy-MM-dd】
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string FormatDate(this object time)
        {
            string timeStr = "";
            if (time != null)
            {
                try
                {
                    timeStr = Convert.ToDateTime(time.ToString()).ToString("yyyy-MM-dd");
                    if (timeStr == "0001-01-01")
                    {
                        timeStr = "";
                    }
                }
                catch
                {

                }
            }
            return timeStr;
        }

        /// <summary>
        /// 格式化时间【yyyy年MM月dd日】
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string FormatDate2(this object time)
        {
            string timeStr = "";
            if (time != null)
            {
                try
                {
                    timeStr = Convert.ToDateTime(time.ToString()).ToString("yyyy年MM月dd日");
                    if (timeStr == "0001年01月01日")
                    {
                        timeStr = "";
                    }
                }
                catch
                {

                }
            }
            return timeStr;
        }

        /// <summary>
        /// 字符串转List
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static List<int> StringToList(object value)
        {
            List<int> list = new List<int>();
            if (value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                var tempArray = value.ToString().Split(',');
                if (tempArray != null && tempArray.Length > 0)
                {
                    foreach (var item in tempArray)
                    {
                        Int32.TryParse(item, out int NewValue);
                        if (NewValue > 0)
                        {
                            list.Add(NewValue);
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 字符串转列表
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static List<string> StringToFileList(object value)
        {
            List<string> list = new List<string>();
            if (value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                var tempArray = value.ToString().Split(',');
                if (tempArray != null && tempArray.Length > 0)
                {
                    foreach (var item in tempArray)
                    {
                        if (!string.IsNullOrWhiteSpace(item))
                        {
                            list.Add(item);
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 时间集合转字符串用户SQL查询使用
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string DateTimeListToString(List<DateTime> list)
        {
            string str = "";
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    str += ",'" + FormatDate(item) + "'";
                }
            }
            return str.TrimStart(',').TrimEnd(',');
        }

        /// <summary>
        /// 根据日期获取星期几
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns></returns>
        public static string GetWeekDay(this object date)
        {
            string weekDay = "";
            if (date != null)
            {
                try
                {
                    var dateTime = Convert.ToDateTime(date.ToString());
                    string strWeek = dateTime.DayOfWeek.ToString();
                    switch (strWeek)
                    {
                        case "Monday": weekDay = "星期一"; break;
                        case "Tuesday": weekDay = "星期二"; break;
                        case "Wednesday": weekDay = "星期三"; break;
                        case "Thursday": weekDay = "星期四"; break;
                        case "Friday": weekDay = "星期五"; break;
                        case "Saturday": weekDay = "星期六"; break;
                        case "Sunday": weekDay = "星期天"; break;
                    }
                }
                catch
                {

                }
            }
            return weekDay;
        }

        /// <summary>
        /// 计算两个时间相差分钟数
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static int CalcMinutes(DateTime start, DateTime end)
        {
            int minutes = Convert.ToInt32((end - start).TotalSeconds);
            minutes /= 60;
            return minutes;
        }

        /// <summary>
        /// 是否为时间型字符串
        /// </summary>
        /// <param name="source">时间字符串(15:00:00)</param>
        /// <returns></returns>
        public static bool IsTime(string StrSource)
        {
            return Regex.IsMatch(StrSource, @"^((20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d)$");
        }

        /// <summary>
        /// 获取英文字母
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetEnChar(int index)
        {
            string[] tempArray = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            if (index < tempArray.Length)
            {
                return tempArray[index];
            }
            return "";
        }

        /// <summary>
        /// 根据文件路径转base64
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string ConvertImageToBase64(string filePath)
        {
            Image file = Image.FromFile(filePath);
            using MemoryStream memoryStream = new MemoryStream();
            file.Save(memoryStream, file.RawFormat);
            byte[] imageBytes = memoryStream.ToArray();
            return Convert.ToBase64String(imageBytes);
        }

        /// <summary>
        /// 字符串转数字
        /// </summary>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public static int ConvertStringToNumber(string inputStr)
        {
            int result = 0;
            var chatArray = inputStr.ToCharArray();
            foreach (var cItem in chatArray)
            {
                switch (cItem)
                {
                    case '一':
                        result += 1;
                        break;
                    case '二':
                        result += 2;
                        break;
                    case '三':
                        result += 3;
                        break;
                    case '四':
                        result += 4;
                        break;
                    case '五':
                        result += 5;
                        break;
                    case '六':
                        result += 6;
                        break;
                    case '七':
                        result += 7;
                        break;
                    case '八':
                        result += 8;
                        break;
                    case '九':
                        result += 9;
                        break;
                    case '十':
                        result += 10;
                        break;
                }
            }
            return result;
        }

        /// <summary>
        /// 拼接图片字符串
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        public static string GetImageString(string base64)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"<p><img src='data:image/png;base64,{0}' /></p>", base64);
            return builder.ToString();
        }

        /// <summary>
        /// 拼接下划线
        /// </summary>
        /// <param name="value"></param>
        /// <param name="isAddNbsp"></param>
        /// <returns></returns>
        public static string GetSpanString(string value, bool isAddNbsp = false)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"<span style='text-decoration: underline;'>{1}{0}{1}</span>", value, (isAddNbsp ? "&nbsp;&nbsp;" : ""));
            return builder.ToString();
        }

        /// <summary>
        /// 根据出生日期计算年龄
        /// </summary>
        /// <param name="birthdate"></param>
        /// <returns></returns>
        public static int GetAgeByBirthdate(object birthdate)
        {
            int age = 0;
            DateTime now = DateTime.Now;
            if (birthdate != null)
            {
                try
                {
                    var timeStr = Convert.ToDateTime(birthdate.ToString()).ToString("yyyy-MM-dd");
                    if (timeStr == "0001-01-01")
                    {
                        timeStr = "";
                    }
                    if (!string.IsNullOrEmpty(timeStr))
                    {
                        var newBirth = Convert.ToDateTime(timeStr);
                        age = now.Year - newBirth.Year;
                        if (now.Month < newBirth.Month || (now.Month == newBirth.Month && now.Day < newBirth.Day))
                        {
                            age--;
                        }
                    }
                }
                catch
                {

                }
            }
            return age < 0 ? 0 : age;
        }

        /// <summary>
        /// 获取时间段字符串
        /// </summary>
        /// <param name="objDate"></param>
        /// <returns></returns>
        public static string GetTimeStr(object objDate)
        {
            string str = "";
            if (objDate != null)
            {
                DateTime date = Convert.ToDateTime(objDate);
                int times = date.Hour;
                if (times >= 0 && times < 12) { str = "早上"; }
                if (times >= 12 && times < 17) { str = "下午"; }
                if (times >= 17 && times < 24) { str = "晚上"; }
            }
            return str;
        }

        /// <summary>
        /// 获取最近一个星期一
        /// </summary>
        /// <param name="objDate"></param>
        /// <returns></returns>
        public static string GetWeekMonday(string objDate)
        {
            DateTime date = Convert.ToDateTime(objDate);
            DateTime first = System.DateTime.Now;
            switch (date.DayOfWeek)
            {
                case System.DayOfWeek.Monday:
                    first = date;
                    break;
                case System.DayOfWeek.Tuesday:
                    first = date.AddDays(-1);
                    break;
                case System.DayOfWeek.Wednesday:
                    first = date.AddDays(-2);
                    break;
                case System.DayOfWeek.Thursday:
                    first = date.AddDays(-3);
                    break;
                case System.DayOfWeek.Friday:
                    first = date.AddDays(-4);
                    break;
                case System.DayOfWeek.Saturday:
                    first = date.AddDays(-5);
                    break;
                case System.DayOfWeek.Sunday:
                    first = date.AddDays(-6);
                    break;
            }
            return FormatDate(first);
        }

        /// <summary>
        /// 获取最近一个星期天
        /// </summary>
        /// <param name="objDate"></param>
        /// <returns></returns>
        public static string GetWeekSunday(string objDate)
        {
            DateTime date = Convert.ToDateTime(objDate);
            DateTime last = Convert.ToDateTime(objDate);
            switch (date.DayOfWeek)
            {
                case System.DayOfWeek.Monday:
                    last = date.AddDays(6);
                    break;
                case System.DayOfWeek.Tuesday:
                    last = date.AddDays(5);
                    break;
                case System.DayOfWeek.Wednesday:
                    last = date.AddDays(4);
                    break;
                case System.DayOfWeek.Thursday:
                    last = date.AddDays(3);
                    break;
                case System.DayOfWeek.Friday:
                    last = date.AddDays(2);
                    break;
                case System.DayOfWeek.Saturday:
                    last = date.AddDays(1);
                    break;
                case System.DayOfWeek.Sunday:
                    last = date;
                    break;
            }
            return FormatDate(last);
        }

        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <returns></returns>
        public static int CreateRandomNum()
        {
            Random rd = new Random();
            int result = rd.Next(1, 100);
            return result % 4;
        }

        /// <summary>
        /// 一年中的第几周
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int WeekOfYear(DateTime dt)
        {
            int days = DaysInFirstweekInYear(dt);
            int dayofyear = dt.DayOfYear;
            if (dayofyear < days) return 1;
            else
            {
                int week2 = (int)Math.Ceiling((double)(dayofyear - days) / (double)7);
                return week2 + 1;
            }
        }

        /// <summary>
        /// 计算某年第一周的天数
        /// </summary>
        /// <param name="dt">某年中的一个时间</param>
        /// <returns></returns>
        public static int DaysInFirstweekInYear(DateTime dt)
        {
            DateTime FirstDate = Convert.ToDateTime(string.Format("{0}-1-1", dt.Year));
            int DayOfWeekInYear = DayOfWeek(FirstDate);
            int DaysInFirstWeek = 8 - DayOfWeekInYear;
            return DaysInFirstWeek;
        }

        /// <summary>
        /// 计算星期几，转换为数字
        /// </summary>
        /// <param name="dt">某天的日期</param>
        /// <returns></returns>
        public static int DayOfWeek(DateTime dt)
        {
            string strDayOfWeek = dt.DayOfWeek.ToString().ToLower();
            int intDayOfWeek = 0;
            switch (strDayOfWeek)
            {
                case "monday":
                    intDayOfWeek = 1;
                    break;
                case "tuesday":
                    intDayOfWeek = 2;
                    break;
                case "wednesday":
                    intDayOfWeek = 3;
                    break;
                case "thursday":
                    intDayOfWeek = 4;
                    break;
                case "friday":
                    intDayOfWeek = 5;
                    break;
                case "saturday":
                    intDayOfWeek = 6;
                    break;
                case "sunday":
                    intDayOfWeek = 7;
                    break;
            }
            return intDayOfWeek;

        }
    }
}