using VetClinicDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VetClinicContracts.BindingModels;
using VetClinicContracts.ViewModels;
using System.Collections;
using System.ComponentModel;

namespace VetClinicDataBaseImplement.Models
{
	public class Visit : IVisitModel
	{
		public int Id { get; set; }
		[Required]
		public int AdminId { get; set; }

		[Required]
		public string NameVisit { get; set; } = string.Empty;

		[Required]
		public DateTime DateVisit { get; set; }

		[ForeignKey("VisitId")]
		public virtual List<VisitAnimal> Animals { get; set; } = new();

	    [ForeignKey("VisitId")]
	   public virtual List<VisitService> Services { get; set; } = new();
		public virtual Admin Admin { get; set; }
		public Dictionary<int, IAnimalModel>? _visitAnimals = null;

		public Dictionary<int, IServiceModel>? _serviceVisits = null;

		[NotMapped]
		public Dictionary<int, IAnimalModel> VisitAnimals
		{
			get
			{
				if (_visitAnimals == null)
				{
					_visitAnimals = Animals.ToDictionary(recPC => recPC.AnimalId, recPC =>
				   recPC.Animal as IAnimalModel);
				}
				return _visitAnimals;
			}
		}
		[NotMapped]
		public Dictionary<int, IServiceModel> ServiceVisits
		{
			get
			{
				if (_serviceVisits == null)
				{
					_serviceVisits = Services.ToDictionary(recPC => recPC.ServiceId, recPC =>
				   recPC.Service as IServiceModel);
				}
				return _serviceVisits;
			}
		}
		public static Visit? Create(VetClinicDatabase context, VisitBindingModel? model)
		{
			if (model == null)
			{
				return null;
			}
			return new Visit()
			{
				Id = model.Id,
				AdminId = model.AdminId,
			
				NameVisit = model.NameVisit,
				DateVisit = model.DateVisit,
				Animals = model.VisitAnimals.Select(x => new VisitAnimal
				{
					Animal = context.Animals.First(y => y.Id == x.Key)
				}).ToList(),
				Services = model.ServiceVisits.Select(x => new VisitService
				{
					Service = context.Services.First(y => y.Id == x.Key)
				}).ToList(),
			};
		}
		public void Update(VisitBindingModel? model)
		{
			if (model == null)
			{
				return;
			}
			NameVisit = model.NameVisit;
			DateVisit = model.DateVisit;
		}
		public VisitViewModel GetViewModel => new()
		{
			Id = Id,
			VisitAnimals = VisitAnimals,
			AdminId = AdminId,
			NameVisit = NameVisit,
			DateVisit = DateVisit,
			ServiceVisits = ServiceVisits

		};
		public void UpdateAnimals(VetClinicDatabase context, VisitBindingModel model)
		{
			var visitAnimals = context.VisitAnimals.Where(rec => rec.VisitId == model.Id).ToList();
			if (visitAnimals != null)
			{ // удалили те, которых нет в модели
				context.VisitAnimals.RemoveRange(visitAnimals.Where(rec => !model.VisitAnimals.ContainsKey(rec.AnimalId)));
				context.SaveChanges();

			}
			var visit = context.Visits.First(x => x.Id == Id);
			foreach (var pc in model.VisitAnimals)
			{
				context.VisitAnimals.Add(new VisitAnimal
				{
					Visit = visit,
					Animal = context.Animals.First(x => x.Id == pc.Key),

				});
				context.SaveChanges();
			}
			_visitAnimals = null;
		}
		public void UpdateService(VetClinicDatabase context, VisitBindingModel model)
		{
			var serviceVisits = context.ServiceVisits.Where(rec => rec.VisitId == model.Id).ToList();
			if (serviceVisits != null)
			{ // удалили те, которых нет в модели
				context.ServiceVisits.RemoveRange(serviceVisits.Where(rec => !model.ServiceVisits.ContainsKey(rec.ServiceId)));
				context.SaveChanges();

			}
			var visit = context.Visits.First(x => x.Id == Id);
			foreach (var pc in model.ServiceVisits)
			{
				context.ServiceVisits.Add(new VisitService
				{
					Visit = visit,
					Service = context.Services.First(x => x.Id == pc.Key),

				});
				context.SaveChanges();
			}
			_serviceVisits = null;
		}
	}

}
