using AbstractIceCreamShopDataModels.Models;
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;

namespace IceCreamShopDatabaseImplement.Models
{
    public class Shop : IShopModel
    {

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

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

        public DateTime OpeningDate { get; private set; }

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

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

        [Required]
        public int IceCreamMaxCount { get; private set; }

        public int Id { get; private set; }

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

        public static Shop? Create(IceCreamShopDatabase context, ShopBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = model.Id,
                Name = model.Name,
                Adress = model.Adress,
                OpeningDate = model.OpeningDate,
                IceCreamMaxCount = model.IceCreamMaxCount,
                IceCreams = model.ShopIceCreams.Select(x => new ShopIcecream
                {
                    IceCream = context.IceCreams.FirstOrDefault(y => y.Id == x.Key)!,
                    Count = x.Value.Item2,
                }).ToList()
            };
        }
        public void Update(ShopBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Name = model.Name;
            Adress = model.Adress;
            OpeningDate = model.OpeningDate;
            IceCreamMaxCount = IceCreamMaxCount;
        }
        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            Adress = Adress,
            ShopIceCreams = ShopIceCreams,
            OpeningDate = OpeningDate,
            IceCreamMaxCount = IceCreamMaxCount,
        };

        public void UpdateIceCreams(IceCreamShopDatabase context, ShopBindingModel model)
        {
            //if (model.ShopIceCreams == null) return;
            var iceCreams = context.ShopIcecreams.Where(rec => rec.ShopId == model.Id).ToList();
            if (iceCreams != null && iceCreams.Count > 0)
            {
                // удалили те, которых нет в модели
                context.ShopIcecreams.RemoveRange(iceCreams.Where(rec => !model.ShopIceCreams.ContainsKey(rec.IceCreamId)));
                context.SaveChanges();
                iceCreams = context.ShopIcecreams.Where(rec => rec.ShopId == model.Id).ToList();
                // обновили количество у существующих записей
                foreach (var updateIceCream in iceCreams)
                {
                    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 elem in model.ShopIceCreams)
            {
                context.ShopIcecreams.Add(new ShopIcecream
                {
                    Shop = shop,
                    IceCream = context.IceCreams.First(x => x.Id == elem.Key),
                    Count = elem.Value.Item2
                });
                context.SaveChanges();
            }
            _cashedIcecreams = null;
        }
    }
}
