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

namespace CanteenDatabaseImplement.Models
{
    public class Lunch : ILunchModel
    {
        public int Id { get; private set; }
        [Required]
        public int VisitorId { get; private set; }
        [Required]
        public string LunchName { get; private set; } = string.Empty;
        public double Sum { get; private set; }
        [Required]
        public LunchStatus Status { get; private set; } = LunchStatus.Создан;
        [Required]
        public DateTime DateCreate { get; private set; } = DateTime.Now;
        public DateTime? DateImplement { get; private set; }

        private Dictionary<int, (IProductModel, int)>? _lunchProducts = null;

        [NotMapped]
        public Dictionary<int, (IProductModel, int)> LunchProducts
        {
            get
            {
                if (_lunchProducts == null)
                {
                    _lunchProducts = Products.ToDictionary(record => record.ProductId, record => (record.Product as IProductModel, record.CountProducts));
                }
                return _lunchProducts;
            }
        }

        [ForeignKey("LunchId")]
        public virtual List<LunchProduct> Products { get; set; } = new();
        [ForeignKey("LunchId")]
        public virtual List<LunchOrder> Orders { get; set; } = new();
        public virtual Visitor Visitor { get; set; }

        public static Lunch? Create(CanteenDatabase context, LunchBindingModel model)
        {
            return new Lunch()
            {
                Id = model.Id,
                VisitorId = model.VisitorId,
                LunchName = model.LunchName,
                Sum = model.Sum,
                Status = model.Status,
                DateCreate = model.DateCreate,
                DateImplement = model.DateImplement,
                Products = model.LunchProducts.Select(x => new LunchProduct
                {
                    Product = context.Products.First(y => y.Id == x.Key),
                    CountProducts = x.Value.Item2
                }).ToList()
            };
        }

        public void Update(LunchBindingModel model)
        {
            LunchName = model.LunchName;
            Sum = model.Sum;
            Status = model.Status;
            DateImplement = model.DateImplement;
        }

        public LunchViewModel GetViewModel => new()
        {
            Id = Id,
            VisitorId = VisitorId,
            LunchName = LunchName,
            Sum = Sum,
            Status = Status,
            DateCreate = DateCreate,
            DateImplement = DateImplement,
            LunchProducts = LunchProducts
        };

        public void UpdateProducts(CanteenDatabase context, LunchBindingModel model)
        {
            var lunchProducts = context.LunchProduct.Where(record => record.LunchId == model.Id).ToList();
            if (lunchProducts != null && lunchProducts.Count > 0)
            {
                context.LunchProduct.RemoveRange(lunchProducts.Where(record => !model.LunchProducts.ContainsKey(record.ProductId)));
                context.SaveChanges();

                foreach (var updateProduct in lunchProducts)
                {
                    updateProduct.CountProducts = model.LunchProducts[updateProduct.ProductId].Item2;
                    model.LunchProducts.Remove(updateProduct.ProductId);
                }
                context.SaveChanges();
            }

            var lunch = context.Lunches.First(x => x.Id == Id);
            foreach (var pp in model.LunchProducts)
            {
                context.LunchProduct.Add(new LunchProduct
                {
                    Lunch = lunch,
                    Product = context.Products.First(x => x.Id == pp.Key),
                    CountProducts = pp.Value.Item2
                });
                context.SaveChanges();
            }

            _lunchProducts = null;
        }
    }
}
