using SecureShopContracts.BindingModels;
using SecureShopContracts.SearchModels;
using SecureShopContracts.StoragesContracts;
using SecureShopContracts.ViewModels;
using SecureShopListImplement.Models;

namespace SecureShopListImplement.Implements
{
    public class FacilitiesStorage : IFacilitiesStorage
    {
        private readonly DataListSingleton _source;

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

        public List<FacilitiesViewModel> GetFullList()
        {
            var result = new List<FacilitiesViewModel>();
            foreach (var Facilities in _source.Facilitiess)
            {
                result.Add(Facilities.GetViewModel);
            }
            return result;
        }

        public List<FacilitiesViewModel> GetFilteredList(FacilitiesSearchModel model)
        {
            var result = new List<FacilitiesViewModel>();
            if (string.IsNullOrEmpty(model.FacilitiesName))
            {
                return result;
            }
            foreach (var Facilities in _source.Facilitiess)
            {
                if (Facilities.FacilitiesName.Contains(model.FacilitiesName))
                {
                    result.Add(Facilities.GetViewModel);
                }
            }
            return result;
        }

        public FacilitiesViewModel? GetElement(FacilitiesSearchModel model)
        {
            if (string.IsNullOrEmpty(model.FacilitiesName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var Facilities in _source.Facilitiess) 
            {
                if ((!string.IsNullOrEmpty(model.FacilitiesName) && Facilities.FacilitiesName == model.FacilitiesName) || (model.Id.HasValue && Facilities.Id == model.Id))
                {
                    return Facilities.GetViewModel;
                }
            }
            return null;
        }

        public FacilitiesViewModel? Insert(FacilitiesBindingModel model)
        {
            model.Id = 1;
            foreach (var Facilities in _source.Facilitiess)
            {
                if (model.Id <= Facilities.Id)
                {
                    model.Id = Facilities.Id + 1;
                }
            }
            var newFacilities = Facilities.Create(model);
            if (newFacilities == null)
            {
                return null;
            }
            _source.Facilitiess.Add(newFacilities);
            return newFacilities.GetViewModel;
        }

        public FacilitiesViewModel? Update(FacilitiesBindingModel model)
        {
            foreach (var Facilities in _source.Facilitiess)
            {
                if (Facilities.Id == model.Id)
                {
                    Facilities.Update(model);
                    return Facilities.GetViewModel;
                }
            }
            return null;
        }

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