using BarContracts.BindingModels;
using BarContracts.ViewModels;
using BarDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace BarDatabaseImplement.Models
{
    public class Cocktail : ICocktailModel
    {
        public int Id { get; set; }

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

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

        private Dictionary<int, (IComponentModel, int)>? _cocktailComponents = null;

        [NotMapped]
        public Dictionary<int, (IComponentModel, int)> CocktailComponents
        {
            get
            {
                if (_cocktailComponents == null)
                {
                    _cocktailComponents = Components.ToDictionary(RepComp => RepComp.ComponentId, RepComp =>
                        (RepComp.Component as IComponentModel, RepComp.Count));
                }

                return _cocktailComponents;
            }
        }

        [ForeignKey("CocktailId")]
        public virtual List<CocktailComponent> Components { get; set; } = new();

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

        public static Cocktail Create(BarDatabase Context, CocktailBindingModel Model)
        {
            return new Cocktail()
            {
                Id = Model.Id,
                CocktailName = Model.CocktailName,
                Price = Model.Price,
                Components = Model.CocktailComponents.Select(x => new CocktailComponent
                {
                    Component = Context.Components.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }

        public void Update(CocktailBindingModel Model)
        {
            CocktailName = Model.CocktailName;
            Price = Model.Price;
        }

        public CocktailViewModel GetViewModel => new()
        {
            Id = Id,
            CocktailName = CocktailName,
            Price = Price,
            CocktailComponents = CocktailComponents
        };

        public void UpdateComponents(BarDatabase Context, CocktailBindingModel Model)
        {
            var CocktailComponents = Context.CocktailComponents.Where(rec => rec.CocktailId == Model.Id).ToList();

            if (CocktailComponents != null && CocktailComponents.Count > 0)
            {
                Context.CocktailComponents.RemoveRange(CocktailComponents.Where(rec => !Model.CocktailComponents.ContainsKey(rec.ComponentId)));
                Context.SaveChanges();

                try
                {
                    foreach (var ComponentToUpdate in CocktailComponents)
                    {
                        ComponentToUpdate.Count = Model.CocktailComponents[ComponentToUpdate.ComponentId].Item2;
                        Model.CocktailComponents.Remove(ComponentToUpdate.ComponentId);
                    }
                }catch (Exception ex)
                {

                }

                Context.SaveChanges();
            }

            var Cocktail = Context.Cocktails.First(x => x.Id == Id);

            foreach (var CocktailComponent in Model.CocktailComponents)
            {
                Context.CocktailComponents.Add(new CocktailComponent
                {
                    Cocktail = Cocktail,
                    Component = Context.Components.First(x => x.Id == CocktailComponent.Key),
                    Count = CocktailComponent.Value.Item2
                });

                Context.SaveChanges();
            }

            _cocktailComponents = null;
        }
    }
}
