using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.SearchModel;
using ZooContracts.StoragesContracts;
using ZooContracts.ViewModels;
using ZooDatabaseImplements;
using ZooDatabaseImplements.Models;

namespace ZooDatabaseImplements.Implements
{
    public class ClientStorage : IClientStorage
    {
        public List<ClientViewModel> GetFullList()
        {
            using var context = new ZooDatabase();
            return context.Clients
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
        {
            if (model == null)
            {
                return new();
            }
            if (model.Id.HasValue)
            {
                var res = GetElement(model);
                return res != null ? new() { res } : new();
            }
            if (model.ClientEmail != null)
            {
                using var context = new ZooDatabase();
                return context.Clients
                .Where(rec => rec.ClientEmail.Contains(model.ClientEmail))
                .Select(x => x.GetViewModel)
                .ToList();
            }
            return new();
        }
        public ClientViewModel? GetElement(ClientSearchModel model)
        {
            if (model == null)
            {
                return null;
            }
            using var context = new ZooDatabase();
            if (model.Id.HasValue)
                return context.Clients.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
            if (model.ClientEmail != null && model.ClientPassword != null)
                return context.Clients
                    .FirstOrDefault(x => x.ClientEmail == model.ClientEmail
                                      && x.ClientPassword == model.ClientPassword)?.GetViewModel;
            if (model.ClientEmail != null)
                return context.Clients.FirstOrDefault(x => x.ClientEmail == model.ClientEmail)?.GetViewModel;
            return null;
        }
        public ClientViewModel? Insert(ClientBindingModel model)
        {
            using var context = new ZooDatabase();
            if (model == null)
            {
                return null;
            }
            var newClient = Client.Create(model);
            if (newClient == null)
            {
                return null;
            }
            context.Clients.Add(newClient);
            context.SaveChanges();
            return newClient.GetViewModel;
        }
        public ClientViewModel? Update(ClientBindingModel model)
        {
            using var context = new ZooDatabase();
            var res = context.Clients.FirstOrDefault(x => x.Id == model.Id);
            res?.Update(model);
            context.SaveChanges();
            return res?.GetViewModel;
        }
        public ClientViewModel? Delete(ClientBindingModel model)
        {
            using var context = new ZooDatabase();
            var res = context.Clients.FirstOrDefault(x => x.Id == model.Id);
            if (res != null)
            {
                context.Clients.Remove(res);
                context.SaveChanges();
            }
            return res?.GetViewModel;
        }
    }
}
