using System.Collections.Generic;
using System.Windows;
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 DynamicGridMemoColumn : DynamicGridEditorColumn<MemoEditor, GridTextColumn, TreeGridTextColumn>
{
    private void UpdateBinding(GridColumnBase column, string mapping)
    {
        var binding = new Binding
        {
            Path = new PropertyPath(mapping), 
            Converter = new FuncConverter<object?, string>(
                x => x is string[] strArr ? string.Join('\n', strArr) : (x?.ToString() ?? ""),
                x => x)
        };
        column.DisplayBinding = binding;
        column.ValueBinding = binding;
        column.Padding = new Thickness(2);
    }

    protected override void UpdateBinding(GridTextColumn column)
    {
        base.UpdateBinding(column);
        UpdateBinding(column, MappingName);
    }

    protected override void UpdateBinding(TreeGridTextColumn column)
    {
        base.UpdateBinding(column);
        UpdateBinding(column, TreeMappingName);
    }

    protected override void Configure(GridTextColumn column, MemoEditor editor)
    {
        column.TextWrapping = TextWrapping.Wrap;
        column.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
    }
    protected override void Configure(TreeGridTextColumn column, MemoEditor editor)
    {
        column.TextWrapping = TextWrapping.Wrap;
        column.UpdateTrigger = UpdateSourceTrigger.PropertyChanged;
    }
    
    public override bool VariableHeight => true;

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