using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using CarCenterContracts.SearchModels;
using CarCenterContracts.BindingModels;
using CarCenterDataBaseImplement.Models;

namespace CarCenterDataBaseImplement.Implements
{
    public class EquipmentStorage : IEquipmentStorage
    {
        public List<EquipmentViewModel> GetFullList()
        {
            using var context = new CarCenterDataBase();
            return context.Equipments
                .Include(x => x.Cars)
                .ThenInclude(x => x.Car)
                .ThenInclude(x => x.InspectionCar)
                .ThenInclude(x => x.Inspection)
                .Include(x => x.PreSaleWork)
                .Include(x => x.Administrator)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public EquipmentViewModel? GetElement(EquipmentSearchModel model)
        {
            if (!model.Id.HasValue && string.IsNullOrEmpty(model.EquipmentName))
            {
                return null;
            }

            using var context = new CarCenterDataBase();

            return context.Equipments
                    .Include(x => x.Cars)
                    .ThenInclude(x => x.Car)
                    .ThenInclude(x => x.InspectionCar)
                    .ThenInclude(x => x.Inspection)
                    .Include(x => x.PreSaleWork)
                    .Include(x => x.Administrator)
                    .FirstOrDefault(x => (!string.IsNullOrEmpty(model.EquipmentName) && x.EquipmentName == model.EquipmentName) || (model.Id.HasValue && x.Id == model.Id))?
                    .GetViewModel;
        }

        public List<EquipmentViewModel> GetFilteredList(EquipmentSearchModel model)
        {
            if (!model.Id.HasValue && !model.AdministratorId.HasValue)
            {
                return new();
            }

            using var context = new CarCenterDataBase();
			if (!model.DateFrom.HasValue && !model.DateTo.HasValue && model.DateFrom.HasValue)
			{
				return context.Equipments
					   .Include(x => x.Cars)
					   .ThenInclude(x => x.Car)
					   .ThenInclude(x => x.InspectionCar)
					   .ThenInclude(x => x.Inspection)
					   .Include(x => x.PreSaleWork)
					   .Include(x => x.Administrator)
						.Where(x => x.DateCreateEquipment >= model.DateFrom && x.DateCreateEquipment <= model.DateTo && x.AdministratorId == model.AdministratorId)
						.Select(x => x.GetViewModel)
						.ToList();
			}
			else if (model.AdministratorId.HasValue)
            {
                return context.Equipments
                    .Include(x => x.Cars)
                    .ThenInclude(x => x.Car)
                    .ThenInclude(x => x.InspectionCar)
                    .ThenInclude(x => x.Inspection)
                    .Include(x => x.PreSaleWork)
                    .Include(x => x.Administrator)
                    .Where(x => x.AdministratorId == model.AdministratorId)
                    .ToList()
                    .Select(x => x.GetViewModel)
                    .ToList();
            }

            return context.Equipments
                .Include(x => x.Cars)
                .ThenInclude(x => x.Car)
                .ThenInclude(x => x.InspectionCar)
                .ThenInclude(x => x.Inspection)
                .Include(x => x.PreSaleWork)
                .Include(x => x.Administrator)
                .Where(x => x.EquipmentName.Contains(model.EquipmentName))
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
        }

        public EquipmentViewModel? Insert(EquipmentBindingModel model)
        {
            using var context = new CarCenterDataBase();
            var newEquipment = Equipment.Create(context, model);

            if (newEquipment == null)
            {
                return null;
            }

            context.Equipments.Add(newEquipment);
            context.SaveChanges();

            return context.Equipments
                .Include(x => x.Cars)
                .ThenInclude(x => x.Car)
                .ThenInclude(x => x.InspectionCar)
                .ThenInclude(x => x.Inspection)
                .ThenInclude(x => x.Employee)
                .Include(x => x.PreSaleWork)
                .Include(x => x.Administrator)
               .FirstOrDefault(x => x.Id == newEquipment.Id)
               ?.GetViewModel;
        }

        public EquipmentViewModel? Update(EquipmentBindingModel model)
        {
            using var context = new CarCenterDataBase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var elem = context.Equipments
                    .Include(x => x.Cars)
                    .ThenInclude(x => x.Car)
                    .ThenInclude(x => x.InspectionCar)
                    .ThenInclude(x => x.Inspection)
                    .ThenInclude(x => x.Employee)
                    .FirstOrDefault(rec => rec.Id == model.Id);
                if (elem == null)
                {
                    return null;
                }

                elem.UpdateCars(context, model);
                context.SaveChanges();
                elem.Update(model);
                context.SaveChanges();
                transaction.Commit();
                return elem.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            } 
        }
        public EquipmentViewModel? Delete(EquipmentBindingModel model)
        {
            using var context = new CarCenterDataBase();

            var element = context.Equipments
                .Include(x => x.Cars)
                .ThenInclude(x => x.Car)
                .ThenInclude(x => x.InspectionCar)
                .ThenInclude(x => x.Inspection)
                .ThenInclude(x => x.Employee)
                .FirstOrDefault(rec => rec.Id == model.Id);

            if (element != null)
            {
                context.Equipments.Remove(element);
                context.SaveChanges();

                return element.GetViewModel;
            }

            return null;
        }
    }
}
