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

namespace InABox.DynamicGrid
{
    public class ExpressionEditorControl : DynamicEditorControl<string, ExpressionEditor>
    {
        
        static ExpressionEditorControl()
        {
            //DynamicEditorControlFactory.Register<ExpressionEditorControl, ExpressionEditor>();
        }
        
        private TextBox TextBox = null!;
        private Button Edit = null!;

        public ExpressionEditorControl() { }
        
        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
            };
            Edit.SetValue(Grid.ColumnProperty, 1);
            Edit.SetValue(Grid.RowProperty, 0);
            Edit.Click += EditButton_Click;

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

            return grid;
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            var variables = EditorDefinition.GetVariables(Host.GetItems());
            var window = new ExpressionEditorWindow(variables)
            {
                Expression = TextBox.Text
            };
            if (window.ShowDialog() == true)
            {
                TextBox.Text = window.Expression;
                CheckChanged();
            }
        }

        protected override string RetrieveValue()
        {
            return TextBox.Text;
        }

        protected override void UpdateValue(string value)
        {
            TextBox.Text = value;
        }
    }
}
