using ShoopNCook.Views;
using System.Windows.Input;
using Models;
using LocalEndpoint;
using Endpoint;

namespace ShoopNCook.Pages;

public partial class RecipePage : ContentPage
{

    private uint note;
    private bool isFavorite;


    private IAccountRecipesPreferences preferences;
    private IUserNotifier notifier;
    private RecipeInfo info;

    public ICommand StarCommand => new Command<string>(count => SetNote(uint.Parse(count)));

    public RecipePage(Recipe recipe, IUserNotifier notifier, IAccountRecipesPreferences preferences, uint amount)
    {
        InitializeComponent();

        this.preferences = preferences;
        this.notifier = notifier;
        this.info = recipe.Info;

        AccountRecipeRate rate = preferences.GetRate(recipe.Info);

        note = rate.Rate;
        isFavorite = rate.IsFavorite;

        SetFavorite(isFavorite);
        SetNote(note);

        RecipeInfo info = recipe.Info;

        Counter.Count = amount;
        CookTime.Text = info.CookTimeMins.ToString();
        Energy.Text = info.CalPerPers.ToString() + " cal/pers";
        RecipeName.Text = info.Name;

        if (info.Image != null)
            RecipeImage.Source = ImageSource.FromUri(info.Image);

        foreach (Ingredient ingredient in recipe.Ingredients)
            IngredientList.Add(new IngredientView(ingredient));

        //retrieves the app's styles
        var styles = Application.Current.Resources.MergedDictionaries.ElementAt(1);

        int count = 0;
        foreach (PreparationStep step in recipe.Steps) {
            //TODO display name of PreparationSteps.
            Label label = new Label();
            label.Style = (Style)styles["Small"];
            label.Text = "Step " + ++count + ": " + step.Description;
            StepList.Add(label);
        }
    }

    private void SetNote(uint note)
    {
        this.note = note;
        int i = 1;
        foreach (ImageButton img in Stars.Children)
        {
            if (i <= note)
            {
                img.Source = ImageSource.FromFile("star_full.svg");
                i++;
            }
            else
                img.Source = ImageSource.FromFile("star_empty.svg");
        }
    }

    private void OnFavorite(object o, EventArgs e)
    {
        SetFavorite(!isFavorite);
    }

    private void OnSubmitReviewClicked(object o, EventArgs e)
    {
        preferences.SetReviewScore(info, note);
        notifier.Success("Your review has been successfuly submited");
    }

    private void OnAddToMyListClicked(object o, EventArgs e)
    {
        if (!preferences.AddToWeeklyList(info, Counter.Count))
            notifier.Notice("You already added this recipe to you weekly list!");
        else
            notifier.Success("Recipe added to your weekly list.");
    }

    private void SetFavorite(bool isFavorite)
    {
        this.isFavorite = isFavorite;
        if (isFavorite)
        {
            Favorite.Source = ImageSource.FromFile("hearth_on.svg");
            preferences.AddToFavorites(info);
        }
        else
        {
            Favorite.Source = ImageSource.FromFile("hearth_off.svg");
            preferences.RemoveFromFavorites(info);
        }
    }
    private void OnBackButtonClicked(object sender, EventArgs e)
    {

        Navigation.PopAsync();
    }

}