using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushibarListImplement.Models;

namespace SushibarListImplement.Implements
{
    public class SushiStorage : ISushiStorage
    {
        private readonly DataListSingleton _source;
        public SushiStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public List<SushiViewModel> GetFullList()
        {
            var result = new List<SushiViewModel>();
            foreach (var sushi in _source.Sushi)
            {
                result.Add(sushi.GetViewModel);
            }
            return result;
        }
        public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
        {
            var result = new List<SushiViewModel>();
            if (string.IsNullOrEmpty(model.SushiName))
            {
                return result;
            }
            foreach (var sushi in _source.Sushi)
            {
                if (sushi.SushiName.Contains(model.SushiName))
                {
                    result.Add(sushi.GetViewModel);
                }
            }
            return result;
        }
        public SushiViewModel? GetElement(SushiSearchModel model)
        {
            if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var sushi in _source.Sushi)
            {
                if ((!string.IsNullOrEmpty(model.SushiName) &&
                      sushi.SushiName == model.SushiName) ||
                      (model.Id.HasValue && sushi.Id == model.Id))
                {
                    return sushi.GetViewModel;
                }
            }
            return null;
        }
        public SushiViewModel? Insert(SushiBindingModel model)
        {
            model.Id = 1;
            foreach (var sushi in _source.Sushi)
            {
                if (model.Id <= sushi.Id)
                {
                    model.Id = sushi.Id + 1;
                }
            }
            Sushi? newComponent = Sushi.Create(model);
            if (newComponent == null)
            {
                return null;
            }
            _source.Sushi.Add(newComponent);
            return newComponent.GetViewModel;
        }
        public SushiViewModel? Update(SushiBindingModel model)
        {
            foreach (var sushi in _source.Sushi)
            {
                if (sushi.Id == model.Id)
                {
                    sushi.Update(model);
                    return sushi.GetViewModel;
                }
            }
            return null;
        }
        public SushiViewModel? Delete(SushiBindingModel model)
        {
            for (int i = 0; i < _source.Sushi.Count; i++)
            {
                if (_source.Sushi[i].Id == model.Id)
                {
                    var element = _source.Sushi[i];
                    _source.Sushi.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
    }
}
