using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.SearchModels;
using AbstractFoodOrdersContracts.StoragesContracts;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractFoodOrdersListImplement.Implements
{
    public class ComponentStorage : IComponentStorage
    {
        private readonly DataListSingleton _source;
        public ComponentStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public List<ComponentViewModel> GetFullList()
        {
            var result = new List<ComponentViewModel>();
            foreach (var ingredient in _source.Components)
            {
                result.Add(ingredient.GetViewModel);
            }
            return result;
        }

        public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
        {
            var result = new List<ComponentViewModel>();
            if (string.IsNullOrEmpty(model.ComponentName))
            {
                return result;
            }
            foreach (var ingredient in _source.Components)
            {
                if (ingredient.ComponentName.Contains(model.ComponentName))
                {
                    result.Add(ingredient.GetViewModel);
                }
            }
            return result;
        }

        public ComponentViewModel? GetElement(ComponentSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var ingredient in _source.Components)
            {
                if ((!string.IsNullOrEmpty(model.ComponentName) && ingredient.ComponentName == model.ComponentName) ||
                (model.Id.HasValue && ingredient.Id == model.Id))
                {
                    return ingredient.GetViewModel;
                }
            }
            return null;
        }

        public ComponentViewModel? Insert(ComponentBindingModel model)
        {
            model.Id = 1;
            foreach (var ingredient in _source.Components)
            {
                if (model.Id <= ingredient.Id)
                {
                    model.Id = ingredient.Id + 1;
                }
            }
            var newIngredient = Component.Create(model);
            if (newIngredient == null)
            {
                return null;
            }
            _source.Components.Add(newIngredient);
            return newIngredient.GetViewModel;
        }

        public ComponentViewModel? Update(ComponentBindingModel model)
        {
            foreach (var ingredient in _source.Components)
            {
                if (ingredient.Id == model.Id)
                {
                    ingredient.Update(model);
                    return ingredient.GetViewModel;
                }
            }
            return null;
        }

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