using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.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 BlacksmithWorkshopDatabaseImplement.Models
{
    public class Shop : IShopModel
    {
        public int Id { get; private set; }
        [Required]
        public string ShopName { get; private set; } = string.Empty;
        [Required]
        public string Address { get; private set; } = string.Empty;
        [Required]
        public DateTime OpeningDate { get; private set; }
        [Required]
        public int MaxCapacity { get; private set; }
        private Dictionary<int, (IManufactureModel, int)>? _ShopManufacture = null;
        [NotMapped]
        public Dictionary<int, (IManufactureModel, int)> ShopManufactures
        {
            get
            {
                if (_ShopManufacture == null)
                {
                    _ShopManufacture = Manufactures
                    .ToDictionary(x => x.ManufactureId, x =>
                   (x.Manufacture as IManufactureModel, x.Count));
                }
                return _ShopManufacture;
            }
        }
        [ForeignKey("ShopId")]
        public virtual List<ShopManufacture> Manufactures { get; set; } = new();
        public static Shop? Create(BlacksmithWorkshopDataBase context, ShopBindingModel model)
        {
            if (model == null)
                return null;
            return new Shop()
            {
                Id = model.Id,
                ShopName = model.ShopName,
                Address = model.Address,
                OpeningDate = model.OpeningDate,
                MaxCapacity = model.MaxCapacity,
                Manufactures = model.ShopManufactures.Select(x => new ShopManufacture
                {
                    Manufacture = context.Manufactures.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }
        public void Update(ShopBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            ShopName = model.ShopName;
            Address = model.Address;
            OpeningDate = model.OpeningDate;
            MaxCapacity = model.MaxCapacity;
        }
        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            ShopName = ShopName,
            Address = Address,
            OpeningDate = OpeningDate,
            ShopManufactures = ShopManufactures,
            MaxCapacity = MaxCapacity
        };
        public void UpdateManufactures(BlacksmithWorkshopDataBase context, ShopBindingModel model)
        {
            var ShopManufacture = context.ShopManufactures.Where(rec =>
                    rec.ShopId == model.Id).ToList();
            if (ShopManufacture != null && ShopManufacture.Count > 0)
            {
                context.ShopManufactures.RemoveRange(ShopManufacture.Where(rec
                        => !model.ShopManufactures.ContainsKey(rec.ShopId)));

                context.SaveChanges();
                foreach (var updateManufacture in ShopManufacture)
                {
                    updateManufacture.Count =
                    model.ShopManufactures[updateManufacture.ManufactureId].Item2;
                    model.ShopManufactures.Remove(updateManufacture.ManufactureId);
                }
                context.SaveChanges();
            }
            var shop = context.Shops.First(x => x.Id == Id);
            foreach (var pc in model.ShopManufactures)
            {
                context.ShopManufactures.Add(new ShopManufacture
                {
                    Shop = shop,
                    Manufacture = context.Manufactures.First(x => x.Id == pc.Key),
                    Count = pc.Value.Item2
                });
                context.SaveChanges();
            }
            _ShopManufacture = null;
        }
    }
}
