using CtrEditor.ObjetosSim;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows;

namespace CtrEditor.Controls
{
    public partial class ObjectHierarchyView : UserControl
    {
        private MainViewModel _mainViewModel;
        public ObservableCollection<OsTreeNode> RootNodes { get; set; }

        public ObjectHierarchyView()
        {
            InitializeComponent();
            RootNodes = new ObservableCollection<OsTreeNode>();
            this.DataContext = this;
        }

        public void Initialize(MainViewModel mainViewModel)
        {
            _mainViewModel = mainViewModel;
            _mainViewModel.ObjetosSimulables.CollectionChanged += ObjetosSimulables_CollectionChanged;
            UpdateTree();
        }

        private void ObjetosSimulables_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            UpdateTree();
        }

        public void UpdateTree()
        {
            RootNodes.Clear();

            // Agrupar por tipos
            var groupsByType = _mainViewModel.ObjetosSimulables
                .GroupBy(obj => obj.GetType());

            foreach (var group in groupsByType)
            {
                // Usar GetTypeDisplayName para obtener el nombre de la clase
                var typeNode = new OsTreeNode(OsTreeNode.GetTypeDisplayName(group.Key));
                RootNodes.Add(typeNode);

                foreach (var obj in group)
                {
                    var objNode = new OsTreeNode(obj.Nombre, obj);
                    typeNode.Children.Add(objNode);

                    // Agregar relaciones group_Panel
                    if (!string.IsNullOrEmpty(obj.Group_Panel))
                    {
                        var linkedPanel = _mainViewModel.ObjetosSimulables
                            .FirstOrDefault(o => o.Nombre == obj.Group_Panel);
                        if (linkedPanel != null)
                        {
                            objNode.Children.Add(new OsTreeNode(linkedPanel.Nombre, linkedPanel, "(group_Panel)"));
                        }
                    }

                    // Agregar relaciones group_FramePanel
                    if (!string.IsNullOrEmpty(obj.Group_FramePanel))
                    {
                        var linkedFrame = _mainViewModel.ObjetosSimulables
                            .FirstOrDefault(o => o.Nombre == obj.Group_FramePanel);
                        if (linkedFrame != null)
                        {
                            objNode.Children.Add(new OsTreeNode(linkedFrame.Nombre, linkedFrame, "(group_FramePanel)"));
                        }
                    }

                    // Agregar objetos que hacen referencia a este objeto
                    var referencingObjects = _mainViewModel.ObjetosSimulables
                        .Where(o => o.Group_Panel == obj.Nombre || o.Group_FramePanel == obj.Nombre);

                    foreach (var refObj in referencingObjects)
                    {
                        var relationType = refObj.Group_Panel == obj.Nombre ? "(referenced by group_Panel)" : "(referenced by group_FramePanel)";
                        objNode.Children.Add(new OsTreeNode(refObj.Nombre, refObj, relationType));
                    }
                }
            }
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is OsTreeNode node && node.AssociatedObject != null)
            {
                // Actualizar la selección en el MainViewModel
                _mainViewModel.SelectedItemOsList = node.AssociatedObject;

              
            }
        }
    }
}
