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 RouteReserveStorage : IRouteReserveStorage
    {
        public RouteReserveViewModel? Delete(RouteReserveBindingModel model)
        {
            using var context = new ZooDatabase();
            var RouteReserve = context.RouteReserves.FirstOrDefault(rec => rec.Id == model.Id);
            if (RouteReserve != null)
            {
                context.RouteReserves.Remove(RouteReserve);
                context.SaveChanges();
                return RouteReserve.GetViewModel;
            }
            return null;
        }

        public RouteReserveViewModel? GetElement(RouteReserveSearchModel model)
        {
            if (model == null)
            {
                return null;
            }
            using var context = new ZooDatabase();
            if (model.Id.HasValue)
            {
                return context.RouteReserves.Include(x => x.Route)
                    .Include(x => x.Reserve)
                    .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
            }
            return null;
        }

        public List<RouteReserveViewModel> GetFilteredList(RouteReserveSearchModel model)
        {
            using var context = new ZooDatabase();
            return context.RouteReserves
                .Where(x => x.Id == model.Id)
                .Include(x => x.Route)
                .Include(x => x.Reserve)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public List<RouteReserveViewModel> GetFullList()
        {
            using var context = new ZooDatabase();
            return context.RouteReserves
                .Include(x => x.Route)
                .Include(x => x.Reserve)
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public RouteReserveViewModel? Insert(RouteReserveBindingModel model)
        {
            using var context = new ZooDatabase();
            var newRouteReserve = RouteReserve.Create(context, model);
            if (newRouteReserve == null)
            {
                return null;
            }
            context.RouteReserves.Add(newRouteReserve);
            context.SaveChanges();
            return newRouteReserve.GetViewModel;
        }

        public RouteReserveViewModel? Update(RouteReserveBindingModel model)
        {
            using var context = new ZooDatabase();
            var RouteReserve = context.RouteReserves.FirstOrDefault(x => x.Id == model.Id);
            if (RouteReserve == null)
            {
                return null;
            }
            RouteReserve.Update(model);
            context.SaveChanges();
            return RouteReserve.GetViewModel;
        }
    }
}
