using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using InABox.Core;
using InABox.WPF;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.TreeGrid;

namespace InABox.DynamicGrid;

public class DynamicGridCheckBoxColumn : DynamicGridEditorColumn<CheckBoxEditor, GridTemplateColumn, TreeGridTemplateColumn>
{
    private readonly BitmapImage ticked = Wpf.Resources.Bullet_Tick.AsBitmapImage();
    private readonly BitmapImage unticked = null; //Wpf.Resources.tick.AsGrayScale().AsBitmapImage();

    private DataTemplate CreateCellTemplate(string mapping)
    {
        return TemplateGenerator.CreateDataTemplate
        (
            () =>
            {
                var result = new Image() { Margin = new Thickness(2) };
                result.SetBinding(
                    Image.SourceProperty, 
                    new Binding()
                    {
                        Path = new PropertyPath(mapping), 
                        Converter = new BoolToImageConverter()
                    }
                );
                return result;
            }
        );
    }
    private DataTemplate CreateEditTemplate(string mapping)
    {
        return TemplateGenerator.CreateDataTemplate
        (
            () =>
            {
                DockPanel dock = new DockPanel();
                dock.Children.Add(CreateImage(mapping, ticked, Visibility.Visible, Visibility.Collapsed, BindingMode.TwoWay));
                dock.Children.Add(CreateImage(mapping, unticked, Visibility.Collapsed, Visibility.Visible, BindingMode.TwoWay));
                Border border = new Border()
                {
                    Background = new SolidColorBrush(Colors.White),
                    Child = dock
                };
                
                border.PreviewMouseDown += (sender, args) =>
                {
                    if (sender is Border b && b.Child is DockPanel d)
                        ToggleImages(d);
                    args.Handled = true;
                };
                border.KeyUp += (sender, args) =>
                {
                    if (args.Key == Key.Space && sender is Border b && b.Child is DockPanel d)
                        ToggleImages(d);
                    args.Handled = true;
                };
                return border;
            }
        );
    }

    
    protected override void Configure(GridTemplateColumn column, CheckBoxEditor editor)
    {
        column.CellTemplate = CreateCellTemplate(MappingName);
        column.EditTemplate = CreateEditTemplate(MappingName);
        column.SetCellBoundValue = false;
    }
    protected override void Configure(TreeGridTemplateColumn column, CheckBoxEditor editor)
    {
        column.CellTemplate = CreateCellTemplate(TreeMappingName);
        column.EditTemplate = CreateEditTemplate(TreeMappingName);
        column.SetCellBoundValue = false;
    }

    private static void ToggleImages(DockPanel d)
    {
        var t = d.Children.OfType<Image>().First();
        var u = d.Children.OfType<Image>().Last();
        t.Visibility = t.Visibility == Visibility.Visible
            ? Visibility.Collapsed
            : Visibility.Visible;
        u.Visibility = u.Visibility == Visibility.Visible
            ? Visibility.Collapsed
            : Visibility.Visible;
    }

    private Image CreateImage(string mapping, BitmapImage source, Visibility truevalue, Visibility falsevalue, BindingMode mode)
    {
        var image = new Image() { Source = source, Margin = new Thickness(2) };
        image.SetValue(DockPanel.DockProperty,Dock.Top);
        image.SetBinding(
            Image.VisibilityProperty, 
            new Binding()
            {
                Path = new PropertyPath(mapping), 
                Converter = new WPF.BooleanToVisibilityConverter() { TrueValue = truevalue, FalseValue = falsevalue }, 
                Mode = mode
            }
        );
        image.PreviewMouseDown += (sender, args) =>
        {
            if (sender is Image i)
                i.Visibility = i.Visibility == truevalue
                    ? falsevalue
                    : truevalue;
            args.Handled = true;
        };
        image.KeyUp += (sender, args) =>
        {
            if (args.Key == Key.Space && sender is Image i)
                i.Visibility = i.Visibility == truevalue
                    ? falsevalue
                    : truevalue;
            args.Handled = true;
        };
        return image;
    }

    public override bool Filtered => false;
    public override bool VariableWidth => false;

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