using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement;
using ConfectioneryDataModels;
using ConfectioneryDataModels.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace ConfectioneryDatabaseImplement.Models
{
    [DataContract]
    public class Shop : IShopModel
    {
        [Required]
        [DataMember]
        public string Name { get; private set; } = string.Empty;

        [Required]
        [DataMember]
        public string Address { get; private set; } = string.Empty;

        [Required]
        [DataMember]
        public int MaxCountPastries { get; private set; }

        [DataMember]
        public DateTime DateOpening { get; private set; }

        private Dictionary<int, (IPastryModel, int)>? _cachedPastries = null;
        [NotMapped]
        [DataMember]
        public  Dictionary<int, (IPastryModel, int)> Pastries
        {
            get
            {
                if (_cachedPastries == null)
                {
                    using var context = new ConfectioneryDatabase();
                    _cachedPastries = ShopPastries
                        .ToDictionary(x => x.PastryId, x => (context.Pastries
                        .FirstOrDefault(y => y.Id == x.PastryId)! as IPastryModel, x.Count));
                }
                return _cachedPastries;
            }
        }

        [DataMember]
        public int Id { get; private set; }

        [ForeignKey("ShopId")]
        public virtual List<ShopPastry> ShopPastries { get; set; } = new();

        public static Shop? Create(ConfectioneryDatabase context, ShopBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = model.Id,
                Name = model.Name,
                Address = model.Address,
                DateOpening = model.DateOpening,
                MaxCountPastries = model.MaxCountPastries,
                ShopPastries = model.Pastries.Select(x => new ShopPastry
                {
                    Pastry = context.Pastries.FirstOrDefault(y => y.Id == x.Key)!,
                    Count = x.Value.Item2,
                }).ToList()
            };
        }
        public void Update(ShopBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Name = model.Name;
            Address = model.Address;
            DateOpening = model.DateOpening;
        }
        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            Address = Address,
            Pastries = Pastries,
            DateOpening = DateOpening,
            MaxCountPastries = MaxCountPastries,
        };

        public void UpdatePastries(ConfectioneryDatabase context, ShopBindingModel model)
        {
            var shopPastries = context.ShopPastries
                .Where(rec => rec.ShopId == model.Id)
                .ToList();
            // удалили те, которых нет в модели
            if (shopPastries != null && shopPastries.Count > 0)
            {
                context.ShopPastries
                    .RemoveRange(shopPastries
                    .Where(rec => !model.Pastries
                                        .ContainsKey(rec.PastryId)));
                // обновили количество у существующих записей
                foreach (var updatePastry in shopPastries.Where(x => model.Pastries.ContainsKey(x.PastryId)))
                {
                    updatePastry.Count = model.Pastries[updatePastry.PastryId].Item2;
                    model.Pastries.Remove(updatePastry.PastryId);
                }
            }
            var shop = context.Shops.First(x => x.Id == model.Id);
            shop.ShopPastries.AddRange(model.Pastries.Select(x => new ShopPastry
            {
                Pastry = context.Pastries.First(y => y.Id == x.Key),
                Count = x.Value.Item2,
            }).Except(shopPastries ?? new()));
            context.SaveChanges();
            _cachedPastries = null;
        }
    }
}