using FishFactoryContracts.BindingModels;
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;
using FishFactoryListImplement.Models;

namespace FishFactoryListImplement.Implements
{
    public class IngredientStorage : IIngredientStorage
    {
        private readonly DataListSingleton _source;
        public IngredientStorage()
        {
            _source = DataListSingleton.GetInstance();
        }

        public List<IngredientViewModel> GetFullList()
        {
            var result = new List<IngredientViewModel>();
            foreach (var component in _source.Ingredients)
            {
                result.Add(component.GetViewModel);
            }
            return result;
        }

        public List<IngredientViewModel> GetFilteredList(IngredientSearchModel model)
        {
            var result = new List<IngredientViewModel>();
            if (string.IsNullOrEmpty(model.IngredientName))
            {
                return result;
            }
            foreach (var component in _source.Ingredients)
            {
                if (component.IngredientName.Contains(model.IngredientName))
                {
                    result.Add(component.GetViewModel);
                }
            }
            return result;
        }

        public IngredientViewModel? GetElement(IngredientSearchModel model)
        {
            if (string.IsNullOrEmpty(model.IngredientName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var component in _source.Ingredients)
            {
                if ((!string.IsNullOrEmpty(model.IngredientName) &&
               component.IngredientName == model.IngredientName) ||
                (model.Id.HasValue && component.Id == model.Id))
                {
                    return component.GetViewModel;
                }
            }
            return null;
        }

        public IngredientViewModel? Insert(IngredientBindingModel model)
        {
            model.Id = 1;
            foreach (var component in _source.Ingredients)
            {
                if (model.Id <= component.Id)
                {
                    model.Id = component.Id + 1;
                }
            }
            var newIngredient = Ingredient.Create(model);
            if (newIngredient == null)
            {
                return null;
            }
            _source.Ingredients.Add(newIngredient);
            return newIngredient.GetViewModel;
        }

        public IngredientViewModel? Update(IngredientBindingModel model)
        {
            foreach (var component in _source.Ingredients)
            {
                if (component.Id == model.Id)
                {
                    component.Update(model);
                    return component.GetViewModel;
                }
            }
            return null;
        }

        public IngredientViewModel? Delete(IngredientBindingModel model)
        {
            for (int i = 0; i < _source.Ingredients.Count; ++i)
            {
                if (_source.Ingredients[i].Id == model.Id)
                {
                    var element = _source.Ingredients[i];
                    _source.Ingredients.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
    }
}
