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

namespace NexusShell.Views;

public partial class NotificationCenterOverlay : UserControl
{
    private const double SlideOffset = 438;
    private const int SlideDurationMs = 260;

    public event EventHandler? DismissRequested;

    public NotificationCenterOverlay()
    {
        InitializeComponent();
    }

    public void PlayOpenAnimation()
    {
        Opacity = 0;

        if (PanelHost.RenderTransform is TranslateTransform tt)
        {
            tt.X = SlideOffset;
        }

        var slideIn = new DoubleAnimation(SlideOffset, 0, TimeSpan.FromMilliseconds(SlideDurationMs))
        {
            EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut },
        };

        var fadeIn = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(SlideDurationMs))
        {
            EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut },
        };

        if (PanelHost.RenderTransform is TranslateTransform transform)
        {
            transform.BeginAnimation(TranslateTransform.XProperty, slideIn);
        }

        BeginAnimation(OpacityProperty, fadeIn);
    }

    public void PlayCloseAnimation(Action? onComplete = null)
    {
        var slideOut = new DoubleAnimation(0, SlideOffset, TimeSpan.FromMilliseconds(SlideDurationMs))
        {
            EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn },
        };

        var fadeOut = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(SlideDurationMs))
        {
            EasingFunction = new CubicEase { EasingMode = EasingMode.EaseIn },
        };

        slideOut.Completed += (_, _) => onComplete?.Invoke();

        if (PanelHost.RenderTransform is TranslateTransform transform)
        {
            transform.BeginAnimation(TranslateTransform.XProperty, slideOut);
        }

        BeginAnimation(OpacityProperty, fadeOut);
    }

    private void Overlay_OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        DismissRequested?.Invoke(this, EventArgs.Empty);
    }

    private void Panel_OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        e.Handled = true;
    }
}
