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

namespace SecuritySystemListImplement.Implements
{
    public class ClientStorage : IClientStorage
    {
        private readonly DataListSingleton _source;
        public ClientStorage()
        {
            _source = DataListSingleton.GetInstance();
        }
        public List<ClientViewModel> GetFullList()
        {
            var result = new List<ClientViewModel>();
            foreach (var client in _source.Clients)
            {
                result.Add(client.GetViewModel);
            }
            return result;
        }
        public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
        {
            var clients = _source.Clients
            .Select(x => x.GetViewModel)
            .ToList();
            if (model.Id.HasValue)
            {
                clients = clients.Where(x => x.Id == model.Id.Value).ToList();
            }
            if (model.Email != null)
            {
                clients = clients.Where(x => x.Email == model.Email).ToList();
            }
            if (model.Password != null)
            {
                clients = clients.Where(x => x.Password == model.Password).ToList();
            }
            if (model.ClientFIO != null)
            {
                clients = clients.Where(x => x.ClientFIO == model.ClientFIO).ToList();
            }
            return clients;
        }
        public ClientViewModel? GetElement(ClientSearchModel model)
        {
            return GetFilteredList(model).FirstOrDefault();
        }
        public ClientViewModel? Insert(ClientBindingModel model)
        {
            model.Id = 1;
            foreach (var client in _source.Clients)
            {
                if (model.Id <= client.Id)
                {
                    model.Id = client.Id + 1;
                }
            }
            var newClient = Client.Create(model);
            if (newClient == null)
            {
                return null;
            }
            _source.Clients.Add(newClient);
            return newClient.GetViewModel;
        }
        public ClientViewModel? Update(ClientBindingModel model)
        {
            foreach (var client in _source.Clients)
            {
                if (client.Id == model.Id)
                {
                    client.Update(model);
                    return client.GetViewModel;
                }
            }
            return null;
        }
        public ClientViewModel? Delete(ClientBindingModel model)
        {
            for (int i = 0; i < _source.Clients.Count; ++i)
            {
                if (_source.Clients[i].Id == model.Id)
                {
                    var element = _source.Clients[i];
                    _source.Clients.RemoveAt(i);
                    return element.GetViewModel;
                }
            }
            return null;
        }
    }
}
