using InABox.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace InABox.DynamicGrid;

public class DynamicGridColumnsEditorControl : DynamicEditorControl<DynamicGridColumns, DynamicGridColumnsEditorAttribute>
{
    private TextBox TextBox { get; set; }
    private Button Edit { get; set; }

    private DynamicGridColumns? _columns;
    private DynamicGridColumns? Columns
    {
        get => _columns;
        set
        {
            _columns = value;
            if(value is not null)
            {
                TextBox.Text = string.Join("; ", value.Select(x => x.ColumnName));
            }
            else
            {
                TextBox.Text = "";
            }
        }
    }

    private Type? _columnsType { get; set; }

    public Type? ColumnsType
    {
        get => _columnsType;
        set
        {
            if(_columnsType != value)
            {
                if(value is not null)
                {
                    var cols = DefaultColumns.GetDefaultColumns(value);
                    Columns = [.. cols.Select(DynamicGridColumn.FromCoreGridColumn)];
                }
                else
                {
                    Columns = null;
                }
            }
            _columnsType = value;
            if(Edit != null)
            {
                Edit.IsEnabled = value != null;
            }
            CheckChanged();
        }
    }

    public override int DesiredHeight()
    {
        return 25;
    }

    public override int DesiredWidth()
    {
        return int.MaxValue;
    }

    public override void SetColor(Color color)
    {
        TextBox.Background = new SolidColorBrush(color);
    }

    public override void SetFocus()
    {
        TextBox.Focus();
    }
    public override void Configure()
    {
    }

    protected override FrameworkElement CreateEditor()
    {
        var grid = new Grid
        {
            VerticalAlignment = VerticalAlignment.Stretch,
            HorizontalAlignment = HorizontalAlignment.Stretch
        };
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });

        TextBox = new TextBox
        {
            VerticalAlignment = VerticalAlignment.Stretch,
            VerticalContentAlignment = VerticalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Stretch,
            Margin = new Thickness(0, 0, 0, 0),
            IsEnabled = false
        };
        TextBox.SetValue(Grid.ColumnProperty, 0);
        TextBox.SetValue(Grid.RowProperty, 0);

        Edit = new Button
        {
            VerticalAlignment = VerticalAlignment.Stretch,
            VerticalContentAlignment = VerticalAlignment.Center,
            HorizontalAlignment = HorizontalAlignment.Stretch,
            Margin = new Thickness(5, 1, 0, 1),
            Width = 45,
            Content = "Edit",
            Focusable = false,
            IsEnabled = ColumnsType != null
        };
        Edit.SetValue(Grid.ColumnProperty, 1);
        Edit.SetValue(Grid.RowProperty, 0);
        Edit.Click += EditButton_Click;

        grid.Children.Add(TextBox);
        grid.Children.Add(Edit);

        ColumnsType = EditorDefinition.Type;

        return grid;
    }

    private void EditButton_Click(object sender, RoutedEventArgs e)
    {
        if (ColumnsType is null || Columns is null) return;

        var schema = new DynamicGridObjectColumnSchema(ColumnsType);

        var editor = new DynamicGridColumnsEditor(schema, ColumnsType) { WindowStartupLocation = WindowStartupLocation.CenterScreen };

        editor.Columns.AddRange(Columns);

        if (editor.ShowDialog().Equals(true))
        {
            Columns = editor.Columns;
        }
    }

    protected override DynamicGridColumns RetrieveValue()
    {
        return Columns ?? new();
    }

    protected override void UpdateValue(DynamicGridColumns value)
    {
        if (ColumnsType != null)
            Columns = value;
        else
            Columns = null;
    }
}
