using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HospitalDataBaseImplements.Models
{
    public class Recipes : IRecipesModel
    {
        public int Id { get; private set; }
        [Required]
        public string Dose { get; private set; } = string.Empty;
        [Required]
        public DateTime Date { get; private set; } = DateTime.Now;
        [Required]
        public int ClientId { get; set; }
        public virtual Medicines Medicines { get; set; } = new();
        public int MedicinesId { get; private set; }
        [Required]
        public string ModeOfApplication { get; private set; } = string.Empty;
        public Client Client { get; set; }
        private Dictionary<int, IProceduresModel>? _recipeProcedures = null;
        [NotMapped]
        public Dictionary<int, IProceduresModel> RecipeProcedures
        {
            get
            {
                if (_recipeProcedures == null)
                {
                   _recipeProcedures = Procedures.ToDictionary(recPC => recPC.ProcedureId, recPC =>
                   recPC.Procedure as IProceduresModel);
                }
                return _recipeProcedures;
            }
        }
        [ForeignKey("RecipesId")] 
        public virtual List<RecipesProcedures> Procedures { get; set; } = new();

        private Dictionary<int, ISymptomsModel>? _recipeSymptoms = null;
        [NotMapped]
        public Dictionary<int, ISymptomsModel> RecipeSymptoms
        {
            get
            {
                if (_recipeSymptoms == null)
                {
                    _recipeSymptoms = Symptoms.ToDictionary(recPC => recPC.SymptomsId, recPC => (recPC.Symptoms as ISymptomsModel));
                }
                return _recipeSymptoms;
            }
        }
        public virtual List<RecipesSymptoms> Symptoms { get; set; } = new();

        public static Recipes Create(HospitalDatabase context, RecipesBindingModel model)
        {
            return new Recipes()
            {
                Id = model.Id,
                Date = model.Date,
                MedicinesId = model.MedicinesId,
                ClientId = model.ClientId,
                Procedures = model.RecipeProcedures.Select(x => new RecipesProcedures
                {
                    Procedure = context.Procedures.First(y => y.Id == x.Key),
                }).ToList(),
                Symptoms = model.RecipeSymptoms.Select(x => new RecipesSymptoms
                {
                    Symptoms = context.Symptomses.First(y => y.Id == x.Key),
                }).ToList()
            };
        }
        public void Update(RecipesBindingModel model)
        {
            Date = model.Date;
            MedicinesId = model.MedicinesId;
        }
        public RecipesViewModel GetViewModel
        {
            get {
                using var context = new HospitalDatabase();
                return new RecipesViewModel
                {
                    Id = Id,
                    Date = Date,
                    ClientId = ClientId,
                    ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
                    RecipeProcedures = RecipeProcedures,
                    RecipeSymptoms = RecipeSymptoms
                };
            }
        }
        public void UpdateProcedures(HospitalDatabase context, RecipesBindingModel model)
        {
            var recipeProcedures = context.RecipesProcedures.Where(rec => rec.RecipesId == model.Id).ToList();
            if (recipeProcedures != null && recipeProcedures.Count > 0)
            { // удалили те, которых нет в модели
                context.RecipesProcedures.RemoveRange(recipeProcedures.Where(rec
               => !model.RecipeProcedures.ContainsKey(rec.ProcedureId)));
                context.SaveChanges();
            }
            var recipe = context.Recipes.First(x => x.Id == Id);
            foreach (var pc in model.RecipeProcedures)
            {
                context.RecipesProcedures.Add(new RecipesProcedures
                {
                    Recipe = recipe,
                    Procedure = context.Procedures.First(x => x.Id == pc.Key)
                });
                context.SaveChanges();
            }
            _recipeProcedures = null;
        }
        public void UpdateSymptomses(HospitalDatabase context, RecipesBindingModel model)
        {
            var recipeSymptomses = context.RecipesSymptoms.Where(rec => rec.RecipesId == model.Id).ToList();
            if (recipeSymptomses != null && recipeSymptomses.Count > 0)
            { // удалили те, которых нет в модели
                context.RecipesSymptoms.RemoveRange(recipeSymptomses.Where(rec
               => !model.RecipeSymptoms.ContainsKey(rec.SymptomsId)));
                context.SaveChanges();
            }
            var recipe = context.Recipes.First(x => x.Id == Id);
            foreach (var pc in model.RecipeSymptoms)
            {
                context.RecipesSymptoms.Add(new RecipesSymptoms
                {
                    Recipe = recipe,
                    Symptoms = context.Symptomses.First(x => x.Id == pc.Key)
                });
                context.SaveChanges();
            }
            _recipeSymptoms = null;
        }
    }
}