using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;
using NexusShell.Services;

namespace NexusShell.Controls;

public partial class NexusBottomDock
{
    public NexusBottomDock()
    {
        InitializeComponent();
        Loaded += OnDockLoaded;
    }

    private void OnDockLoaded(object sender, RoutedEventArgs e)
    {
        if (DockIcons == null)
        {
            return;
        }

        DockIcons.ItemContainerGenerator.StatusChanged += (_, _) =>
        {
            if (DockIcons.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
            {
                StaggerIconEntrance();
            }
        };

        StaggerIconEntrance();
    }

    private void StaggerIconEntrance()
    {
        if (DockIcons == null)
        {
            return;
        }

        for (var i = 0; i < DockIcons.Items.Count; i++)
        {
            if (DockIcons.ItemContainerGenerator.ContainerFromIndex(i) is not ContentPresenter presenter)
            {
                continue;
            }

            if (FindVisualChild<Button>(presenter) is not Button button)
            {
                continue;
            }

            AnimateIconEntrance(button, i);
        }
    }

    private static void AnimateIconEntrance(Button button, int index)
    {
        if (NexusMotionPreferences.ShouldReduceMotion())
        {
            button.Opacity = 1;
            return;
        }

        var delay = TimeSpan.FromMilliseconds(index * 45);
        var duration = TimeSpan.FromMilliseconds(220);
        var ease = new CubicEase { EasingMode = EasingMode.EaseOut };

        var storyboard = new Storyboard { BeginTime = delay };

        var scaleX = new DoubleAnimation(0.6, 1, duration) { EasingFunction = ease };
        Storyboard.SetTarget(scaleX, button);
        Storyboard.SetTargetProperty(scaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));

        var scaleY = new DoubleAnimation(0.6, 1, duration) { EasingFunction = ease };
        Storyboard.SetTarget(scaleY, button);
        Storyboard.SetTargetProperty(scaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

        var opacity = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(280));
        Storyboard.SetTarget(opacity, button);
        Storyboard.SetTargetProperty(opacity, new PropertyPath(UIElement.OpacityProperty));

        storyboard.Children.Add(scaleX);
        storyboard.Children.Add(scaleY);
        storyboard.Children.Add(opacity);
        storyboard.Begin(button);
    }

    private static T? FindVisualChild<T>(DependencyObject parent) where T : DependencyObject
    {
        for (var i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            if (child is T match)
            {
                return match;
            }

            var nested = FindVisualChild<T>(child);
            if (nested != null)
            {
                return nested;
            }
        }

        return null;
    }
}
