using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace SushiBarDatabaseImplement.Models
{
    public class Sushi : ISushiModel
    {
        public int Id { get; set; }

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

        [Required]
        public double Price { get; set; }

        private Dictionary<int, (IIngredientModel, int)>? _sushiIngredients = null;

        [NotMapped]
        public Dictionary<int, (IIngredientModel, int)> SushiIngredients
        {
            get
            {
                if (_sushiIngredients == null)
                {
                    _sushiIngredients = Ingredients
                            .ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count));
                }
                return _sushiIngredients;
            }
        }

        [ForeignKey("SushiId")]
        public virtual List<SushiIngredient> Ingredients { get; set; } = new();
        [ForeignKey("SushiId")]
        public virtual List<Order> Orders { get; set; } = new();

        public static Sushi Create(SushiBarDatabase context, SushiBindingModel model)
        {
            return new Sushi()
            {
                Id = model.Id,
                SushiName = model.SushiName,
                Price = model.Price,
                Ingredients = model.SushiIngredients.Select(x => new SushiIngredient
                {
                    Ingredient = context.Ingredients.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }

        public void Update(SushiBindingModel model)
        {
            SushiName = model.SushiName;
            Price = model.Price;
        }

        public SushiViewModel GetViewModel => new()
        {
            Id = Id,
            SushiName = SushiName,
            Price = Price,
            SushiIngredients = SushiIngredients
        };

        public void UpdateIngredients(SushiBarDatabase context, SushiBindingModel model)
        {
            var sushiIngredients = context.SushiIngredients.Where(rec => rec.SushiId == model.Id).ToList();
            if (sushiIngredients != null && sushiIngredients.Count > 0)
            {   // удалили те, которых нет в модели
                context.SushiIngredients.RemoveRange(sushiIngredients.Where(rec => !model.SushiIngredients.ContainsKey(rec.IngredientId)));
                context.SaveChanges();
                // обновили количество у существующих записей
                foreach (var updateIngredient in sushiIngredients)
                {
                    updateIngredient.Count = model.SushiIngredients[updateIngredient.IngredientId].Item2;
                    model.SushiIngredients.Remove(updateIngredient.IngredientId);
                }
                context.SaveChanges();
            }
            var sushi = context.ListSushi.First(x => x.Id == Id);
            foreach (var si in model.SushiIngredients)
            {
                context.SushiIngredients.Add(new SushiIngredient
                {
                    Sushi = sushi,
                    Ingredient = context.Ingredients.First(x => x.Id == si.Key),
                    Count = si.Value.Item2
                });
                context.SaveChanges();
            }
            _sushiIngredients = null;
        }
    }
}