using ComputersShopContracts.BindingModels;
using ComputersShopContracts.ViewModels;
using ComputersShopDataModels;
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;

namespace ComputersShopDatabaseImplement.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 DateOpen { get; private set; }
        [Required]
        public int MaxCapacity { get; private set; }
        private Dictionary<int, (IComputerModel, int)>? _shopComputers =
       null;
        [NotMapped]
        public Dictionary<int, (IComputerModel, int)> ShopComputers
        {
            get
            {
                if (_shopComputers == null)
                {
                    _shopComputers = Computers
                    .ToDictionary(x => x.ComputerId, x =>
                   (x.Computer as IComputerModel, x.Count));
                }
                return _shopComputers;
            }
        }
        [ForeignKey("ShopId")]
        public virtual List<ShopComputer> Computers { get; set; } = new();
        public static Shop? Create(ComputersShopDatabase context, ShopBindingModel model)
        {
            if (model == null)
                return null;
            return new Shop()
            {
                Id = model.Id,
                ShopName = model.ShopName,
                Address = model.Address,
                DateOpen = model.DateOpen,
                MaxCapacity = model.MaxCapacity,
                Computers = model.ShopComputers.Select(x => new
               ShopComputer
                {
                    Computer = context.Computers.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;
            DateOpen = model.DateOpen;
            MaxCapacity = model.MaxCapacity;
        }

        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            ShopName = ShopName,
            Address = Address,
            DateOpen = DateOpen,
            ShopComputers = ShopComputers,
            MaxCapacity = MaxCapacity
        };

        public void UpdateComputers(ComputersShopDatabase context,
       ShopBindingModel model)
        {
            var shopComputers = context.ShopComputers.Where(rec =>
           rec.ShopId == model.Id).ToList();
            if (shopComputers != null && shopComputers.Count > 0)
            {
                context.ShopComputers.RemoveRange(shopComputers.Where(rec
=> !model.ShopComputers.ContainsKey(rec.ShopId)));

                context.SaveChanges();
                foreach (var updateComputer in shopComputers)
                {
                    updateComputer.Count =
                   model.ShopComputers[updateComputer.ComputerId].Item2;
                    model.ShopComputers.Remove(updateComputer.ComputerId);
                }
                context.SaveChanges();
            }
            var shop = context.Shops.First(x => x.Id == Id);
            foreach (var pc in model.ShopComputers)
            {
                context.ShopComputers.Add(new ShopComputer
                {
                    Shop = shop,
                    Computer = context.Computers.First(x => x.Id == pc.Key),
                    Count = pc.Value.Item2
                });
                context.SaveChanges();
            }
            _shopComputers = null;
        }
    }
}
