using SecureShopContracts.BindingModels;
using SecureShopContracts.ViewModels;
using SecureShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SecureShopDatabaseImplement.Models
{
    public class Secure : ISecureModel
    {
        public int Id { get; set; }

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

        [Required]
        public double Price { get; set; }

        private Dictionary<int, (IFacilitiesModel, int)>? _SecureFacilitiess = null;

        [NotMapped]
        public Dictionary<int, (IFacilitiesModel, int)> SecureFacilitiess
        {
            get
            {
                if (_SecureFacilitiess == null)
                {
                    _SecureFacilitiess = Facilitiess
                            .ToDictionary(recPC => recPC.FacilitiesId, recPC => (recPC.Facilities as IFacilitiesModel, recPC.Count));
                }
                return _SecureFacilitiess;
            }
        }

        [ForeignKey("SecureId")]
        public virtual List<SecureFacilities> Facilitiess { get; set; } = new();

        [ForeignKey("SecureId")]
        public virtual List<Order> Orders { get; set; } = new();

        public static Secure Create(SecureShopDatabase context, SecureBindingModel model)
        {
            return new Secure()
            {
                Id = model.Id,
                SecureName = model.SecureName,
                Price = model.Price,
                Facilitiess = model.SecureFacilitiess.Select(x => new SecureFacilities
                {
                    Facilities = context.Facilitiess.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }

        public void Update(SecureBindingModel model)
        {
            SecureName = model.SecureName;
            Price = model.Price;
        }

        public SecureViewModel GetViewModel => new()
        {
            Id = Id,
            SecureName = SecureName,
            Price = Price,
            SecureFacilitiess = SecureFacilitiess
        };

        public void UpdateFacilitiess(SecureShopDatabase context, SecureBindingModel model)
        {
            var SecureFacilitiess = context.SecureFacilitiess.Where(rec => rec.SecureId == model.Id).ToList();
            if (SecureFacilitiess != null && SecureFacilitiess.Count > 0)
            {   // удалили те, которых нет в модели
                context.SecureFacilitiess.RemoveRange(SecureFacilitiess.Where(rec => !model.SecureFacilitiess.ContainsKey(rec.FacilitiesId)));
                context.SaveChanges();
                // обновили количество у существующих записей
                foreach (var updateFacilities in SecureFacilitiess)
                {
                    updateFacilities.Count = model.SecureFacilitiess[updateFacilities.FacilitiesId].Item2;
                    model.SecureFacilitiess.Remove(updateFacilities.FacilitiesId);
                }
                context.SaveChanges();
            }
            var Secure = context.Secures.First(x => x.Id == Id);
            foreach (var ia in model.SecureFacilitiess)
            {
                context.SecureFacilitiess.Add(new SecureFacilities
                {
                    Secure = Secure,
                    Facilities = context.Facilitiess.First(x => x.Id == ia.Key),
                    Count = ia.Value.Item2
                });
                context.SaveChanges();
            }
            _SecureFacilitiess = null;
        }
    }
}
