using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SchoolAgainStudyDataBaseImplements.Models
{
    public class Lesson : ILesson
    {
        [Required]
        public string Title { get; set; } = string.Empty;
        [Required]
        public DateTime DateEvent { get; set; }
        [Required]
        public int ProductId { get; set; }

        public string ProductName { get; set; } = string.Empty;

        public virtual Product Product { get; set; }
        [Required]
        public int TeacherId { get; set; }
        public string TeacherName { get; set; } = string.Empty;
        public virtual Teacher Teacher { get; set; }
        private Dictionary<int, IMaterial>? _LessonMaterials = null;
        [NotMapped]
        public Dictionary<int, IMaterial> LessonMaterials 
        { get 
            {
                if (_LessonMaterials == null)
                {
                    _LessonMaterials = Materials
                            .ToDictionary(recPC => recPC.MaterialId, recPC => (recPC.Material as IMaterial));
                }
                return _LessonMaterials;
            } 
        }

        public int Id { get; set; }

        [ForeignKey("LessonId")]
        public virtual List<LessonMaterial> Materials { get; set; } = new();
        public static Lesson Create(SchoolDataBase context, LessonBindingModel model)
        {
            return new Lesson()
            {
                Id = model.Id,
                Title = model.Title,
                DateEvent = model.DateEvent,
                ProductId = model.ProductId,
                ProductName = model.ProductName,
                TeacherId = model.TeacherId,
                TeacherName=model.TeacherName,
                Materials = model.LessonMaterials.Select(x => new LessonMaterial
                {
                    Material = context.Materials.First(y => y.Id == x.Key),
                }).ToList()
            };
        }

        public void Update(LessonBindingModel model)
        {
            Title = model.Title;
            ProductId = model.ProductId;
            ProductName = model.ProductName;
            DateEvent = model.DateEvent;       
        }

        public LessonViewModel GetViewModel => new()
        {
            Id = Id,
            Title = Title,
            DateEvent = DateEvent,
            ProductId = ProductId,
            ProductName = ProductName,
            TeacherId = TeacherId,
            TeacherName=TeacherName,
            LessonMaterials = LessonMaterials
        };

        public void UpdateMaterials(SchoolDataBase context, LessonBindingModel model)
        {
            var lessonMaterials = context.LessonMaterials.Where(rec => rec.LessonId == model.Id).ToList();
            if (lessonMaterials != null && lessonMaterials.Count > 0)
            {
                context.LessonMaterials.RemoveRange(lessonMaterials.Where(rec => !model.LessonMaterials.ContainsKey(rec.MaterialId)));
                context.SaveChanges();

                foreach (var updateMaterial in lessonMaterials)
                {
                    model.LessonMaterials.Remove(updateMaterial.MaterialId);
                }
                context.SaveChanges();
            }
            var lesson = context.Lessons.First(x => x.Id == Id);
            foreach (var pc in model.LessonMaterials)
            {
                context.LessonMaterials.Add(new LessonMaterial
                {
                    Lesson = lesson,
                    Material = context.Materials.First(x => x.Id == pc.Key),
                });
                context.SaveChanges();
            }
            _LessonMaterials = null;
        }
    }
}