using WebApi.Models;

namespace WebApi.Services;

public static class PizzaService
{
    static List<Pizza> Pizzas { get; }
    static int nextId = 0;

    static PizzaService()
    {
        Pizzas = new List<Pizza>()
        {
            new Pizza() { Id = 1, Name = "Classic Italian", IsGluentFree = false },
            new Pizza() { Id = 2, Name = "Veggie", IsGluentFree = true },
        };

        nextId = Pizzas.Count + 1;
    }

    public static List<Pizza> GetAll() => Pizzas;

    public static Pizza? Get(int id) => Pizzas.FirstOrDefault(p => p.Id == id);

    public static void Add(Pizza pizza)
    {
        pizza.Id = nextId++;
        Pizzas.Add(pizza);
    }

    public static void Delete(int id)
    {
        var pizza = Get(id);
        if (pizza is null)
            return;
        
        Pizzas.Remove(pizza);
    }

    public static void Update(Pizza pizza)
    {
        var index = Pizzas.FindIndex(p => p.Id == pizza.Id);
        if (index < 0)
            return;

        Pizzas[index] = pizza;
    }
}