using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CtrEditor.ObjetosSim;
namespace CtrEditor.Controls
{
///
/// Interaction logic for osVisFilter.xaml
///
public partial class osVisFilter : UserControl
{
public osVisFilter()
{
InitializeComponent();
DataContext = FilterViewModel = new osVisFilterViewModel();
}
public osVisFilterViewModel FilterViewModel { get; private set; }
///
/// Updates the type filters based on the provided types
///
public void UpdateAvailableTypes(IEnumerable types)
{
FilterViewModel.UpdateTypeFilters(types);
}
}
///
/// ViewModel for the osVisFilter control
///
public partial class osVisFilterViewModel : ObservableObject
{
[ObservableProperty]
private bool showAll = true;
[ObservableProperty]
private bool showCloned;
[ObservableProperty]
private bool showAutoCreated;
[ObservableProperty]
private bool showEnableOnAllPages;
[ObservableProperty]
private bool showOnThisPage;
[ObservableProperty]
private ObservableCollection typeFilters = new ObservableCollection();
public osVisFilterViewModel()
{
// PropertyChanged listeners could be added here if needed
}
///
/// Updates the type filters based on the provided types
///
public void UpdateTypeFilters(IEnumerable types)
{
// Remove types that are no longer present
var typesToRemove = TypeFilters
.Where(tf => !types.Contains(tf.Type))
.ToList();
foreach (var item in typesToRemove)
{
TypeFilters.Remove(item);
}
// Add new types that aren't already in the list
foreach (var type in types)
{
if (!TypeFilters.Any(tf => tf.Type == type))
{
TypeFilters.Add(new TypeFilterItem(type)
{
DisplayName = GetTypeDisplayName(type),
IsSelected = true
});
}
}
}
///
/// Gets the display name for a type, using the NombreClase static method if available
///
private string GetTypeDisplayName(Type type)
{
var methodInfo = type.GetMethod("NombreClase",
BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
return methodInfo != null ?
methodInfo.Invoke(null, null)?.ToString() :
type.Name;
}
}
///
/// Represents a type filter item with selection state
///
public partial class TypeFilterItem : ObservableObject
{
[ObservableProperty]
private bool isSelected = true;
[ObservableProperty]
private string displayName;
public Type Type { get; }
public TypeFilterItem(Type type)
{
Type = type;
DisplayName = type?.Name ?? "Unknown Type";
}
}
}