using ComputerShopDataModels.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;

namespace ComputerShopDatabaseImplement.Models
{
    internal class Assembly : IAssemblyModel
    {
        public int Id { get; set; }
        [Required]
        public string AssemblyName { get; set; } = string.Empty;
        [Required]
        public double Price { get; set; }
        [Required]
        public int ClientId { get; private set; }
        private Dictionary<int, (IComponentModel, int)>? _assemblyComponents = null;
        public Dictionary<int, (IComponentModel, int)> AssemblyComponents
        {
            get
            {
                if (_assemblyComponents == null)
                {
                    _assemblyComponents = Components
                    .ToDictionary(recPC => recPC.ComponentId, recPC =>
                    (recPC.Component as IComponentModel, recPC.Count));
                }
                return _assemblyComponents;
            }
        }

        public virtual List<AssemblyComponent> Components { get; set; } = new();
        public virtual Client Client { get; set; }
        public static Assembly Create(ComputerShopDatabase context, AssemblyBindingModel model)
        {
            return new Assembly()
            {
                Id = model.Id,
                AssemblyName = model.AssemblyName,
                Price = model.Price,
                ClientId = model.ClientId,
                Components = model.AssemblyComponents.Select(x => new
                AssemblyComponent
                {
                    Component = context.Components.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList(),
            };
        }
        public void Update(AssemblyBindingModel model)
        {
            AssemblyName = model.AssemblyName;
            Price = model.Price;
        }
        public AssemblyViewModel GetViewModel => new()
        {
            Id = Id,
            AssemblyName = AssemblyName,
            Price = Price,
            AssemblyComponents = AssemblyComponents,
            ClientId = ClientId
        };
        public void UpdateComponents(ComputerShopDatabase context, AssemblyBindingModel model)
        {
            var assemblyComponents = context.AssemblyComponents.Where(rec => rec.Id == model.Id).ToList();
            if (assemblyComponents != null && assemblyComponents.Count > 0)
            { // удалили те, которых нет в модели
                context.AssemblyComponents.RemoveRange(assemblyComponents.Where(rec
                => !model.AssemblyComponents.ContainsKey(rec.ComponentId)));
                context.SaveChanges();
                // обновили количество у существующих записей
                foreach (var updateComponent in assemblyComponents)
                {
                    updateComponent.Count = model.AssemblyComponents[updateComponent.ComponentId].Item2;
                    model.AssemblyComponents.Remove(updateComponent.ComponentId);
                }
                context.SaveChanges();
            }
            var assembly = context.Assemblies.First(x => x.Id == Id);
            foreach (var pc in model.AssemblyComponents)
            {
                context.AssemblyComponents.Add(new AssemblyComponent
                {
                    Assembly = assembly,
                    Component = context.Components.First(x => x.Id == pc.Key),
                    Count = pc.Value.Item2
                });
                context.SaveChanges();
            }
            _assemblyComponents = null;
        }
    }
}
