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>
        /// 获取时间格式
        /// </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>
        /// 获取时间格式
        /// </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>
        /// 根据日期获取星期几
        /// </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="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();
        }
    }
}