using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using IceCreamShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace IceCreamShopDatabaseImplement.Models
{
    public class Shop : IShopModel
    {
        public int Id { get; set; }

        [Required]
        public string ShopName { get; set; } = string.Empty;

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

        [Required]
        public DateTime DateOpening { get; set; }

        [Required]
        public int IceCreamsMaximum { get; set; }

        private Dictionary<int, (IIceCreamModel, int)>? _shopIceCreams = null;

        [NotMapped]
        public Dictionary<int, (IIceCreamModel, int)> ShopIceCreams
        {
            get
            {
                if (_shopIceCreams == null)
                {
                    _shopIceCreams = IceCreams
                            .ToDictionary(x => x.IceCreamId, x => (x.IceCream as IIceCreamModel, x.Count));
                }
                return _shopIceCreams;
            }
        }

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

        public static Shop Create(IceCreamShopDatabase context, ShopBindingModel model)
        {
            return new Shop()
            {
                Id = model.Id,
                ShopName = model.ShopName,
                Address = model.Address,
                DateOpening = model.DateOpening,
                IceCreamsMaximum = model.IceCreamsMaximum,
                IceCreams = model.ShopIceCreams.Select(x => new ShopIceCream
                {
                    IceCream = context.IceCreams.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }

        public void Update(ShopBindingModel model)
        {
            ShopName = model.ShopName;
            Address = model.Address;
            DateOpening = model.DateOpening;
            IceCreamsMaximum = model.IceCreamsMaximum;
        }

        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            ShopName = ShopName,
            Address = Address,
            DateOpening = DateOpening,
            IceCreamsMaximum = IceCreamsMaximum,
            ShopIceCreams = ShopIceCreams
        };

        public void UpdateIceCreams(IceCreamShopDatabase context, ShopBindingModel model)
        {
            var shopIceCreams = context.ShopIceCreams.Where(rec => rec.ShopId == model.Id).ToList();
            if (shopIceCreams != null && shopIceCreams.Count > 0)
            {
                context.ShopIceCreams.RemoveRange(shopIceCreams.Where(rec => !model.ShopIceCreams.ContainsKey(rec.IceCreamId)));
                context.SaveChanges();
                foreach (var updateIceCream in shopIceCreams)
                {
                    if (model.ShopIceCreams.ContainsKey(updateIceCream.IceCreamId))
                    {
                        updateIceCream.Count = model.ShopIceCreams[updateIceCream.IceCreamId].Item2;
                        model.ShopIceCreams.Remove(updateIceCream.IceCreamId);
                    }
                }
                context.SaveChanges();
            }
            var shop = context.Shops.First(x => x.Id == Id);
            foreach (var ic in model.ShopIceCreams)
            {
                context.ShopIceCreams.Add(new ShopIceCream
                {
                    Shop = shop,
                    IceCream = context.IceCreams.First(x => x.Id == ic.Key),
                    Count = ic.Value.Item2
                });
                context.SaveChanges();
            }
            _shopIceCreams = null;
        }
    }
}
