using ComputersShopDataModels.Models;
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 ComputersShopContracts.BindingModels;
using ComputersShopContracts.ViewModels;

namespace ComputersShopDataBaseImplement.Models
{
    public class Computer : IComputerModel
    {
        public int Id { get; set; }
        [Required]
        public string ComputerName { get; set; } = string.Empty;
        [Required]
        public double Price { get; set; }
        private Dictionary<int, (IComponentModel, int)>? _ComputerComponents = null;
        [NotMapped]
        public Dictionary<int, (IComponentModel, int)> ComputerComponents
        {
            get
            {
                if (_ComputerComponents == null)
                {
                    _ComputerComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC =>
                   (recPC.Component as IComponentModel, recPC.Count));
                }
                return _ComputerComponents;
            }
        }
        [ForeignKey("ComputerId")]
        public virtual List<ComputerComponent> Components { get; set; } = new();
        [ForeignKey("ComputerId")]
        public virtual List<Order> Orders { get; set; } = new();
        public static Computer Create(ComputersShopDataBase context, ComputerBindingModel model)
        {
            return new Computer()
            {
                Id = model.Id,
                ComputerName = model.ComputerName,
                Price = model.Price,
                Components = model.ComputerComponents.Select(x => new ComputerComponent
                {
                    Component = context.Components.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }
        public void Update(ComputerBindingModel model)
        {
            ComputerName = model.ComputerName;
            Price = model.Price;
        }
        public ComputerViewModel GetViewModel => new()
        {
            Id = Id,
            ComputerName = ComputerName,
            Price = Price,
            ComputerComponents = ComputerComponents
        };
        public void UpdateComponents(ComputersShopDataBase context,
        ComputerBindingModel model)
        {
            var ComputerComponents = context.ComputerComponents.Where(rec =>
           rec.ComputerId == model.Id).ToList();
            if (ComputerComponents != null && ComputerComponents.Count > 0)
            { // удалили те, которых нет в модели
                context.ComputerComponents.RemoveRange(ComputerComponents.Where(rec
               => !model.ComputerComponents.ContainsKey(rec.ComponentId)));
                context.SaveChanges();
                // обновили количество у существующих записей
                foreach (var updateComponent in ComputerComponents)
                {
                    updateComponent.Count = model.ComputerComponents[updateComponent.ComponentId].Item2;
                    model.ComputerComponents.Remove(updateComponent.ComponentId);
                }
                context.SaveChanges();
            }
            var Computer = context.Computers.First(x => x.Id == Id);
            foreach (var pc in model.ComputerComponents)
            {
                context.ComputerComponents.Add(new ComputerComponent
                {
                    Computer = Computer,
                    Component = context.Components.First(x => x.Id == pc.Key),
                    Count = pc.Value.Item2
                });
                context.SaveChanges();
            }
            _ComputerComponents = null;
        }
    }
}

