using InABox.Wpf;
using InABox.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;

namespace InABox.DynamicGrid;

public class DynamicGridColumnFilterUIButton : Button
{
    private BitmapImage _filterImage = Wpf.Resources.filter.AsBitmapImage();
    private BitmapImage _filterSetImage = Wpf.Resources.filter_set.AsBitmapImage();

    private readonly IDynamicGridColumnFilter Filter;

    public DynamicGridColumnFilterUIButton(IDynamicGridColumnFilter filter)
    {
        Filter = filter;

        Content = new Image { Source = filter.IsFiltered() ? _filterSetImage : _filterImage };
        Background = Colors.Transparent.ToBrush();
        BorderThickness = new(0);
        MaxHeight = 23;

        var btnStyle = new Style(typeof(Button));
        btnStyle.AddSetter(Button.TemplateProperty, TemplateGenerator.CreateControlTemplate(typeof(Button), () =>
        {
            var border = new Border
            {
            };
            border.Bind(Border.BackgroundProperty, this, x => x.Background);
            var content = new ContentPresenter();
            content.Bind(ContentPresenter.ContentProperty, this, x => x.Content);
            border.Child = content;
            return border;
        }));
        Style = btnStyle;
        Click += (o, e) =>
        {
            FilterButton_Click();
        };

        Loaded += DynamicGridColumnFilterUIButton_Loaded;
        Unloaded += DynamicGridColumnFilterUIButton_Unloaded;
    }

    private void DynamicGridColumnFilterUIButton_Loaded(object sender, RoutedEventArgs e)
    {
        Filter.FilterChanged += Filter_FilterChanged;
    }

    private void DynamicGridColumnFilterUIButton_Unloaded(object sender, RoutedEventArgs e)
    {
        Filter.FilterChanged -= Filter_FilterChanged;
    }

    private void Filter_FilterChanged(IDynamicGridColumnFilter obj)
    {
        Content = new Image
        {
            Source = Filter.IsFiltered() ? _filterSetImage : _filterImage
        };
    }

    private void FilterButton_Click()
    {
        var popup = new Popup
        {
            AllowsTransparency = true
        };

        var border = new Border
        {
            Width = 250,
            Height = 400,
            Background = Colors.White.ToBrush(),
            BorderBrush = Colors.Gray.ToBrush(),
            BorderThickness = new(1.0),
            Margin = new(5.0)
        };
        border.Effect = new DropShadowEffect
        {
            BlurRadius = 8,
            ShadowDepth = 0,
            Opacity = 0.5
        };
        border.Padding = new(5.0);

        var grid = new Grid();
        grid.AddRow(GridUnitType.Star);
        grid.AddRow(GridUnitType.Auto);

        grid.AddColumn(GridUnitType.Star);

        var clearButton = new Button
        {
            Content = "Clear Filter",
            Padding = new(5.0),
            Margin = new(0.0, 5.0, 0.0, 0.0)
        };
        clearButton.Click += (o, e) =>
        {
            Filter.ClearFilter();
        };

        var filterControl = Filter.CreateControl();

        grid.AddChild(filterControl, 0, 0);
        grid.AddChild(clearButton, 1, 0);

        border.Child = grid;

        popup.Child = border;

        popup.StaysOpen = false;
        popup.PlacementTarget = this;
        popup.Placement = PlacementMode.Left;
        popup.IsOpen = true;
    }
}
