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 SortOrderEditorControl : DynamicEditorControl<string, SortOrderEditor>
{
    private TextBox TextBox { get; set; }
    private Button Edit { get; set; }

    private ISortOrder? _sortOrder;
    private ISortOrder? SortOrder
    {
        get => _sortOrder;
        set
        {
            _sortOrder = value;
            if(value is not null)
            {
                TextBox.Text = value.AsOData();
            }
            else
            {
                TextBox.Text = "";
            }
        }
    }

    private Type? _sortOrderType { get; set; }

    public Type? SortOrderType
    {
        get => _sortOrderType;
        set
        {
            if(_sortOrderType != value)
            {
                SortOrder = null;
            }
            _sortOrderType = 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 = SortOrderType != null
        };
        Edit.SetValue(Grid.ColumnProperty, 1);
        Edit.SetValue(Grid.RowProperty, 0);
        Edit.Click += EditButton_Click;

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

        SortOrderType = EditorDefinition.Type;

        return grid;
    }

    private void EditButton_Click(object sender, RoutedEventArgs e)
    {
        if (SortOrderType is null || SortOrder is null) return;
    }

    protected override string RetrieveValue()
    {
        return SortOrder != null ? Serialization.Serialize(SortOrder) : "";
    }

    protected override void UpdateValue(string value)
    {
        if (SortOrderType != null)
            SortOrder = Serialization.Deserialize(typeof(SortOrder<>).MakeGenericType(SortOrderType), value) as ISortOrder;
        else
            SortOrder = null;
    }
}
