using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
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");
                }
                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>
        /// 字符串转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;
        }
    }
}