using BlackcmithWorkshopFileImplement;
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BlacksmithWorkshopFileImplement.Models
{
    public class Shop : IShopModel
    {
        public int Id { get; private set; }
        public string ShopName { get; private set; } = string.Empty;
        public string Address { get; private set; } = string.Empty;
        public DateTime OpeningDate { get; private set; }
        public int MaxCapacity { get; private set; }
        public Dictionary<int, int> Manufactures { get; private set; } = new();
        private Dictionary<int, (IManufactureModel, int)>? _shopManufactures = null;
        public Dictionary<int, (IManufactureModel, int)> ShopManufactures
        {
            get
            {
                if (_shopManufactures == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _shopManufactures = Manufactures.ToDictionary(x => x.Key, y =>
                    ((source.Manufactures.FirstOrDefault(z => z.Id == y.Key) as IManufactureModel)!,
                    y.Value));
                }
                return _shopManufactures;
            }
        }
        public static Shop? Create(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.ToDictionary(x => x.Key, x => x.Value.Item2)
            };
        }
        public static Shop? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                ShopName = element.Element("ShopName")!.Value,
                Address = element.Element("Address")!.Value,
                MaxCapacity = Convert.ToInt32(element.Element("MaxCapacity")!.Value),
                OpeningDate = Convert.ToDateTime(element.Element("DateOpen")!.Value),
                Manufactures = element.Element("ShopManufactures")!.Elements("ShopManufacture")
                    .ToDictionary(x =>
                    Convert.ToInt32(x.Element("Key")?.Value), x =>
                    Convert.ToInt32(x.Element("Value")?.Value))
            };
        }
        public void Update(ShopBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            ShopName = model.ShopName;
            Address = model.Address;
            OpeningDate = model.OpeningDate;
            MaxCapacity = model.MaxCapacity;
            if (model.ShopManufactures.Count > 0)
            {
                Manufactures = model.ShopManufactures.ToDictionary(x => x.Key, x => x.Value.Item2);
                _shopManufactures = null;
            }
        }
        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            ShopName = ShopName,
            Address = Address,
            OpeningDate = OpeningDate,
            MaxCapacity = MaxCapacity,
            ShopManufactures = ShopManufactures
        };
        public XElement GetXElement => new("Shop",
             new XAttribute("Id", Id),
             new XElement("ShopName", ShopName),
             new XElement("Address", Address),
             new XElement("DateOpen", OpeningDate),
             new XElement("MaxCapacity", MaxCapacity),
             new XElement("ShopManufactures", Manufactures
                 .Select(x => new XElement("ShopManufacture",
                     new XElement("Key", x.Key),
                     new XElement("Value", x.Value))
                 ).ToArray()));
    }
}
