using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaListImplement.Models;

namespace PizzeriaListImplement.Implements
{
    public class PizzaStorage : IPizzaStorage
    {
        private readonly DataListSingleton _source;
        public PizzaStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public List<PizzaViewModel> GetFullList()
        {
            var result = new List<PizzaViewModel>();
            foreach (var Pizza in _source.Pizzas)
            {
                result.Add(Pizza.GetViewModel);
            }
            return result;
        }
        public List<PizzaViewModel> GetFilteredList(PizzaSearchModel model)
        {
            var result = new List<PizzaViewModel>();
            if (string.IsNullOrEmpty(model.PizzaName))
            {
                return result;
            }
            foreach (var Pizza in _source.Pizzas)
            {
                if (Pizza.PizzaName.Contains(model.PizzaName))
                {
                    result.Add(Pizza.GetViewModel);
                }
            }
            return result;
        }
        public PizzaViewModel? GetElement(PizzaSearchModel model)
        {
            if (string.IsNullOrEmpty(model.PizzaName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var Pizza in _source.Pizzas)
            {
                if ((!string.IsNullOrEmpty(model.PizzaName) && Pizza.PizzaName == model.PizzaName) || (model.Id.HasValue && Pizza.Id == model.Id))
                {
                    return Pizza.GetViewModel;
                }
            }
            return null;
        }
        public PizzaViewModel? Insert(PizzaBindingModel model)
        {
            model.Id = 1;
            foreach (var Pizza in _source.Pizzas)
            {
                if (model.Id <= Pizza.Id)
                {
                    model.Id = Pizza.Id + 1;
                }
            }
            var newPizza = Pizza.Create(model);
            if (newPizza == null)
            {
                return null;
            }
            _source.Pizzas.Add(newPizza);
            return newPizza.GetViewModel;
        }
        public PizzaViewModel? Update(PizzaBindingModel model)
        {
            foreach (var Pizza in _source.Pizzas)
            {
                if (Pizza.Id == model.Id)
                {
                    Pizza.Update(model);
                    return Pizza.GetViewModel;
                }
            }
            return null;
        }
        public PizzaViewModel? Delete(PizzaBindingModel model)
        {
            for (int i = 0; i < _source.Pizzas.Count; ++i)
            {
                if (_source.Pizzas[i].Id == model.Id)
                {
                    var element = _source.Pizzas[i];
                    _source.Pizzas.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
    }
}
