using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.SearchModels;
using SecuritySystemContracts.StoragesContracts;
using SecuritySystemContracts.ViewModels;
using SecuritySystemListImplement.Models;

namespace SecuritySystemListImplement.Implements
{
    public class ImplementerStorage : IImplementerStorage
    {
        private readonly DataListSingleton _source;

        public ImplementerStorage(DataListSingleton source)
        {
            _source = DataListSingleton.GetInstance();
        }

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

        public ImplementerViewModel? GetElement(ImplementerSearchModel model)
        {
            return GetFilteredList(model).FirstOrDefault();
        }

        public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
        {
            var implementers = _source.Implementers
            .Select(x => x.GetViewModel)
            .ToList();
            if (model.Id.HasValue)
            {
                implementers = implementers.Where(x => x.Id == model.Id.Value).ToList();
            }
            if (model.Password != null)
            {
                implementers = implementers.Where(x => x.Password == model.Password).ToList();
            }
            if (model.ImplementerFIO != null)
            {
                implementers = implementers.Where(x => x.ImplementerFIO == model.ImplementerFIO).ToList();
            }
            return implementers;
        }

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

        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;
        }
    }
}
