using SewingDressesContracts.BindingModels;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;


namespace SewingDressesDatabaseImplement.Models
{
    public class Dress : IDressModel
    {
        public int Id { get; set; }
        [Required]
        public string DressName { get; set; } = string.Empty;
        [Required]
        public double Price { get; set; }
        private Dictionary<int, (IComponentModel, int)>? _dressComponents = null;
        [NotMapped]
        public Dictionary<int, (IComponentModel, int)> DressComponents
        {
            get
            {
                if (_dressComponents == null)
                {
                    _dressComponents = Components.ToDictionary(recDC => recDC.ComponentId, recDC => (recDC.Component as IComponentModel, recDC.Count));
                }
                return _dressComponents;
            }
        }
        [ForeignKey("DressId")]
        public virtual List<DressComponent> Components { get; set; } = new();
        [ForeignKey("DressId")]
        public virtual List<Order> Orders { get; set; } = new();
        public static Dress Create(SewingDressesDatabase context, DressBindingModel model)
        {
            return new Dress()
            {
                Id = model.Id,
                DressName = model.DressName,
                Price = model.Price,
                Components = model.DressComponents.Select(x => new DressComponent
                {
                    Component = context.Components.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }
        public void Update(DressBindingModel model)
        {
            DressName = model.DressName;
            Price = model.Price;
        }
        public DressViewModel GetViewModel => new()
        { 
            Id = Id,
            DressName = DressName,
            Price = Price,
            DressComponents = DressComponents
        };
        public void UpdateComponents(SewingDressesDatabase context, DressBindingModel model)
        {
            var dressComponents = context.DressComponents.Where(rec => rec.DressId == model.Id).ToList();
            if (dressComponents != null && dressComponents.Count > 0)
            {
                context.DressComponents.RemoveRange(dressComponents.Where(rec => !model.DressComponents.ContainsKey(rec.ComponentId)));
                context.SaveChanges();
                foreach (var updateComponent in dressComponents)
                {
                    updateComponent.Count = model.DressComponents[updateComponent.ComponentId].Item2;
                    model.DressComponents.Remove(updateComponent.ComponentId);
                }
                context.SaveChanges();
            }
            var dress = context.Dresses.First(x => x.Id == Id);
            foreach (var pc in model.DressComponents)
            {
                context.DressComponents.Add(new DressComponent
                {
                    Dress = dress,
                    Component = context.Components.First(x => x.Id == pc.Key),
                    Count = pc.Value.Item2
                });
                context.SaveChanges();
            }
            _dressComponents = null;
        }

    }
}
