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

namespace ZooDatabaseImplements.Implements
{
    public class ReserveStorage : IReserveStorage
    {
        public List<ReserveViewModel> GetFullList()
        {
            using var context = new ZooDatabase();
            return context.Reserves
                .Include(x => x.Employee)
                .Select(x => x.GetViewModel)
                .ToList();
        }
        public List<ReserveViewModel> GetFilteredList(ReserveSearchModel model)
        {
            using var context = new ZooDatabase();
            return context.Reserves
            .Where(x => x.Id == model.Id)
            .Include(x => x.Employee)
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public ReserveViewModel? GetElement(ReserveSearchModel model)
        {
            using var context = new ZooDatabase();
            if (model.Id.HasValue)//сначала ищем по Id
            {
                return context.Reserves
                    .Include(x => x.Employee)
                    .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
            }
            if (!string.IsNullOrEmpty(model.ReserveName))//ищем по названию
            {
                return context.Reserves
                       .Include(x => x.Employee)
                    .FirstOrDefault(x => x.ReserveName == model.ReserveName)?.GetViewModel;
            }
            return null;
        }
        public ReserveViewModel? Insert(ReserveBindingModel model)
        {
            using var context = new ZooDatabase();
            var newReserve = Reserve.Create(context, model);
            if (newReserve == null)
            {
                return null;
            }
            context.Reserves.Add(newReserve);
            context.SaveChanges();
            return newReserve.GetViewModel;
        }
        public ReserveViewModel? Update(ReserveBindingModel model)
        {
            using var context = new ZooDatabase();
            var Reserve = context.Reserves.FirstOrDefault(x => x.Id == model.Id);
            if (Reserve == null)
            {
                return null;
            }
            Reserve.Update(model);
            context.SaveChanges();
            return Reserve.GetViewModel;
        }
        public ReserveViewModel? Delete(ReserveBindingModel model)
        {
            using var context = new ZooDatabase();
            var element = context.Reserves.FirstOrDefault(x => x.Id == model.Id);
            if (element != null)
            {
                context.Reserves.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
