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 CarCenterDataModels.Models;
using CarCenterContracts.BindingModels;
using CarCenterContracts.ViewModels;
using System.Diagnostics;

namespace CarCenterDataBaseImplement.Models
{
    public class Equipment : IEquipmentModel
    {
        public int Id { get; private set; }
        [Required]
        public string EquipmentName { get; set; } = string.Empty;

        [Required]
        public double EquipmentPrice { get; set; }
        public DateTime DateCreateEquipment { get; set; } = DateTime.Now;

        public int AdministratorId { get; private set; }
        public int? PreSaleWorkId { get; private set; }

		public virtual Administrator Administrator { get; set; }
        public virtual PreSaleWork? PreSaleWork { get; set; }

		[ForeignKey("EquipmentId")]
        public virtual List<EquipmentCar> Cars { get; set; } = new();

        private Dictionary<int, ICarModel> _equipmentCars = null;
        /*[NotMapped]
        public Dictionary<int, ICarModel> EquipmentCars
        {
            get
            {
                if (_equipmentCars == null)
                {
                    *//*using var context = new CarCenterDataBase();*//*
                    _equipmentCars = Cars
                        *//*.ToDictionary(x => x.CarId, x => (context.Cars
                        .FirstOrDefault(y => y.Id == x.CarId)! as ICarModel));*//*
                        .ToDictionary(recPC => recPC.CarId, recPC =>
                   (recPC.Car as ICarModel));
                }
                return _equipmentCars;
            }
        }*/

        [NotMapped]
        public Dictionary<int, ICarModel> EquipmentCars
        {
            get
            {
                if (_equipmentCars == null)
                {

                    _equipmentCars = Cars
                    .ToDictionary(x => x.CarId, x => (x.Car as ICarModel));
                }
                return _equipmentCars;
            }
        }

        public static Equipment Create(CarCenterDataBase context, EquipmentBindingModel model)
        {
            return new Equipment()
            {
                Id = model.Id,
                EquipmentName = model.EquipmentName,
                EquipmentPrice = model.EquipmentPrice,
                DateCreateEquipment = model.DateCreateEquipment,
                AdministratorId = model.AdministratorId,
                PreSaleWorkId = model.PreSaleWorkId,
				

				Cars = model.EquipmentCars.Select(x => new EquipmentCar
                {
                    Car = context.Cars.First(y => y.Id == x.Key),
                }).ToList()
            };
        }

        public void Update(EquipmentBindingModel model)
        {
            EquipmentName = model.EquipmentName;
            EquipmentPrice = model.EquipmentPrice;
            AdministratorId = model.AdministratorId;
            PreSaleWorkId = model.PreSaleWorkId;
        }

        public EquipmentViewModel GetViewModel => new()
        {
            Id = Id,
            EquipmentName = EquipmentName,
            AdministratorId = AdministratorId,
            PreSaleWorkId = PreSaleWorkId,
            EquipmentPrice = EquipmentPrice,
            EquipmentCars = EquipmentCars,
            DateCreateEquipment = DateCreateEquipment,
        };

        public void UpdateCars(CarCenterDataBase context, EquipmentBindingModel model)
        {
            var equipmentCars = context.EquipmentCars.Where(rec => rec.EquipmentId == model.Id).ToList();

            var list = new List<int>();
            foreach (var rec in model.EquipmentCars)
            {
                list.Add(rec.Key);
            }

            if (equipmentCars != null && equipmentCars.Count > 0)
            {
                context.EquipmentCars.RemoveRange(equipmentCars.Where(rec => !model.EquipmentCars.ContainsKey(rec.CarId)));
                context.SaveChanges();

                foreach (var updateCar in equipmentCars)
                {
                    model.EquipmentCars.Remove(updateCar.CarId);
                }
                context.SaveChanges();
            }

            var equipment = context.Equipments.First(x => x.Id == Id);

            foreach (var ec in model.EquipmentCars)
            {
                context.EquipmentCars.Add(new EquipmentCar
                {
                    Equipment = equipment,
                    Car = context.Cars.First(x => x.Id == ec.Key),
                });
                context.SaveChanges();
            }
            _equipmentCars = null;
        }
    }
}
