using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using InABox.Core;
using InABox.WPF;
using Microsoft.Xaml.Behaviors;
using Syncfusion.Windows.Controls.Grid;

namespace InABox.DynamicGrid;

public class DynamicGridDateColumn : DynamicGridMaskColumn<DateEditor> 
{
    protected override Behavior CreateBehaviour() => 
        new TextBoxDateMaskBehavior(Definition.Format);

    protected override IValueConverter CreateConverter() =>
        new DateToStringConverter(Definition.Format);
    
    protected override void UpdateButtons(object? value, DynamicGridMaskColumnButton[]? buttons)
    {
        if (value is DateTime datetime && buttons?.Any() == true)
        {
            foreach (var button in buttons)
                button.Visibility = datetime.IsEmpty()
                    ? Visibility.Collapsed
                    : Visibility.Visible;
        }
    }

    private readonly BitmapImage _clear = Wpf.Resources.delete.AsBitmapImage(20,20);

    protected override DynamicGridMaskColumnButton[]? CreateButtons()
    {
        return new DynamicGridMaskColumnButton[]
        {
            new DynamicGridMaskColumnButton()
            {
                Image = _clear,
                Clicked = (sender,args) =>
                {
                    args.Value = DateTime.MinValue;
                    sender.Visibility = Visibility.Collapsed;
                }
            }
        };
    }

    public DynamicGridDateColumn(DynamicGridColumn definition) : base(definition)
    {
    }
}