using InABox.Core;
using InABox.Wpf.DynamicGrid.Editors.ColumnsEditor;
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 ColumnsEditorControl : DynamicEditorControl<string, ColumnsEditor>
{
    private TextBox TextBox { get; set; }
    private Button Edit { get; set; }

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

    private Type? _columnsType { get; set; }

    public Type? ColumnsType
    {
        get => _columnsType;
        set
        {
            if(_columnsType != value)
            {
                if(value is not null)
                {
                    Columns = Core.Columns.Create(value, ColumnTypeFlags.IncludeVisible);
                }
                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 grid = new ColumnsEditorGrid(ColumnsType, Columns);
        grid.Refresh(true, true);
        var dlg = new DynamicContentDialog(grid)
        {
            Title = "Edit Columns",
            Width = 400,
            Height = 600,
            WindowStartupLocation = WindowStartupLocation.CenterScreen
        };
        grid.OnChanged += (o, e) =>
        {
            dlg.CanSave = true;
        };
        if(dlg.ShowDialog() == true)
        {
            Columns = grid.GetColumns();
            CheckChanged();
        }
    }

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

    protected override void UpdateValue(string value)
    {
        if (ColumnsType != null)
            Columns = Serialization.Deserialize(typeof(Columns<>).MakeGenericType(ColumnsType), value) as IColumns
                ?? Core.Columns.Create(ColumnsType, ColumnTypeFlags.IncludeVisible);
        else
            Columns = null;
    }
}
