using MotorPlantContracts.BindingModels;
using MotorPlantContracts.ViewModels;
using MotorPlantDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

namespace MotorPlantDatabaseImplement.Models
{
    [DataContract]
    public class Engine : IEngineModel
    {
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        [Required]
        public string EngineName { get; set; } = string.Empty;
        [DataMember]
        [Required]
        public double Price { get; set; }
        private Dictionary<int, (IComponentModel, int)>? _EngineComponents = null;
        [DataMember]
        [NotMapped]
        public Dictionary<int, (IComponentModel, int)> EngineComponents
        {
            get
            {
                if (_EngineComponents == null)
                {
                    _EngineComponents = Components
                    .ToDictionary(recPC => recPC.ComponentId, recPC =>
                   (recPC.Component as IComponentModel, recPC.Count));
                }
                return _EngineComponents;
            }
        }
        [ForeignKey("EngineId")]
        public virtual List<EngineComponent> Components { get; set; } = new();
        [ForeignKey("EngineId")]
        public virtual List<Order> Orders { get; set; } = new();
        public static Engine Create(MotorPlantDataBase context,
        EngineBindingModel model)
        {
            return new Engine()
            {
                Id = model.Id,
                EngineName = model.EngineName,
                Price = model.Price,
                Components = model.EngineComponents.Select(x => new
               EngineComponent
                {
                    Component = context.Components.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }
        public void Update(EngineBindingModel model)
        {
            EngineName = model.EngineName;
            Price = model.Price;
        }
        public EngineViewModel GetViewModel => new()
        {
            Id = Id,
            EngineName = EngineName,
            Price = Price,
            EngineComponents = EngineComponents
        };
        public void UpdateComponents(MotorPlantDataBase context,
       EngineBindingModel model)
        {
            var EngineComponents = context.EngineComponents.Where(rec =>
           rec.EngineId == model.Id).ToList();
            if (EngineComponents != null && EngineComponents.Count > 0)
            {
                context.EngineComponents.RemoveRange(EngineComponents.Where(rec
=> !model.EngineComponents.ContainsKey(rec.ComponentId)));

                context.SaveChanges();
                foreach (var updateComponent in EngineComponents)
                {
                    updateComponent.Count =
                   model.EngineComponents[updateComponent.ComponentId].Item2;
                    model.EngineComponents.Remove(updateComponent.ComponentId);
                }
                context.SaveChanges();
            }
            var Engine = context.Engines.First(x => x.Id == Id);
            foreach (var pc in model.EngineComponents)
            {
                context.EngineComponents.Add(new EngineComponent
                {
                    Engine = Engine,
                    Component = context.Components.First(x => x.Id == pc.Key),
                    Count = pc.Value.Item2
                });
                context.SaveChanges();
            }
            _EngineComponents = null;
        }
    }
}
