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 PaymentStorage : IPaymentStorage
    {
        public List<PaymentViewModel> GetFullList()
        {
            using var context = new ZooDatabase();
            return context.Payments
            .Include(rec => rec.RouteReserve)
            .Select(x => x.GetViewModel)
           .ToList();
        }

        public List<PaymentViewModel> GetFilteredList(PaymentSearchModel model)
        {
            if (model == null)
            {
                return null;
            }
            using var context = new ZooDatabase();
            var list = context.Payments.Where(x => x.RouteReserveId == model.RouteReserveId)
            .OrderBy(x => x.DateOfPayment)
            .Include(x => x.RouteReserve);
            return list
            .Select(x => new PaymentViewModel
            {
                DateOfPayment = x.DateOfPayment,
                Id = x.Id,
                PaySum = x.PaySum,
                RouteReserveId = x.RouteReserveId
            })
             .ToList();
        }
        public PaymentViewModel GetElement(PaymentSearchModel model)
        {
            if (model == null)
            {
                return null;
            }
            using var context = new ZooDatabase();
            {
                var payment = context.Payments.Include(x => x.RouteReserve)
                  .FirstOrDefault(x => x.RouteReserveId == model.RouteReserveId);
                return payment != null ? new PaymentViewModel
                {
                    DateOfPayment = payment.DateOfPayment,
                    Id = payment.Id,
                    PaySum = payment.PaySum,
                    RouteReserveId = payment.RouteReserveId
                } :
                null;
            }
        }
        public PaymentViewModel? Insert(PaymentBindingModel model)
        {
            var context = new ZooDatabase();
            var newPayment = Payment.Create(context, model);
            if (newPayment == null)
            {
                return null;
            }
            context.Payments.Add(newPayment);
            context.SaveChanges();
            return newPayment.GetViewModel;
        }

        public PaymentViewModel? Update(PaymentBindingModel model)
        {
            var context = new ZooDatabase();
            var element = context.Payments.
                Include(x => x.RouteReserve)
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element == null)
            {
                throw new Exception("Оплата не найдена");
            }
            element.Update(model);
            context.SaveChanges();
            return element.GetViewModel;
        }
        public PaymentViewModel? Delete(PaymentBindingModel model)
        {
            var context = new ZooDatabase();

            var element = context.Payments
                .Include(x => x.RouteReserve)
                .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Payments.Remove(element);
                context.SaveChanges();
                return element?.GetViewModel;
            }
            else
            {
                throw new Exception("Оплата не найдена");
            }
        }
    }
}
