using System;
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 DynamicGridUniqueCodeColumn : DynamicGridEditorColumn<UniqueCodeEditor,GridTemplateColumn, TreeGridTemplateColumn>
{


    private Tuple<DataTemplate, DataTemplate> GetTemplates(GridColumnBase column, UniqueCodeEditor editor, string mapping)
    {
        var cell = TemplateGenerator.CreateDataTemplate
        (
            () =>
            {
                var result = new Label();
                result.HorizontalContentAlignment = column.TextAlignment == TextAlignment.Left
                    ? HorizontalAlignment.Left
                    : column.TextAlignment == TextAlignment.Center
                        ? HorizontalAlignment.Center
                        : HorizontalAlignment.Right;
                var binding = new Binding()
                {
                    Path = new PropertyPath(mapping),
                };
                result.SetBinding(Label.ContentProperty, binding);
                return result;
            }
        );

        var edit = TemplateGenerator.CreateDataTemplate
        (
            () =>
            {
                var textbox = new TextBox();
                textbox.CharacterCasing = CharacterCasing.Upper;
                textbox.TextAlignment = column.TextAlignment;
                textbox.SetBinding(TextBox.TextProperty, new Binding()
                {
                    Path = new PropertyPath(mapping)
                });
                
                textbox.SetValue(Grid.ColumnSpanProperty, 2);
                textbox.Padding = new Thickness(2, 0, 0, 0);
                textbox.VerticalContentAlignment = VerticalAlignment.Center;
                textbox.PreviewTextInput += (sender, args) => textbox.Tag = true;
                textbox.TextChanged += (sender, args) =>
                {
                    if (Equals(textbox.Tag, false))
                    {
                        textbox.SelectAll();
                        textbox.Tag = true;
                    }
                };
                textbox.SetValue(FocusManagerHelper.FocusedElementProperty, true);
                
                return textbox;
            }
        );
        return new(cell, edit);
    }

    protected override void Configure(TreeGridTemplateColumn column, UniqueCodeEditor editor)
    {
        var (cell, edit) = GetTemplates(column, editor, TreeMappingName);
        column.CellTemplate = cell;
        column.EditTemplate = edit;
    }

    protected override void Configure(GridTemplateColumn column, UniqueCodeEditor editor)
    {
        var (cell, edit) = GetTemplates(column, editor, MappingName);
        column.CellTemplate = cell;
        column.EditTemplate = edit;
    }
    
    public DynamicGridUniqueCodeColumn(DynamicGridColumn definition) : base(definition)
    {
    }
}