using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Data.Converters;
using Avalonia.Layout;
using Avalonia.Media;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InABox.Avalonia.Components;

public class AvaloniaDataGridDateColumn<TEntity> : AvaloniaDataGridColumn<TEntity, DateTime>
{
    public string Format { get; set; } = "dd MMM yy";

    public bool BlankIfZero { get; set; } = true;

    public AvaloniaDataGridDateColumn()
    {
        Width = GridLength.Auto;
        Alignment = TextAlignment.Center;
    }

    public override DataGridColumn CreateColumn()
    {
        var column = CreateColumn<DataGridTemplateColumn>();
        column.CellTemplate = new FuncDataTemplate<object?>((value, scope) =>
        {
            var label = new TextBlock
            {
                Margin = new(2, 0, 0, 0),
                TextAlignment = Alignment,
                VerticalAlignment = VerticalAlignment.Center
            };
            label.Bind(TextBlock.TextProperty, new Binding(ColumnName)
            {
                Converter = new FuncValueConverter<DateTime, string>(x => x == DateTime.MinValue && BlankIfZero ? "" : x.ToString(Format))
            });
            return label;
        });
        return column;
    }
}
