using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarListImplement;
using SushiBarListImplements.Models;

namespace SushiBarListImplements.Implements
{
    public class SushiStorage : ISushiStorage
    {
        private DataListSingleton _source;

        public SushiStorage()
        {
            _source = DataListSingleton.GetInstance();
        }

        public List<SushiViewModel> GetFullList()
        {
            var result = new List<SushiViewModel>();
            foreach (var order in _source.Sushis)
            {
                result.Add(order.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.Sushis)
            {
                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.Sushis)
            {
                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.Sushis)
            {
                if (model.Id <= sushi.Id)
                {
                    model.Id = sushi.Id + 1;
                }
            }
            var newSushi = Sushi.Create(model);
            if (newSushi == null)
            {
                return null;
            }
            _source.Sushis.Add(newSushi);
            return newSushi.GetViewModel;
        }

        public SushiViewModel? Update(SushiBindingModel model)
        {
            foreach (var sushi in _source.Sushis)
            {
                if (sushi.Id == model.Id)
                {
                    sushi.Update(model);
                    return sushi.GetViewModel;
                }
            }
            return null;
        }

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