using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using InABox.Core;
using InABox.Wpf;

namespace InABox.DynamicGrid;

public class SubQueryNode : ValueNode
{
    private TextBox Display;
    private Button Edit;

    private Type? QueryType;
    private string? Column;
    private IFilter? Filter;

    private FilterEditorConfiguration Configuration;

    public override object? Value
    {
        get => GetSubQuery();
        set
        {
            if (value is not ISubQuery subQuery)
            {
                QueryType = null;
                Column = null;
                Filter = null;
            }
            else
            {
                QueryType = subQuery.GetQueryType();
                Column = subQuery.GetColumn().Property;
                Filter = subQuery.GetFilter();
            }
            Display.Text = value?.ToString() ?? "";
        }
    }
    
            
    public override bool IsEnabled
    {
        get => Edit.Visibility == Visibility.Visible;
        set
        {
            Edit.Visibility = value 
                ? Visibility.Visible 
                : Visibility.Collapsed;
            Display.Background = value
                ? new SolidColorBrush(Colors.LightYellow)
                : new SolidColorBrush(Colors.LightGray);
        }
    }

    public SubQueryNode(FilterEditorConfiguration configuration)
    {
        Configuration = configuration;

        var panel = new DockPanel();

        Display = new TextBox {
            VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
            Background = new SolidColorBrush(Colors.LightYellow),
            Margin = new Thickness(0, 0, 5, 0),
            IsEnabled = false
        };
        Edit = new Button
        {
            Content = "Edit",
            Padding = new Thickness(5)
        };
        Edit.Click += Edit_Click;

        Display.SetValue(DockPanel.DockProperty, Dock.Left);
        Edit.SetValue(DockPanel.DockProperty, Dock.Right);

        panel.Children.Add(Edit);
        panel.Children.Add(Display);

        Content = panel;
    }

    private void Edit_Click(object sender, RoutedEventArgs e)
    {
        var window = new SubQueryNodePopup(Configuration);
        window.QueryType = QueryType;
        window.Column = Column;
        window.Filter = Filter ?? (QueryType != null ? (Activator.CreateInstance(typeof(Filter<>).MakeGenericType(QueryType)) as IFilter) : null);
        if(window.ShowDialog() == true)
        {
            QueryType = window.QueryType;
            Column = window.Column;
            Filter = window.Filter;
            Display.Text = GetSubQuery()?.ToString() ?? "";
        }
    }

    private object? GetSubQuery()
    {
        if (QueryType is null)
            return null;

        var column = Column is null ? null : Activator.CreateInstance(typeof(Column<>).MakeGenericType(QueryType), Column);

        return Activator.CreateInstance(typeof(SubQuery<>).MakeGenericType(QueryType), Filter, column);
    }
}