using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using InABox.Core;
using InABox.WPF;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.TreeGrid;

namespace InABox.DynamicGrid;

public class DynamicGridColorColumn : DynamicGridEditorColumn<ColorEditor, GridImageColumn, TreeGridTemplateColumn>
{
    private int _height = 25;
    private readonly int _width;

    
    protected override void UpdateBinding(GridImageColumn column)
    {
        column.ValueBinding = GetBinding(MappingName);
    }

    private Binding GetBinding(string mapping)
    {
        return new Binding
        {
            Path = new PropertyPath(mapping),
            Converter = new StringToColorImageConverter(Math.Max(25,_width) - 8, Math.Max(25,_height) - 8)
        };
    }

    protected override void Configure(GridImageColumn column, ColorEditor editor)
    {
        column.ImageHeight = Math.Max(25,_height) - 8;
        column.ImageWidth = Math.Max(25,_width) - 8;
        column.Padding = new Thickness(4);
    }

    protected override void Configure(TreeGridTemplateColumn column, ColorEditor editor)
    {
        var converter = new StringToColorImageConverter(Math.Max(25, _width) - 8, Math.Max(25, _height) - 8);

        column.Padding = new Thickness(4);
        column.CellTemplate = TemplateGenerator.CreateDataTemplate(() =>
        {
            var image = new Image
            {
                Height = Math.Max(25, _height) - 8,
                Width = Math.Max(25, _width) - 8
            };
            image.SetBinding(Image.SourceProperty, GetBinding(TreeMappingName));
            return image;
        });
    }

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

    public override void UpdateUIComponent(IDynamicGridGridUIComponent component)
    {
        base.UpdateUIComponent(component);
        _height = Math.Max(25, component.RowHeight);
    }

    public DynamicGridColorColumn(DynamicGridColumn definition) : base(definition)
    {
        _width = Math.Max(25, definition.Width);
    }
}