using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using MauiAppStock.Models;
using MauiAppStock.Data;
using MauiAppStock.Helpers;

namespace MauiAppStock.ViewModels
{
    public class AppareilsViewModel : BaseViewModel
    {
        public ObservableCollection<Appareil> Appareils { get; set; }
        public ICommand LoadAppareilsCommand { get; }
        public ICommand DeleteAppareilCommand { get; }

        public AppareilsViewModel()
        {
            Appareils = new ObservableCollection<Appareil>();
            LoadAppareilsCommand = new AsyncCommand(LoadAppareils);
            DeleteAppareilCommand = new AsyncCommand<Appareil>(DeleteAppareil);
        }

        private async Task LoadAppareils()
        {
            Appareils.Clear();
            var appareilsList = await Database.GetAppareilsAsync();
            foreach (var appareil in appareilsList)
            {
                Appareils.Add(appareil);
            }
        }

        private async Task DeleteAppareil(Appareil appareil)
        {
            if (appareil != null)
            {
                await Database.DeleteAppareilAsync(appareil);
                Appareils.Remove(appareil);
            }
        }
    }
}