using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using NexusShell.Models;
using NexusShell.ViewModels;

namespace NexusShell.Views;

public partial class NexusMediaView
{
    private bool _seekDragging;
    private NexusMediaViewModel? ViewModel => DataContext as NexusMediaViewModel;

    public NexusMediaView()
    {
        InitializeComponent();
        Loaded += OnLoaded;
        Unloaded += OnUnloaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        if (ViewModel is null)
        {
            return;
        }

        ViewModel.AttachVideoSurface(VideoSurface);
        ViewModel.Initialize();
        ViewModel.FullscreenRequested += OnFullscreenRequested;
    }

    private void OnUnloaded(object sender, RoutedEventArgs e)
    {
        if (ViewModel is null)
        {
            return;
        }

        ViewModel.FullscreenRequested -= OnFullscreenRequested;
        ViewModel.DetachVideoSurface();
    }

    private void OnFullscreenRequested(object? sender, EventArgs e)
    {
        var window = Window.GetWindow(this);
        if (window is null)
        {
            return;
        }

        window.WindowStyle = window.WindowStyle == WindowStyle.None && window.WindowState == WindowState.Maximized
            ? WindowStyle.SingleBorderWindow
            : WindowStyle.None;
        window.WindowState = window.WindowState == WindowState.Maximized
            ? WindowState.Normal
            : WindowState.Maximized;
    }

    private void VideoCard_OnClick(object sender, MouseButtonEventArgs e)
    {
        if (sender is not FrameworkElement element || ViewModel is null)
        {
            return;
        }

        if (element.DataContext is NexusMediaGridItem item)
        {
            ViewModel.SelectItemCommand.Execute(item);
        }
    }

    private void MusicList_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (ViewModel is null || sender is not ListView list)
        {
            return;
        }

        if (list.SelectedItem is NexusMediaListItem item)
        {
            ViewModel.SelectItemCommand.Execute(item);
        }
    }

    private void MusicList_OnDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (ViewModel is null || sender is not ListView { SelectedItem: NexusMediaListItem item })
        {
            return;
        }

        ViewModel.PlayItemCommand.Execute(item);
    }

    private void SeekSlider_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        if (!_seekDragging || ViewModel is null || sender is not Slider slider)
        {
            return;
        }

        ViewModel.SeekCommand.Execute(slider.Value);
    }

    protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        if (e.OriginalSource is Slider or Thumb)
        {
            _seekDragging = false;
        }

        base.OnPreviewMouseLeftButtonUp(e);
    }

    protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        if (e.OriginalSource is Slider or Thumb)
        {
            _seekDragging = true;
        }

        base.OnPreviewMouseLeftButtonDown(e);
    }
}
