using BarContracts.BindingModels;
using BarContracts.SearchModels;
using BarContracts.StoragesContracts;
using BarContracts.ViewModels;
using BarListImplement.Models;

namespace BarListImplement.Implements
{
    public class ImplementerStorage : IImplementerStorage
    {
        private readonly DataListSingleton _source;
        
        public ImplementerStorage()
        {
            _source = DataListSingleton.GetInstance();
        }

        public List<ImplementerViewModel> GetFullList()
        {
            var Result = new List<ImplementerViewModel>();
            
            foreach (var Implementer in _source.Implementers)
            {
                Result.Add(Implementer.GetViewModel);
            }
            
            return Result;
        }

        public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel Model)
        {
            if (string.IsNullOrEmpty(Model.ImplementerFIO))
                return new();

            return _source.Implementers
                .Where(x => (!string.IsNullOrEmpty(Model.ImplementerFIO) && x.ImplementerFIO.Contains(Model.ImplementerFIO)))
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public ImplementerViewModel? GetElement(ImplementerSearchModel Model)
        {
            foreach (var Implementer in _source.Implementers)
            {
                if ((Model.Id.HasValue && Implementer.Id == Model.Id) || 
                    (!string.IsNullOrEmpty(Model.ImplementerFIO) && Implementer.ImplementerFIO == Model.ImplementerFIO))
                {
                    return Implementer.GetViewModel;
                }
            }

            return null;
        }
       
        public ImplementerViewModel? Insert(ImplementerBindingModel Model)
        {
            Model.Id = 1;
            foreach (var Implementer in _source.Implementers)
            {
                if (Model.Id <= Implementer.Id)
                {
                    Model.Id = Implementer.Id + 1;
                }
            }
            
            var NewImplementer = Implementer.Create(Model);
            if (NewImplementer == null)
                return null;
            
            _source.Implementers.Add(NewImplementer);
            return NewImplementer.GetViewModel;
        }

        public ImplementerViewModel? Update(ImplementerBindingModel Model)
        {
            foreach (var Implementer in _source.Implementers)
            {
                if (Implementer.Id == Model.Id)
                {
                    Implementer.Update(Model);
                    return Implementer.GetViewModel;
                }
            }
            
            return null;
        }

        public ImplementerViewModel? Delete(ImplementerBindingModel Model)
        {
            for (int i = 0; i < _source.Implementers.Count; ++i)
            {
                if (_source.Implementers[i].Id == Model.Id)
                {
                    var Implementer = _source.Implementers[i];
                    _source.Implementers.RemoveAt(i);

                    return Implementer.GetViewModel;
                }
            }

            return null;
        }
    }
}
