
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace ReAct_PME
{
    public class GridViewSort
    {
        // pour trier dans le cas de Command d�finies dans ViewModel
        public static ICommand GetCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CommandProperty);
        }

        public static void SetCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CommandProperty, value);
        }

        // pour trier de mani�re automatique
        public static bool GetAutoSort(DependencyObject obj)
        {
            return (bool)obj.GetValue(AutoSortProperty);
        }

        public static void SetAutoSort(DependencyObject obj, bool value)
        {
            obj.SetValue(AutoSortProperty, value);
        }

        // pour trier en fonction d'une colonne (propri�t�)
        public static string GetPropertyName(DependencyObject obj)
        {
            if (obj != null)
                return (string)obj.GetValue(PropertyNameProperty);
            else
                return string.Empty;
        }

        public static void SetPropertyName(DependencyObject obj, string value)
        {
            obj.SetValue(PropertyNameProperty, value);
        }

        private static void ColumnHeader_Click(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader? headerClicked = e.OriginalSource as GridViewColumnHeader;
            if (headerClicked != null)
            {
                string propertyName = GetPropertyName(headerClicked.Column);
                if (!string.IsNullOrEmpty(propertyName))
                {
                    ListView? listView = GetAncestor<ListView>(headerClicked);
                    if (listView != null)
                    {
                        ICommand command = GetCommand(listView);
                        if (command != null)
                        {
                            if (command.CanExecute(propertyName))
                            {
                                command.Execute(propertyName);
                            }
                        }
                        else if (GetAutoSort(listView))
                        {
                            ApplySort(listView.Items, propertyName);
                        }
                    }
                }
            }
        }

        public static T? GetAncestor<T>(DependencyObject reference) where T : DependencyObject
        {
            DependencyObject parent = VisualTreeHelper.GetParent(reference);
            while (!(parent is T))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            if (parent != null)
                return (T)parent;
            else
                return null;
        }

        public static void ApplySort(ICollectionView view, string propertyName)
        {
            ListSortDirection direction = ListSortDirection.Ascending;
            if (view.SortDescriptions.Count > 0)
            {
                SortDescription currentSort = view.SortDescriptions[0];
                if (currentSort.PropertyName == propertyName)
                {
                    if (currentSort.Direction == ListSortDirection.Ascending)
                        direction = ListSortDirection.Descending;
                    else
                        direction = ListSortDirection.Ascending;
                }
                view.SortDescriptions.Clear();
            }
            if (!string.IsNullOrEmpty(propertyName))
            {
                view.SortDescriptions.Add(new SortDescription(propertyName, direction));
            }
        }

        public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.RegisterAttached(
                        "PropertyName",
                        typeof(string),
                        typeof(GridViewSort),
                        new UIPropertyMetadata(null)
                    );

        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
                        "Command",
                        typeof(ICommand),
                        typeof(GridViewSort),
                        new UIPropertyMetadata(
                            null,
                            (o, e) =>
                            {
                                ItemsControl? listView = o as ItemsControl;
                                if (listView != null)
                                {
                                    if (!GetAutoSort(listView))
                                    {
                                        if (e.OldValue != null && e.NewValue == null)
                                        {
                                            listView.RemoveHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
                                        }
                                        if (e.OldValue == null && e.NewValue != null)
                                        {
                                            listView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
                                        }
                                    }
                                }
                            }
                        )
                    );

        public static readonly DependencyProperty AutoSortProperty = DependencyProperty.RegisterAttached(
                        "AutoSort",
                        typeof(bool),
                        typeof(GridViewSort),
                        new UIPropertyMetadata(
                            false,
                            (o, e) =>
                            {
                                ListView? listView = o as ListView;
                                if (listView != null)
                                {
                                    if (GetCommand(listView) == null)
                                    {
                                        bool oldValue = (bool)e.OldValue;
                                        bool newValue = (bool)e.NewValue;
                                        if (oldValue && !newValue)
                                        {
                                            listView.RemoveHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
                                        }
                                        if (!oldValue && newValue)
                                        {
                                            listView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));
                                        }
                                    }
                                }
                            }
                        )
                    );
    }
}

                