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

namespace SecureShopListImplement.Implements
{
    public class SecureStorage : ISecureStorage
    {
        private readonly DataListSingleton _source;

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

        public List<SecureViewModel> GetFullList()
        {
            var result = new List<SecureViewModel>();
            foreach (var Secure in _source.Secures)
            {
                result.Add(Secure.GetViewModel);
            }
            return result;
        }

        public List<SecureViewModel> GetFilteredList(SecureSearchModel model)
        {
            var result = new List<SecureViewModel>();
            if (string.IsNullOrEmpty(model.SecureName))
            {
                return result;
            }
            foreach (var Secure in _source.Secures)
            {
                if (Secure.SecureName.Contains(model.SecureName))
                {
                    result.Add(Secure.GetViewModel);
                }
            }
            return result;
        }

        public SecureViewModel? GetElement(SecureSearchModel model)
        {
            if (string.IsNullOrEmpty(model.SecureName) && !model.Id.HasValue)
            {
                return null;
            }
            foreach (var Secure in _source.Secures)
            {
                if ((!string.IsNullOrEmpty(model.SecureName) && Secure.SecureName == model.SecureName) || (model.Id.HasValue && Secure.Id == model.Id))
                {
                    return Secure.GetViewModel;
                }
            }
            return null;
        }

        public SecureViewModel? Insert(SecureBindingModel model)
        {
            model.Id = 1;
            foreach (var Secure in _source.Secures)
            {
                if (model.Id <= Secure.Id)
                {
                    model.Id = Secure.Id + 1;
                }
            }
            var newSecure = Secure.Create(model);
            if (newSecure == null)
            {
                return null;
            }
            _source.Secures.Add(newSecure);
            return newSecure.GetViewModel;
        }

        public SecureViewModel? Update(SecureBindingModel model)
        {
            foreach (var Secure in _source.Secures)
            {
                if (Secure.Id == model.Id)
                {
                    Secure.Update(model);
                    return Secure.GetViewModel;
                }
            }
            return null;
        }

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