using System.ComponentModel.DataAnnotations;
using TypographyDataModels.Models;
using TypographyContracts.BindingModels;
using TypographyContracts.ViewModels;
using System.ComponentModel.DataAnnotations.Schema;
using TypographyDatabaseImplement.Models;

namespace TypographyDatabaseImplements.Models
{
    public class Printed : IPrintedModel
    {
        public int Id { get; set; }
        [Required]
        public string PrintedName { get; set; } = string.Empty;
        [Required]
        public double Price { get; set; }
        private Dictionary<int, (IComponentModel, int)>? _printedComponents = null;
        [NotMapped]
        public Dictionary<int, (IComponentModel, int)> PrintedComponents
        {
            get
            {
                if (_printedComponents == null)
                {
                    _printedComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
                }
                return _printedComponents;
            }
        }
        [ForeignKey("PrintedId")]
        public virtual List<PrintedComponent> Components { get; set; } = new();
        [ForeignKey("PrintedId")]
        public virtual List<Order> Orders { get; set; } = new();
        public static Printed Create(TypographyDatabase context, PrintedBindingModel model)
        {
            return new Printed()
            {
                Id = model.Id,
                PrintedName = model.PrintedName,
                Price = model.Price,
                Components = model.PrintedComponents.Select(x => new PrintedComponent
                {
                    Component = context.Components.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }
        public void Update(PrintedBindingModel model)
        {
            PrintedName = model.PrintedName;
            Price = model.Price;
        }
        public PrintedViewModel GetViewModel => new()
        {
            Id = Id,
            PrintedName = PrintedName,
            Price = Price,
            PrintedComponents = PrintedComponents
        };
        public void UpdateComponents(TypographyDatabase context, PrintedBindingModel model)
        {
            var printedComponents = context.PrintedComponents.Where(rec => rec.PrintedId == model.Id).ToList();
            if (printedComponents != null && printedComponents.Count > 0)
            { // удалили те, которых нет в модели
                context.PrintedComponents.RemoveRange(printedComponents.Where(rec => !model.PrintedComponents.ContainsKey(rec.ComponentId)));
                context.SaveChanges();
                // обновили количество у существующих записей
                foreach (var updateComponent in printedComponents)
                {
                    updateComponent.Count = model.PrintedComponents[updateComponent.ComponentId].Item2;
                    model.PrintedComponents.Remove(updateComponent.ComponentId);
                }
                context.SaveChanges();
            }
            var printed = context.Printeds.First(x => x.Id == Id);
            foreach (var pc in model.PrintedComponents)
            {
                context.PrintedComponents.Add(new PrintedComponent
                {
                    Printed = printed,
                    Component = context.Components.First(x => x.Id == pc.Key),
                    Count = pc.Value.Item2
                });
                context.SaveChanges();
            }
            _printedComponents = null;
        }
    }
}
