using System;
using System.Linq.Expressions;
using InABox.Core;
namespace InABox.DynamicGrid;

public class DynamicTextColumn : DynamicActionColumn
{
    public delegate object? GetTextDelegate(CoreRow? row);

    public GetTextDelegate Text { get; protected set; }
        
    public Alignment Alignment { get; set; }
    
    public string? Format { get; set; }

    protected DynamicTextColumn()
    {
        Alignment = Alignment.MiddleCenter;
        VerticalHeader = false;
    }

    public DynamicTextColumn(GetTextDelegate text, ActionDelegate? action = null) : this()
    {
        Text = text;
        Action = action;
    }

    public override object? Data(CoreRow? row)
    {
        var result = Text?.Invoke(row);
        if (!string.IsNullOrWhiteSpace(Format))
            return String.Format($"{{0:{Format}}}", result);
        return result;
    }
}

public class DynamicTextColumn<T> : DynamicTextColumn
{
    public Expression<Func<T, object>> Property { get; set; }

    public DynamicTextColumn(Expression<Func<T, object>> property, ActionDelegate? action = null)
    {
        Text = GetText;
        Action = action;
        Property = property;
    }

    private object GetText(CoreRow? row)
    {
        return row?.Get(Property) ?? "";
    }
}