using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BlacksmithWorkshopListImplement.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)
        {
            if (string.IsNullOrEmpty(model.ClientFIO) &&
                string.IsNullOrEmpty(model.Email) &&
                string.IsNullOrEmpty(model.Password))
            {
                return new();
            }
            return _source.Clients
                .Where(x =>
                            (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO)) &&
                            (string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email)) &&
                            (string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password)))
                .Select(x => x.GetViewModel)
                .ToList();
        }
        public ClientViewModel? GetElement(ClientSearchModel model)
        {
            return _source.Clients
                .FirstOrDefault(x =>
                            (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO == model.ClientFIO) &&
                            (string.IsNullOrEmpty(model.Email) || x.Email == model.Email) &&
                            (string.IsNullOrEmpty(model.Password) || x.Password == model.Password) &&
                            (!model.Id.HasValue || x.Id == model.Id))
                ?.GetViewModel;
        }
        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;
        }
    }
}
