using Endpoint;
using LocalEndpoint;
using Models;
using ShoopNCook.Views;

namespace ShoopNCook.Pages;

public partial class MyListPage : ContentPage
{

    private readonly IAccountRecipesPreferences preferences;
    private readonly IUserNotifier notifier;
    private readonly IRecipesService service;

	public MyListPage(Account account, IUserNotifier notifier, IRecipesService service)
    {
		InitializeComponent();

        this.preferences = service.GetPreferencesOf(account);
        this.notifier = notifier;
        this.service = service;

        UpdateMyList();
    }

    private void UpdateMyList()
    {
        RecipesLayout.Children.Clear();
        preferences.GetWeeklyList().ForEach(tuple =>
        {
            RecipeInfo info = tuple.Item1;
            RecipesLayout.Children.Add(new StoredRecipeView(info, tuple.Item2, amount =>
            {
                Recipe recipe = service.GetRecipe(info);
                Shell.Current.Navigation.PushAsync(new RecipePage(recipe, notifier, preferences, amount));
            }));
        });
    }

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