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

namespace InABox.DynamicGrid;

public class DynamicGridMemoColumn<TEntity> : DynamicGridEditorColumn<TEntity, MemoEditor, GridTextColumn, TreeGridTextColumn>
    where TEntity : BaseObject
{
    private void UpdateBinding(GridColumnBase column, string mapping)
    {
        var prop = CoreUtils.GetProperty<TEntity>(Definition.ColumnName);
        var binding = new Binding
        {
            Path = new PropertyPath(mapping), 
            Converter = prop.PropertyType == typeof(string[]) 
                ? new StringArrayConverter() 
                : null
        };
        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)
    {
    }
}