using Microsoft.AspNetCore.Http;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WebAPIBase.Utils
{
    public class ConvertRule
    {
        public static decimal ToDecimal(object val)
        {
            decimal result = default(decimal);
            if (val != null && val.ToString() != "")
            {
                try
                {
                    try
                    {
                        result = Convert.ToDecimal(val);
                        return result;
                    }
                    catch
                    {
                        result = decimal.Parse(val.ToString());
                        return result;
                    }
                }
                catch
                {
                }
            }
            return result;
        }

        public static object ToDecimalObj(object val)
        {
            object result = DBNull.Value;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = decimal.Parse(val.ToString());
                }
                catch
                {
                }
            }
            return result;
        }

        public static double ToDouble(object val)
        {
            double result = 0.0;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = double.Parse(val.ToString());
                }
                catch
                {
                }
            }
            return result;
        }

        public static object ToDoubleObj(object val)
        {
            object result = DBNull.Value;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = double.Parse(val.ToString());
                }
                catch
                {
                }
            }
            return result;
        }

        public static int ToInt(object val)
        {
            int result = 0;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = int.Parse(val.ToString().Replace(",", ""));
                }
                catch
                {
                }
            }
            return result;
        }

        public static int? ToIntNull(object val)
        {
            int? result = null;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = int.Parse(val.ToString().Replace(",", ""));
                    return result;
                }
                catch
                {
                }
            }
            return result;
        }

        public static object ToIntObj(object val)
        {
            object result = DBNull.Value;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = int.Parse(val.ToString().Replace(",", ""));
                }
                catch
                {
                }
            }
            return result;
        }

        public static long ToLong(object val)
        {
            long result = 0L;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = long.Parse(val.ToString());
                }
                catch
                {
                }
            }
            return result;
        }

        public static object ToLongObj(object val)
        {
            object result = DBNull.Value;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = long.Parse(val.ToString());
                }
                catch
                {
                }
            }
            return result;
        }

        public static object ToDate(object val)
        {
            object result = DBNull.Value;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = DateTime.Parse(val.ToString());
                }
                catch
                {
                }
            }
            return result;
        }

        public static DateTime? ToDateTime(object val)
        {
            DateTime? result = null;
            if (val != null && val.ToString() != "")
            {
                try
                {
                    result = DateTime.Parse(val.ToString());
                    return result;
                }
                catch
                {
                }
            }
            return result;
        }

        public static string ToDateString(object val, string format)
        {
            string result = "";
            if (val != null && val.ToString() != "")
            {
                try
                {
                    if (DateTime.Parse(val.ToString()) != DateTime.MinValue)
                    {
                        result = DateTime.Parse(val.ToString()).ToString(format);
                    }
                }
                catch
                {
                }
            }
            return result;
        }

        public static string ToDateString(object val, string format, string nullTimeFormat)
        {
            string result = "";
            if (val != null && val.ToString() != "")
            {
                try
                {
                    if (DateTime.Parse(val.ToString()) != DateTime.MinValue)
                    {
                        DateTime dateTime = DateTime.Parse(val.ToString());
                        result = ((dateTime.Hour != 0 || dateTime.Minute != 0 || dateTime.Second != 0) ? dateTime.ToString(format) : dateTime.ToString(nullTimeFormat));
                    }
                }
                catch
                {
                }
            }
            return result;
        }

        public static string ToDateString(object val)
        {
            return ToDateString(val, "yyyy-MM-dd");
        }

        public static string ToString(object val)
        {
            string result = "";
            try
            {
                if (val != null && val != DBNull.Value)
                {
                    try
                    {
                        result = val.ToString();
                    }
                    catch
                    {
                    }
                }
            }
            catch
            {
            }
            return result;
        }

        public static bool ToBool(object val)
        {
            bool result = false;
            try
            {
                if (val != null && val != DBNull.Value)
                {
                    try
                    {
                        result = bool.Parse(val.ToString());
                    }
                    catch
                    {
                    }
                }
            }
            catch
            {
            }
            return result;
        }

        public static string[] ArrayConcat(object[] arr1, object[] arr2)
        {
            int num = 0;
            if (arr1 != null)
            {
                num += arr1.Length;
            }
            if (arr2 != null)
            {
                num += arr2.Length;
            }
            string[] array = new string[num];
            int num2 = 0;
            if (arr1 != null)
            {
                arr1.CopyTo(array, num2);
                num2 += arr1.Length;
            }
            if (arr2 != null)
            {
                arr2.CopyTo(array, num2);
                num2 += arr2.Length;
            }
            return array;
        }

        public static string JoinArray(object[] arr, string separator)
        {
            string text = "";
            int num = arr.Length;
            for (int i = 0; i < num; i++)
            {
                if (i > 0)
                {
                    text += separator;
                }
                text += arr[i].ToString();
            }
            return text;
        }

        public static string JoinArrayList(ArrayList al, string separator)
        {
            string text = "";
            int count = al.Count;
            for (int i = 0; i < count; i++)
            {
                if (i > 0)
                {
                    text += separator;
                }
                text += ToString(al[i]);
            }
            return text;
        }

        public static string JoinTableColumn(DataTable tb, string ColumnName, string separator)
        {
            string text = "";
            int count = tb.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                if (i > 0)
                {
                    text += separator;
                }
                text += ToString(tb.Rows[i][ColumnName]);
            }
            return text;
        }

        public static int FindArray(string[] arr, string val)
        {
            return FindArray(arr, val, isEgnoreCase: false);
        }

        public static int FindArray(string[] arr, string val, bool isEgnoreCase)
        {
            int result = -1;
            if (arr == null)
            {
                return result;
            }
            int num = arr.Length;
            for (int i = 0; i < num; i++)
            {
                if (isEgnoreCase)
                {
                    if (arr[i].ToUpper() == val.ToUpper())
                    {
                        return i;
                    }
                }
                else if (arr[i] == val)
                {
                    return i;
                }
            }
            return result;
        }

        public static int FindArrayLike(string[] arr, string val)
        {
            int num = arr.Length;
            int length = val.Length;
            for (int i = 0; i < num; i++)
            {
                string text = arr[i];
                if (text.Length > length)
                {
                    text = text.Substring(0, length);
                }
                if (text == val)
                {
                    return i;
                }
            }
            return -1;
        }

        public static object GetArrayItem(object[] arr, int index)
        {
            object result = null;
            if (arr.Length > index)
            {
                result = arr[index];
            }
            return result;
        }

        public static string GetArrayItemString(object[] arr, int index)
        {
            object arrayItem = GetArrayItem(arr, index);
            return ToString(arrayItem);
        }

        public static string Concat(DataTable tb, string ColumnName, string sep)
        {
            try
            {
                string text = "";
                return Concat(tb, ColumnName, sep, "");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string Concat(DataTable tb, string ColumnName, string sep, string filter)
        {
            try
            {
                string text = "";
                if (!tb.Columns.Contains(ColumnName))
                {
                    throw new ApplicationException($"表中未包含列{ColumnName}");
                }
                DataRow[] drs = tb.Select(filter);
                return Concat(drs, ColumnName, sep);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string Concat(DataRow[] drs, string ColumnName, string sep)
        {
            try
            {
                string text = "";
                int num = drs.Length;
                for (int i = 0; i < num; i++)
                {
                    DataRow dataRow = drs[i];
                    if (i > 0)
                    {
                        text += sep;
                    }
                    text += ToString(dataRow[ColumnName]);
                }
                return text;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataTable GetDistinct(DataTable tb, string ColumnName, string filter)
        {
            try
            {
                if (!tb.Columns.Contains(ColumnName))
                {
                    throw new ApplicationException($"表中未包含列{ColumnName}");
                }
                DataRow[] drs = tb.Select(filter);
                return GetDistinct(drs, ColumnName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataTable GetDistinct(DataRow[] drs, string ColumnName)
        {
            try
            {
                DataTable dataTable = new DataTable();
                if (drs.Length != 0)
                {
                    dataTable = drs[0].Table.Clone();
                }
                else
                {
                    Type type = typeof(string);
                    if (drs.Length != 0)
                    {
                        type = drs[0].Table.Columns[ColumnName].DataType;
                    }
                    dataTable.Columns.Add(ColumnName, type);
                }
                int num = drs.Length;
                for (int i = 0; i < num; i++)
                {
                    DataRow dataRow = drs[i];
                    string str = ToString(dataRow[ColumnName]);
                    if (dataTable.Select(ColumnName + "='" + str + "'").Length == 0)
                    {
                        dataTable.ImportRow(drs[i]);
                    }
                }
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static DataView GetDistinct(DataView dv, string ColumnName)
        {
            try
            {
                DataTable dataTable = new DataTable();
                Type type = typeof(string);
                if (dv.Count > 0 && dv[0].Row[ColumnName].GetType() != typeof(DBNull))
                {
                    type = dv[0].Row[ColumnName].GetType();
                }
                dataTable.Columns.Add(ColumnName, type);
                int count = dv.Count;
                for (int i = 0; i < count; i++)
                {
                    string text = ToString(dv[i].Row[ColumnName]);
                    if (dataTable.Select(ColumnName + "='" + text + "'").Length == 0)
                    {
                        DataRow dataRow = dataTable.NewRow();
                        dataRow[ColumnName] = text;
                        dataTable.Rows.Add(dataRow);
                    }
                }
                return new DataView(dataTable);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GetDistinctStr(DataTable tb, string ColumnName, string filter, string sep)
        {
            try
            {
                if (!tb.Columns.Contains(ColumnName))
                {
                    throw new ApplicationException($"表中未包含列{ColumnName}");
                }
                DataRow[] drs = tb.Select(filter);
                return GetDistinctStr(drs, ColumnName, sep);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GetDistinctStr(DataRow[] drs, string ColumnName, string sep)
        {
            try
            {
                DataTable distinct = GetDistinct(drs, ColumnName);
                return Concat(distinct, ColumnName, sep);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string ToEnumString(object objValue, Type enumType)
        {
            string result = "";
            string b = ToString(objValue);
            foreach (object value in Enum.GetValues(enumType))
            {
                if (((int)value).ToString() == b)
                {
                    result = value.ToString();
                    break;
                }
            }
            return result;
        }

        public static string ToEnumDescription(object objValue, Type enumType)
        {
            string result = "";
            string b = ToString(objValue);
            foreach (object value in Enum.GetValues(enumType))
            {
                if (((int)value).ToString() == b)
                {
                    FieldInfo field = value.GetType().GetField(value.ToString());
                    DescriptionAttribute[] array = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), inherit: false);
                    result = ((array.Length == 0) ? value.ToString() : array[0].Description);
                    break;
                }
            }
            return result;
        }

        public static int ToEnumIndex(object objValue, Type enumType)
        {
            int result = -1;
            string b = ToString(objValue);
            foreach (object value in Enum.GetValues(enumType))
            {
                if (value.ToString() == b)
                {
                    result = (int)value;
                    break;
                }
            }
            return result;
        }

        public static bool IsDateStartLessThanEnd(object StartDate, object EndDate)
        {
            try
            {
                bool flag = true;
                object obj = ToDate(StartDate);
                object obj2 = ToDate(EndDate);
                if (obj == DBNull.Value || obj2 == DBNull.Value)
                {
                    return true;
                }
                DateTime t = (DateTime)obj;
                DateTime t2 = (DateTime)obj2;
                return t <= t2;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void DataRowCopy(DataRow drSrc, DataRow drDst, DataTable tbSrc, DataTable tbDst)
        {
            try
            {
                DataRowCopy(drSrc, drDst, tbSrc, tbDst, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void DataRowCopy(DataRow drSrc, DataRow drDst, DataTable tbSrc, DataTable tbDst, string[] ExceptField)
        {
            try
            {
                foreach (DataColumn column in tbDst.Columns)
                {
                    if (tbSrc.Columns.Contains(column.ColumnName) && FindArray(ExceptField, column.ColumnName, isEgnoreCase: true) < 0)
                    {
                        drDst[column.ColumnName] = drSrc[column.ColumnName];
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void DataTableCopyRow(DataTable tbSrc, DataTable tbDst)
        {
            try
            {
                foreach (DataRow row in tbSrc.Rows)
                {
                    DataRow dataRow = tbDst.NewRow();
                    DataRowCopy(row, dataRow, tbSrc, tbDst);
                    tbDst.Rows.Add(dataRow);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void DataTableCopyRow(DataView dvSrc, DataTable tbDst)
        {
            try
            {
                DataTable table = dvSrc.Table;
                foreach (DataRowView item in dvSrc)
                {
                    DataRow row = item.Row;
                    DataRow dataRow = tbDst.NewRow();
                    DataRowCopy(row, dataRow, table, tbDst);
                    tbDst.Rows.Add(dataRow);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void DataTableAddColumn(DataTable tbSrc, DataTable tbDst)
        {
            try
            {
                foreach (DataColumn column in tbSrc.Columns)
                {
                    if (!tbDst.Columns.Contains(column.ColumnName))
                    {
                        tbDst.Columns.Add(column.ColumnName, column.DataType);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void DataRowAddDecimal(DataRow drBase, DataRow drAdd, string ColumnName)
        {
            try
            {
                drBase[ColumnName] = ToDecimal(drBase[ColumnName]) + ToDecimal(drAdd[ColumnName]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void DataRowAddDecimal(DataRow drBase, DataRow drAdd, string[] ColumnNames)
        {
            try
            {
                foreach (string columnName in ColumnNames)
                {
                    DataRowAddDecimal(drBase, drAdd, columnName);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GetArrayLinkString(ArrayList ar)
        {
            return GetArrayLinkString(ar, ",");
        }

        public static string GetArrayLinkString(ArrayList ar, string separator)
        {
            string text = "";
            int count = ar.Count;
            for (int i = 0; i < count; i++)
            {
                if (text != "")
                {
                    text += separator;
                }
                text += (string)ar[i];
            }
            return text;
        }

        public static int GetIntDayOfWeek(DayOfWeek d)
        {
            try
            {
                int result = 0;
                switch (d.ToString())
                {
                    case "Monday":
                        result = 1;
                        break;
                    case "Tuesday":
                        result = 2;
                        break;
                    case "Wednesday":
                        result = 3;
                        break;
                    case "Thursday":
                        result = 4;
                        break;
                    case "Friday":
                        result = 5;
                        break;
                    case "Saturday":
                        result = 6;
                        break;
                    case "Sunday":
                        result = 7;
                        break;
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public static string FormatMM(object objMonth)
        {
            try
            {
                string text = ToString(objMonth);
                return text.PadLeft(2, "0"[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string FormatYYYYMM(object objYear, object objMonth)
        {
            try
            {
                string text = ToString(objYear);
                string text2 = ToString(objMonth);
                return text.PadLeft(4, "0"[0]) + text2.PadLeft(2, "0"[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static int GetDays(string StartDate, string FinishDate)
        {
            int result = 0;
            if (!string.IsNullOrEmpty(StartDate) && !string.IsNullOrEmpty(FinishDate))
            {
                TimeSpan timeSpan = new TimeSpan(DateTime.Parse(StartDate).Ticks);
                TimeSpan ts = new TimeSpan(DateTime.Parse(FinishDate).Ticks);
                result = timeSpan.Subtract(ts).Duration().Days;
            }
            return result;
        }





    }

}
