using GiftShopDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;

namespace GiftShopDatabaseImplement.Models
{
    public class Gift : IGiftModel
    {
        public int Id { get; set; }
        
        [Required]
        public string GiftName { get; set; } = string.Empty;
        
        [Required]
        public double Price { get; set; }
        
        private Dictionary<int, (IComponentModel, int)>? _giftComponents = null;
        
        [NotMapped]
        public Dictionary<int, (IComponentModel, int)> GiftComponents
        {
            get
            {
                if (_giftComponents == null)
                {
                    _giftComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
                   (recPC.Component as IComponentModel, recPC.Count));
                }
                return _giftComponents;
            }
        }
        
        [ForeignKey("GiftId")]
        public virtual List<GiftComponent> Components { get; set; } = new();
        
        [ForeignKey("GiftId")]
        public virtual List<Order> Orders { get; set; } = new();
        
        public static Gift Create(GiftShopDatabase context, GiftBindingModel model)
        {
            return new Gift()
            {
                Id = model.Id,
                GiftName = model.GiftName,
                Price = model.Price,
                Components = model.GiftComponents.Select(x => new GiftComponent
                {
                    Component = context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2
                }).ToList()
            };
        }
        
        public void Update(GiftBindingModel model)
        {
            GiftName = model.GiftName;
            Price = model.Price;
        }
        
        public GiftViewModel GetViewModel => new()
        {
            Id = Id,
            GiftName = GiftName,
            Price = Price,
            GiftComponents = GiftComponents
        };
        
        public void UpdateComponents(GiftShopDatabase context, GiftBindingModel model)
        {
            var giftComponents = context.GiftComponents.Where(rec => rec.GiftId == model.Id).ToList();
            if (giftComponents != null && giftComponents.Count > 0)
            { // удалили те, которых нет в модели
                context.GiftComponents.RemoveRange(giftComponents.Where
                    (rec => !model.GiftComponents.ContainsKey(rec.ComponentId)));
                context.SaveChanges();
                // обновили количество у существующих записей
                foreach (var updateComponent in giftComponents)
                {
                    updateComponent.Count = model.GiftComponents[updateComponent.ComponentId].Item2;
                    model.GiftComponents.Remove(updateComponent.ComponentId);
                }
                context.SaveChanges();
            }
            var gift = context.Gifts.First(x => x.Id == Id);
            foreach (var pc in model.GiftComponents)
            {
                context.GiftComponents.Add(new GiftComponent
                {
                    Gift = gift,
                    Component = context.Components.First(x => x.Id == pc.Key),
                    Count = pc.Value.Item2
                });
                context.SaveChanges();
            }
            _giftComponents = null;
        }
    }
}