using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace Nodify.Calculator
{
    public partial class CreateFunctionDialog : Window
    {
        public string FunctionName { get; private set; } = string.Empty;
        public List<FunctionParameterInfo> InputParameters { get; } = new List<FunctionParameterInfo>();
        public List<FunctionParameterInfo> OutputParameters { get; } = new List<FunctionParameterInfo>();

        private readonly ObservableCollection<FunctionParameterInfo> _inputs = new ObservableCollection<FunctionParameterInfo>();
        private readonly ObservableCollection<FunctionParameterInfo> _outputs = new ObservableCollection<FunctionParameterInfo>();

        public CreateFunctionDialog()
        {
            InitializeComponent();
            InputParamsGrid.ItemsSource = _inputs;
            OutputParamsGrid.ItemsSource = _outputs;
        }

        private void AddInput_Click(object sender, RoutedEventArgs e)
        {
            _inputs.Add(new FunctionParameterInfo { Name = $"param{_inputs.Count + 1}", Type = "string" });
        }

        private void AddOutput_Click(object sender, RoutedEventArgs e)
        {
            _outputs.Add(new FunctionParameterInfo { Name = $"result{_outputs.Count + 1}", Type = "string" });
        }

        private void OnCreateClick(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(FunctionNameBox.Text))
            {
                MessageBox.Show("Please enter a function name.", "Validation", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            FunctionName = FunctionNameBox.Text.Trim();
            InputParameters.AddRange(_inputs);
            OutputParameters.AddRange(_outputs);
            DialogResult = true;
        }

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