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

namespace NexusShell.Views;

public partial class NexusPhotosView
{
    public NexusPhotosView()
    {
        InitializeComponent();
    }

    private NexusPhotosViewModel? Vm => DataContext as NexusPhotosViewModel;

    private void PhotoTile_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (sender is FrameworkElement { DataContext: NexusPhotoGridItem item } && Vm is not null)
        {
            Vm.SelectPhotoCommand.Execute(item);
            if (e.ClickCount >= 2)
            {
                Vm.OpenPhotoCommand.Execute(item);
            }
        }
    }

    private void HomeTile_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (sender is FrameworkElement { DataContext: NexusPhotoGridItem item } && Vm is not null)
        {
            if (e.ClickCount >= 2)
            {
                Vm.OpenPhotoCommand.Execute(item);
            }
            else
            {
                Vm.SelectPhotoCommand.Execute(item);
            }
        }
    }

    private void Root_OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        Vm?.HandleViewerKey(e.Key);
    }

    private void ViewerOverlay_OnMouseWheel(object sender, MouseWheelEventArgs e)
    {
        Vm?.HandleViewerMouseWheel(e.Delta);
        e.Handled = true;
    }

    private void FolderRow_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (sender is FrameworkElement { DataContext: Models.NexusPhotoFolderSummary folder } && Vm is not null)
        {
            Vm.OpenFolderCommand.Execute(folder);
        }
    }
}
