using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows.Media;
using ytLive.Helpers;

namespace ytLive.ViewModels;

public class ReuseImageCandidate
{
    public string SceneName { get; set; } = string.Empty;
    public string SourceName { get; set; } = string.Empty;
    public string AssetId { get; set; } = string.Empty;

    public ImageSource? Thumbnail => ImageCache.Get(AssetId);
    public string Header => $"{SourceName} · in {SceneName}";
}

public class ReuseImageViewModel : ViewModelBase
{
    private ReuseImageCandidate? _selectedCandidate;

    public ObservableCollection<ReuseImageCandidate> Candidates { get; }

    public ReuseImageCandidate? SelectedCandidate
    {
        get => _selectedCandidate;
        set
        {
            if (SetProperty(ref _selectedCandidate, value))
                CommandManager.InvalidateRequerySuggested();
        }
    }

    public ICommand UseSelectedCommand { get; }
    public ICommand NewImageCommand { get; }
    public ICommand CancelCommand { get; }

    public event Action? ReuseRequested;
    public event Action? NewImageRequested;
    public event Action? CancelRequested;

    public ReuseImageViewModel(IEnumerable<ReuseImageCandidate> candidates)
    {
        Candidates = new ObservableCollection<ReuseImageCandidate>(candidates);
        if (Candidates.Count > 0)
            _selectedCandidate = Candidates[0];

        UseSelectedCommand = new RelayCommand(_ => ReuseRequested?.Invoke(), _ => SelectedCandidate != null);
        NewImageCommand = new RelayCommand(_ => NewImageRequested?.Invoke());
        CancelCommand = new RelayCommand(_ => CancelRequested?.Invoke());
    }
}
