using InABox.Configuration;
using InABox.Core;
using InABox.DynamicGrid;
using InABox.WPF;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace InABox.Wpf;

public class FilterButton<T> : Button
{
    public event DynamicGridFilterComponent<T>.FilterSelectedHandler? OnFiltersSelected
    {
        add { Component.OnFiltersSelected += value; }
        remove { Component.OnFiltersSelected -= value; }
    }

    public event DynamicGridFilterComponent<T>.FilterRefreshHandler? OnFilterRefresh
    {
        add { Component.OnFilterRefresh += value; }
        remove { Component.OnFilterRefresh -= value; }
    }

    public List<DynamicGridFilterComponentBuiltInFilter<T>> BuiltInFilters => Component.BuiltInFilters;

    private class ButtonComponent : DynamicGridFilterComponent<T>
    {
        private FilterButton<T> Button;

        public ButtonComponent(
            FilterButton<T> button,
            IConfiguration<CoreFilterDefinitions> globalConfiguration,
            IConfiguration<CoreFilterDefinitions> userConfiguration) : base(globalConfiguration, userConfiguration)
        {
            Button = button;
        }

        protected override void UpdateButtonText(System.Drawing.Bitmap image, string text)
        {
            Button.Update(image, text);
        }

        public void Click()
        {
            DoFilter();
        }
    }

    private ButtonComponent Component;

    public FilterButton(
        IConfiguration<CoreFilterDefinitions> globalConfiguration,
        IConfiguration<CoreFilterDefinitions> userConfiguration)
    {
        Component = new(this, globalConfiguration, userConfiguration);

        SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Gray));
        SetValue(BorderThicknessProperty, new Thickness(0.75));
        Height = 30;
        Padding = new(2);

        Update(Wpf.Resources.filter, "");
    }

    public void SetSettings(DynamicGridSelectedFilterSettings settings, bool refresh)
    {
        Component.SetSettings(settings, refresh);
    }

    private void Update(System.Drawing.Bitmap image, string text)
    {
        var img = new Image
        {
            Source = image.AsBitmapImage(),
            Margin = new()
        };

        if (!string.IsNullOrEmpty(text))
        {
            var stackPnl = new StackPanel();
            stackPnl.Orientation = Orientation.Horizontal;
            //stackPnl.Margin = new Thickness(2);

            stackPnl.MaxWidth = double.MaxValue;
            var lbl = new Label();
            lbl.Content = text;
            lbl.VerticalAlignment = VerticalAlignment.Stretch;
            lbl.VerticalContentAlignment = VerticalAlignment.Center;
            lbl.Margin = new Thickness(2, 0, 0, 0);
            lbl.ToolTip = ToolTip;

            stackPnl.Children.Add(img);
            stackPnl.Children.Add(lbl);

            Content = stackPnl;
        }
        else
        {
            Content = img;
        }
    }

    protected override void OnClick()
    {
        base.OnClick();

        Component.Click();
    }
    public Filter<T>? GetFilter()
    {
        return Component.GetFilter();
    }
}