using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VeterinaryContracts.StorageContracts;
using VeterinaryContracts.BindingModels;
using VeterinaryContracts.SearchModels;
using VeterinaryContracts.ViewModels;
using VeterinaryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using VeterinaryDataModels;


namespace VeterinaryDatabaseImplement.Implements
{
	public class DrugStorage : IDrugStorage
	{
		public List<DrugViewModel> GetFullList()
		{
			using var context = new VeterinaryDatabase();
			return context.Drugs
			 .Include(x => x.Medications)
			.ThenInclude(x => x.Medication)
			.ToList()
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public List<DrugViewModel> GetFilteredList(DrugSearchModel model)
		{
			using var context = new VeterinaryDatabase();
			return context.Drugs.Where(x => x.DoctorId == model.DoctorId).Include(x => x.Medications)
			.ThenInclude(x => x.Medication)
			.Where(x => String.IsNullOrEmpty(model.DrugName) || x.DrugName.Contains(model.DrugName) &&
			(!model.DateCreate.HasValue || x.DateCreate >= model.DateCreate))
			.ToList()
			.Select(x => x.GetViewModel)
			.ToList();
		}
		public DrugViewModel? GetElement(DrugSearchModel model)
		{
			if (string.IsNullOrEmpty(model.DrugName) &&
			!model.Id.HasValue)
			{
				return null;
			}
			using var context = new VeterinaryDatabase();
			return context.Drugs.Include(x => x.Doctor)
			 .Include(x => x.Medications)
			 .ThenInclude(x => x.Medication)
			 .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DrugName) &&
			x.DrugName == model.DrugName) ||
			(model.Id.HasValue && x.Id ==
			model.Id))
			?.GetViewModel;
		}
		public DrugViewModel? Insert(DrugBindingModel model)
		{
			using var context = new VeterinaryDatabase();
			var newDrug = Drug.Create(context, model);
			if (newDrug == null)
			{
				return null;
			}
			context.Drugs.Add(newDrug);
			context.SaveChanges();
			return newDrug.GetViewModel;
		}
		public DrugViewModel? Update(DrugBindingModel model)
		{
			using var context = new VeterinaryDatabase();
			using var transaction = context.Database.BeginTransaction();
			try
			{
				var drug = context.Drugs.FirstOrDefault(rec =>
				rec.Id == model.Id);
				if (drug == null)
				{
					return null;
				}
				
				drug.UpdateMedications(context, model); // работает
				context.SaveChanges();
				drug.Update(model);// не работает сумма
				context.SaveChanges();
				transaction.Commit();
				return drug.GetViewModel;
			}
			catch
			{
				transaction.Rollback();
				throw;
			}
		}
		public DrugViewModel? Delete(DrugBindingModel model)
		{
			using var context = new VeterinaryDatabase();
			var element = context.Drugs
			.Include(x => x.Medications)
			.FirstOrDefault(rec => rec.Id == model.Id);
			if (element != null)
			{
				context.Drugs.Remove(element);
				context.SaveChanges();
				return element.GetViewModel;
			}
			return null;
		}
		public bool SellDrugs(DrugBindingModel model, int count)
		{
			if (model == null)
				return false;
			using var context = new VeterinaryDatabase();
			if (model.Count < count)
				return false;
			else
				model.Count -= count;
			context.SaveChanges();
			return true;
		}
	}
}
