using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.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;

namespace ShipyardDataBaseImplement.Models
{
    public class Shop : IShopModel
    {
        public int Id { get; set; }
        [Required]
        public string ShopName { get; set; } = string.Empty;
        [Required]
        public string Address { get; set; } = string.Empty;
        [Required]
        public DateTime DateOpen { get; set; }
        [Required]
        public int Capacity { get; set; }

        private Dictionary<int, (IShipModel, int)>? _shopShips = null;

        [NotMapped]
        public Dictionary<int, (IShipModel, int)> ShopShips
        {
            get
            {
                if (_shopShips == null)
                {
                    _shopShips = Ships
                            .ToDictionary(recST => recST.ShipId, recST => (recST.Ship as IShipModel, recST.Count));
                }
                return _shopShips;
            }
        }
        [ForeignKey("ShopId")]
        public virtual List<ShopShip> Ships { get; set; } = new();
        public static Shop Create(ShipyardDataBase context, ShopBindingModel model)
        {
            return new Shop()
            {
                Id = model.Id,
                ShopName = model.ShopName,
                Address = model.Address,
                DateOpen = model.DateOpen,
                Capacity = model.Capacity,
                Ships = model.ShopShips.Select(x => new ShopShip
                {
                    Ship = context.Ships.First(y => y.Id == x.Key),
                    Count = x.Value.Item2
                }).ToList()
            };
        }

        public void Update(ShopBindingModel model)
        {
            ShopName = model.ShopName;
            Address = model.Address;
            DateOpen = model.DateOpen;
            Capacity = model.Capacity;
        }

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

        public void UpdateShips(ShipyardDataBase context, ShopBindingModel model)
        {
            var shopShips = context.ShopShips.Where(rec => rec.ShopId == model.Id).ToList();
            if (shopShips != null && shopShips.Count > 0)
            {
                context.ShopShips.RemoveRange(shopShips.Where(rec => !model.ShopShips.ContainsKey(rec.ShipId)));
                context.SaveChanges();
                shopShips = context.ShopShips.Where(rec => rec.ShopId == model.Id).ToList();
                foreach (var updateShip in shopShips)
                {
                    updateShip.Count = model.ShopShips[updateShip.ShipId].Item2;
                    model.ShopShips.Remove(updateShip.ShipId);
                }
                context.SaveChanges();
            }
            var shop = context.Shops.First(x => x.Id == Id);
            foreach (var id in model.ShopShips)
            {
                context.ShopShips.Add(new ShopShip
                {
                    Shop = shop,
                    Ship = context.Ships.First(x => x.Id == id.Key),
                    Count = id.Value.Item2
                });
                context.SaveChanges();
            }
            _shopShips = null;
        }
    }
}
