using CanteenContracts.BindingModels;
using CanteenContracts.View;
using CanteenDataModels.Enums;
using CanteenDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CanteenDatabaseImplement.Models
{
    public class Order : IOrderModel
    {
        public int Id { get; private set; }
        [Required]
        public int VisitorId { get; private set; }
        public int TablewareId { get; set; }
        public int CountTablewares { get; set; }
        [Required]
        public string Description { get; private set; } = string.Empty;
        public double? Sum { get; private set; }

        private Dictionary<int, (IDishModel, int)>? _orderDishes = null;

        [NotMapped]
        public Dictionary<int, (IDishModel, int)> OrderDishes
        {
            get
            {
                if (_orderDishes == null)
                {
                    _orderDishes = Dishes
                            .ToDictionary(recOD => recOD.DishId, recOD => (recOD.Dish as IDishModel, recOD.CountDishes));
                }
                return _orderDishes;
            }
        }

        [ForeignKey("OrderId")]
        public virtual List<OrderDish> Dishes { get; set; } = new();
        [ForeignKey("OrderId")]
        public virtual List<LunchOrder> Lunches { get; set; } = new();
        [ForeignKey("OrderId")]
        public virtual List<OrderCook> Cooks { get; set; } = new();
        public virtual Tableware Tableware { get; set; }
        public virtual Visitor Visitor { get; set; }

        public static Order Create(OrderBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new Order()
            {
                Id = model.Id,
                VisitorId = model.VisitorId,
                Description = model.Description,
                Sum = model.Sum
            };
        }

        public void Update(OrderBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            VisitorId = model.VisitorId;
            Description = model.Description;
            Sum = model.Sum;
        }

        public OrderViewModel GetViewModel => new()
        {
            Id = Id,
            VisitorId = VisitorId,
            Description = Description,
            Sum = Sum,
            OrderDishes = OrderDishes
        };
        public void UpdateOrderDish(CanteenDatabase context, OrderBindingModel model)
        {
            var orderDish = context.OrderDish.Where(rec => rec.DishId == model.Id).ToList();
            if (orderDish != null && (orderDish.Count > 0))
            {
                context.OrderDish.RemoveRange(orderDish.Where(rec => !model.OrderDishes.ContainsKey(rec.DishId)));
                context.SaveChanges();
                foreach (var updateDish in orderDish)
                {
                    updateDish.CountDishes = model.OrderDishes[updateDish.DishId].Item2;
                    model.OrderDishes.Remove(updateDish.DishId);
                }
                context.SaveChanges();
            }
            var order = context.Orders.First(x => x.Id == Id);
            foreach (var dp in model.OrderDishes) 
            { 
            
                context.OrderDish.Add(new OrderDish
                {
                    Order = order,
                    Dish = context.Dishes.First(x => x.Id == dp.Key),
                    CountDishes = dp.Value.Item2
                });
                context.SaveChanges();
            }
            _orderDishes = null;
        }
    }
}
