using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerListImplement.Models;

namespace DinerListImplement.Implements
{
    public class SnackStorage : ISnackStorage
    {
        private readonly DataListSingleton _source;
        public SnackStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public SnackViewModel? Delete(SnackBindingModel model)
        {
            for (int i = 0; i < _source.Products.Count; ++i)
            {
                if (_source.Products[i].Id == model.Id)
                {
                    var element = _source.Products[i];
                    _source.Products.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
        public SnackViewModel? GetElement(SnackSearchModel model)
        {
            if (string.IsNullOrEmpty(model.SnackName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var product in _source.Products)
            {
                if ((!string.IsNullOrEmpty(model.SnackName) && product.SnackName == model.SnackName) || (model.Id.HasValue && product.Id == model.Id))
                {
                    return product.GetViewModel;
                }
            }
            return null;
        }

        public List<SnackViewModel> GetFilteredList(SnackSearchModel model)
        {
            var result = new List<SnackViewModel>();
            if (string.IsNullOrEmpty(model.SnackName))
            {
                return result;
            }
            foreach (var product in _source.Products)
            {
                if (product.SnackName.Contains(model.SnackName))
                {
                    result.Add(product.GetViewModel);
                }
            }
            return result;
        }

        public List<SnackViewModel> GetFullList()
        {
            var result = new List<SnackViewModel>();
            foreach (var product in _source.Products)
            {
                result.Add(product.GetViewModel);
            }
            return result;
        }

        public SnackViewModel? Insert(SnackBindingModel model)
        {
            model.Id = 1;
            foreach (var product in _source.Products)
            {
                if (model.Id <= product.Id)
                {
                    model.Id = product.Id + 1;
                }
            }
            var newProduct = Snack.Create(model);
            if (newProduct == null)
            {
                return null;
            }
            _source.Products.Add(newProduct);
            return newProduct.GetViewModel;
        }

        public SnackViewModel? Update(SnackBindingModel model)
        {
            foreach (var product in _source.Products)
            {
                if (product.Id == model.Id)
                {
                    product.Update(model);
                    return product.GetViewModel;
                }
            }
            return null;
        }
    }
}
