using Models;

namespace ShoopNCook.Pages;

using Endpoint;
using Models;
using ShoopNCook.Views;

public partial class FavoritesPage : ContentPage
{

    private readonly Account account;
    private readonly IUserNotifier notifier;
    private IRecipesService service;

    public FavoritesPage(Account account, IUserNotifier notifier, IRecipesService service)
    {
        InitializeComponent();
        this.account = account;
        this.notifier = notifier;
        this.service = service;

        UpdateFavorites();
    }

    private void UpdateFavorites()
    {
        IAccountRecipesPreferences preferences = service.GetPreferencesOf(account);
        RecipeViewLayout.Children.Clear();
        preferences.GetFavorites().ForEach(info =>
        {
            RecipeViewLayout.Children.Add(new RecipeView(info, () =>
            {
                Recipe recipe = service.GetRecipe(info);
                Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, 1));
            }));
        });
    }

    private void ContentPage_NavigatedTo(object sender, NavigatedToEventArgs e)
    {
        UpdateFavorites();
    }
}