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

namespace SecuritySystemListImplement.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;
        }
    }
}
