using Lab.Mvvm.Models;

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Lab.Mvvm.ViewModels;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Lab.Mvvm.Services;
using System;

public partial class BooksPageViewModel : ObservableObject
{
    [ObservableProperty]
    private List<Book> _books;

    [ObservableProperty]
    private List<string> _genres;

    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(ClearFilterCommand))]
    private string _selectedGenre;

    [ObservableProperty]
    private BookService _booksService;

    public BooksPageViewModel()
    {
        _booksService = new BookService();
        Genres = _booksService.GetGenres();
        LoadBooks();

        /*ClearFilterCommand = new RelayCommand(
            execute: () => SelectedGenre = null,
            canExecute: () => SelectedGenre != null);
    */}

    partial void OnSelectedGenreChanged(string value)
    {
        LoadBooks();

//        ClearFilterCommand.NotifyCanExecuteChanged();
    }

    private void LoadBooks()
    {
        // A Books  property állítása kiváltja az INPC PropertyChanged eseményt (lásd Books property setter fent) - a nézet frissülni fog
        Books = _booksService.GetBooks(SelectedGenre);
    }



    //public IRelayCommand ClearFilterCommand { get; }

    private bool IsClearFilterCommandEnabled => SelectedGenre != null;

    [RelayCommand(CanExecute = nameof(IsClearFilterCommandEnabled))]
    private void ClearFilter() => SelectedGenre = null;
}