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

namespace SushiBarDatabaseImplement.Models
{
    public class Shop : IShopModel
    {
        public int Id { get; set; }
        public string ShopName { get; set; } = string.Empty;

        public string Adress { get; set; } = string.Empty;

        public DateTime OpeningDate { get; set; }

        public int SushiMaxCount { get; set; }

        private Dictionary<int, (ISushiModel, int)>? _shopSushis = null;

        public Dictionary<int, (ISushiModel, int)> ShopSushis
        {
            get
            {
                if (_shopSushis == null)
                {
                    if (_shopSushis == null)
                    {
                        _shopSushis = Sushis
                                .ToDictionary(recSP => recSP.SushiId, recSP => (recSP.Sushi as ISushiModel, recSP.Count));
                    }
                    return _shopSushis;
                }
                return _shopSushis;
            }
        }

        [ForeignKey("ShopId")]
        public List<ShopSushis> Sushis { get; set; } = new();

        public static Shop Create(SushiBarDatabase context, ShopBindingModel model)
        {
            return new Shop()
            {
                Id = model.Id,
                ShopName = model.ShopName,
                Adress = model.Adress,
                OpeningDate = model.OpeningDate,
                Sushis = model.ShopSushis.Select(x => new ShopSushis
                {
                    Sushi = context.Sushis.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList(),
                SushiMaxCount = model.SushiMaxCount
            };
        }

        public void Update(ShopBindingModel model)
        {
            ShopName = model.ShopName;
            Adress = model.Adress;
            OpeningDate = model.OpeningDate;
            SushiMaxCount = model.SushiMaxCount;
        }

        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            ShopName = ShopName,
            Adress = Adress,
            OpeningDate = OpeningDate,
            ShopSushis = ShopSushis,
            SushiMaxCount = SushiMaxCount
        };

        public void UpdateSushis(SushiBarDatabase context, ShopBindingModel model)
        {
            var ShopSushis = context.ShopSushis.Where(rec => rec.ShopId == model.Id).ToList();
            if (ShopSushis != null && ShopSushis.Count > 0)
            {
                context.ShopSushis.RemoveRange(ShopSushis.Where(rec => !model.ShopSushis.ContainsKey(rec.SushiId)));
                context.SaveChanges();
                ShopSushis = context.ShopSushis.Where(rec => rec.ShopId == model.Id).ToList();
                foreach (var updateSushi in ShopSushis)
                {
                    updateSushi.Count = model.ShopSushis[updateSushi.SushiId].Item2;
                    model.ShopSushis.Remove(updateSushi.SushiId);
                }
                context.SaveChanges();
            }
            var shop = context.Shops.First(x => x.Id == Id);
            foreach (var ar in model.ShopSushis)
            {
                context.ShopSushis.Add(new ShopSushis
                {
                    Shop = shop,
                    Sushi = context.Sushis.First(x => x.Id == ar.Key),
                    Count = ar.Value.Item2
                });
                context.SaveChanges();
            }
            _shopSushis = null;
        }

        public void SushisDictionatyUpdate(SushiBarDatabase context)
        {
            UpdateSushis(context, new ShopBindingModel
            {
                Id = Id,
                ShopSushis = ShopSushis,
            });
        }
    }
}
