using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using InABox.Core;
using Syncfusion.Data;

namespace InABox.DynamicGrid;

public class DynamicGridIntegerAggregate : ISummaryAggregate
{
    public int Count { get; private set; }

    public int Sum { get; private set; }

    public Action<IEnumerable, string, PropertyDescriptor> CalculateAggregateFunc()
    {
        return CalculateAggregate;
    }

    private void CalculateAggregate(IEnumerable items, string property, PropertyDescriptor args)
    {
        if (items is IEnumerable<DataRowView> rows)
        {
            if (string.Equals(args.Name, "Count"))
            {
                Count = rows.Count();
            }
            else if (string.Equals(args.Name, "Sum"))
            {
                Sum = 0;
                foreach (var row in rows)
                    if (row[property] is TimeSpan)
                        Sum += (int)row[property];
            }
        }
        else
        {
            Logger.Send(LogType.Error, "", $"Attempting to calculate aggregate on invalid data type '{items.GetType()}'.");
        }
    }
}