using CtrEditor.Controls;
using CtrEditor.ObjetosSim;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace CtrEditor.Windows
{
    public partial class MultiPropertyEditorWindow : Window
    {
        private List<osBase> _objects;

        public MultiPropertyEditorWindow(List<osBase> objects, Window owner)
        {
            InitializeComponent();
            _objects = objects;
            this.Owner = owner;
            InitializeGrid();
        }

        public void UpdateSelectedObjects(List<osBase> newObjects)
        {
            _objects = newObjects;
            MainGrid.Children.Clear();
            MainGrid.ColumnDefinitions.Clear();
            InitializeGrid();
        }

        private void InitializeGrid()
        {
            // Clear existing column definitions
            MainGrid.ColumnDefinitions.Clear();

            // Add column definitions for each object
            for (int i = 0; i < _objects.Count; i++)
            {
                MainGrid.ColumnDefinitions.Add(new ColumnDefinition 
                { 
                    Width = new GridLength(1, GridUnitType.Star) 
                });

                // Create a container for each object's editor
                var container = new DockPanel();
                
                // Add header with object name
                var header = new TextBlock
                {
                    Text = _objects[i].Nombre,
                    FontWeight = FontWeights.Bold,
                    Margin = new Thickness(5),
                    TextAlignment = TextAlignment.Center
                };
                DockPanel.SetDock(header, Dock.Top);
                container.Children.Add(header);

                // Add property grid
                var propertyGrid = new PanelEdicionControl();
                propertyGrid.CargarPropiedades(_objects[i]);
                container.Children.Add(propertyGrid);

                // Add separator line except for the last column
                if (i < _objects.Count - 1)
                {
                    var separator = new GridSplitter
                    {
                        Width = 5,
                        HorizontalAlignment = HorizontalAlignment.Right,
                        VerticalAlignment = VerticalAlignment.Stretch,
                        Background = SystemColors.ControlLightBrush
                    };
                    Grid.SetColumn(separator, i);
                    MainGrid.Children.Add(separator);
                }

                // Add the container to the grid
                Grid.SetColumn(container, i);
                MainGrid.Children.Add(container);
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            // Eliminar el comportamiento anterior que cancelaba el cierre
            // y permitir que la ventana se cierre normalmente
        }
    }
}
