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

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

        public List<CannedViewModel> GetFullList()
        {
            var result = new List<CannedViewModel>();
            foreach(var canned in _source.ListCanned)
            {
                result.Add(canned.GetViewModel);
            }
            return result;
        }

        public List<CannedViewModel> GetFilteredList(CannedSearchModel model)
        {
            var result = new List<CannedViewModel>();
            if (string.IsNullOrEmpty(model.CannedName))
            {
                return result;
            }
            foreach (var canned in _source.ListCanned)
            {
                if (canned.CannedName.Contains(model.CannedName))
                {
                    result.Add(canned.GetViewModel);
                }
            }
            return result;
        }

        public CannedViewModel? GetElement(CannedSearchModel model)
        {
            if (string.IsNullOrEmpty(model.CannedName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var canned in _source.ListCanned)
            {
                if ((!string.IsNullOrEmpty(model.CannedName) &&
               canned.CannedName == model.CannedName) ||
                (model.Id.HasValue && canned.Id == model.Id))
                {
                    return canned.GetViewModel;
                }
            }
            return null;
        }

        public CannedViewModel? Insert(CannedBindingModel model)
        {
            model.Id = 1;
            foreach (var canned in _source.ListCanned)
            {
                if (model.Id <= canned.Id)
                {
                    model.Id = canned.Id + 1;
                }
            }
            var newSushi = Canned.Create(model);
            if (newSushi == null)
            {
                return null;
            }
            _source.ListCanned.Add(newSushi);
            return newSushi.GetViewModel;
        }

        public CannedViewModel? Update(CannedBindingModel model)
        {
            foreach (var canned in _source.ListCanned)
            {
                if (canned.Id == model.Id)
                {
                    canned.Update(model);
                    return canned.GetViewModel;
                }
            }
            return null;
        }

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