using CocktailWeb;
using CocktailWeb.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.EntityFrameworkCore;

namespace CocktailWeb.Pages
{
    public partial class Cocktails
    {
        private DbDataContext? _DataContext;

        private List<Cocktail> CocktailListe { get; set; } = new();
        private List<Filler>? MaschinenFiller;

        protected override async Task OnInitializedAsync()
        {
            _DataContext ??= await DataContextFactory.CreateDbContextAsync();
            if (_DataContext != null)
            {
                MaschinenFiller = _DataContext.Fillers
                    .Include(f => f.Flasche)
                    .OrderBy(f => f.Pos).ToList();
                CocktailListe = await _DataContext.Cocktails
                    .Include(c => c.Cocktailflaschen)
                    .ThenInclude(cf => cf.Flasche)
                    .OrderBy(c => c.Name)
                    .ToListAsync();
            }
        }


        /// <summary>
        /// Prüft, ob der angegebene Cocktail aktuell machbar ist (true) oder ob mindestens eine Flasche nicht auf der Maschine fehlt (false)
        /// </summary>
        /// <param name="cocktail"></param>
        /// <returns></returns>
        private bool IsTappable(Cocktail cocktail)
        {
            if (MaschinenFiller != null)
            {
                return cocktail.Cocktailflaschen.All(cf => MaschinenFiller.Any(mf => mf.Flasche == cf.Flasche));
            }
            return false;
        }
    }
}
