using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Hotline.Share.Tools;
public static class ListExtensions
{
    public static bool IsNullOrEmpty<T>(this List<T> value)
    {
        return value == null || value.Count == 0;
    }

    public static bool NotNullOrEmpty<T>(this List<T> value)
    {
        return value != null && value.Count != 0;
    }

    public static bool NotNullOrEmpty<T>(this IList<T> value)
    {
        return value != null && value.Count != 0;
    }

    public static bool IsNullOrEmpty<T>(this IList<T> value)
    {
        return value == null || value.Count == 0;
    }

    public static bool NotNullOrEmpty<T>(this IReadOnlyList<T> value)
    {
        return value != null && value.Count != 0;
    }

    private static void AddSumLineForDictionaryList(IList<Dictionary<string, object>> list, string fieldName)
    {
        var sumRow = new Dictionary<string, object>();

        foreach (var key in list.First().Keys)
        {
            sumRow[key] = key == fieldName ? "合计" : 0;
        }

        foreach (var item in list)
        {
            foreach (var key in item.Keys)
            {
                if (key != fieldName && item[key] is IConvertible) 
                {
                    try
                    {
                        sumRow[key] = Convert.ToDecimal(sumRow[key]) + Convert.ToDecimal(item[key]);
                    }
                    catch
                    {
                        sumRow[key] = item[key]; 
                    }
                }
            }
        }

        list.Add(sumRow);
    }

    public static void AddSumLine<T>(this IList<T> list, string fieldName)
    {
        if (list == null || list.Count == 0) return;
        var type = typeof(T);
        if (typeof(T) == typeof(Dictionary<string, object>))
        {
            AddSumLineForDictionaryList(list as IList<Dictionary<string, object>>, fieldName);
            return;
        }
        var properties = type.GetProperties();
        var sumLine = Activator.CreateInstance<T>();
        foreach (var property in properties)
        {
            if (!property.CanWrite) continue;
            if (property.Name == fieldName)
            {
                property.SetValue(sumLine, "合计");
            }
            else if (property.PropertyType == typeof(int))
            {
                property.SetValue(sumLine, list.Sum(m => (int)property.GetValue(m)));
            }
            else if (property.PropertyType == typeof(decimal))
            {
                property.SetValue(sumLine, list.Sum(m => (decimal)property.GetValue(m)));
            }
            else if (property.PropertyType == typeof(double?))
            {
                property.SetValue(sumLine, list.Sum(m => (double?)property.GetValue(m)));
            }
            else if (property.PropertyType == typeof(double))
            {
                property.SetValue(sumLine, list.Sum(m => (double?)property.GetValue(m)));
            }
        }
        list.Add(sumLine);
    }
}
