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

namespace InABox.DynamicGrid
{
    /// <summary>
    /// Interaction logic for FilterEditorWindow.xaml
    /// </summary>
    public partial class FilterEditorWindow : ThemableWindow
    {
        private BaseFilterNode? FilterNode;
        private FilterEditorConfiguration? Configuration;

        public FilterEditorWindow(FilterEditorConfiguration? configuration = null)
        {
            InitializeComponent();
            Configuration = configuration;
        }

        public void SetFilter<T>(Filter<T> filter)
        {
            FilterNode = new FilterNode<T>(filter, configuration: Configuration);
            FilterNode.Margin = new Thickness(5);

            Content.Content = FilterNode;

            Title = $"Edit {typeof(T).Name} Filter";
        }
        public Filter<T>? GetFilter<T>()
        {
            return (FilterNode as FilterNode<T>)?.GetFilter();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        public static bool Execute<T>(ref Filter<T>? filter, FilterEditorConfiguration? config = null)
        {
            var window = new FilterEditorWindow(configuration: config);
            window.SetFilter(filter ?? new());
            if(window.ShowDialog() == true)
            {
                filter = window.GetFilter<T>();
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
