using System;
using System.Collections.Generic;
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;

namespace InABox.DynamicGrid;

public class DynamicGridTimeOfDayColumn : DynamicGridMaskColumn<TimeOfDayEditor>
{
    protected override Behavior CreateBehaviour() => 
        new TextBoxTimeSpanMaskBehavior(Definition?.Format);

    protected override IValueConverter CreateConverter() =>
        new TimeSpanToStringConverter(Definition?.Format);
    
    protected override void UpdateButtons(object? value, DynamicGridMaskColumnButton[]? buttons)
    {
        if (value is TimeSpan timespan && buttons?.Any() == true)
        {
            foreach (var button in buttons)
                button.Visibility = timespan == TimeSpan.Zero
                    ? 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 = TimeSpan.Zero;
                    sender.Visibility = Visibility.Collapsed;
                }
            }
        };
    }

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