using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels;
using SoftwareInstallationDataModels.Models;
using System.Xml.Linq;

namespace SoftwareInstallationFileImplement
{
    public class Shop : IShopModel
    {
        public string Name { get; private set; } = string.Empty;

        public string Address { get; private set; } = string.Empty;

        public int MaxCountPackages { get; private set; }

        public DateTime DateOpening { get; private set; }

        public Dictionary<int, int> CountPackages { get; private set; } = new();

        private Dictionary<int, (IPackageModel, int)>? _cachedPackages = null;
        public  Dictionary<int, (IPackageModel, int)> Packages
        {
            get
            {
                if (_cachedPackages == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _cachedPackages = CountPackages
                        .ToDictionary(x => x.Key, x => (source.Packages
                        .FirstOrDefault(y => y.Id == x.Key)! as IPackageModel, x.Value));
                }
                return _cachedPackages;
            }
        }

        public int Id { get; private set; }

        public static Shop? Create(ShopBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = model.Id,
                Name = model.Name,
                Address = model.Address,
                DateOpening = model.DateOpening,
                MaxCountPackages = model.MaxCountPackages,
                CountPackages = new()
            };
        }
        public static Shop? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new()
            {
                Id               = Convert.ToInt32(element.Attribute("Id")!.Value),
                Name             = element.Element("Name")!.Value,
                Address          = element.Element("Address")!.Value,
                DateOpening      = Convert.ToDateTime(element.Element("DateOpening")!.Value),
                MaxCountPackages = Convert.ToInt32(element.Element("MaxCountPackages")!.Value),
                CountPackages    = element.Element("CountPackages")!.Elements("CountPackage")
                    .ToDictionary(
                        x => Convert.ToInt32(x.Element("Key")?.Value),
                        x => Convert.ToInt32(x.Element("Value")?.Value)
                    )
            };   
        }
        public void Update(ShopBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Name = model.Name;
            Address = model.Address;
            DateOpening = model.DateOpening;
            CountPackages = model.Packages.ToDictionary(x => x.Key, x => x.Value.Item2);
            _cachedPackages = null;
        }
        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            Address = Address,
            Packages = Packages,
            DateOpening = DateOpening,
            MaxCountPackages = MaxCountPackages,
        };
        public XElement GetXElement => new("Shop",
             new XAttribute("Id", Id),
             new XElement("Name", Name),
             new XElement("Address", Address),
             new XElement("DateOpening", DateOpening),
             new XElement("MaxCountPackages", MaxCountPackages),
             new XElement("CountPackages", CountPackages
                 .Select(x => new XElement("CountPackage",
                     new XElement("Key", x.Key),
                     new XElement("Value", x.Value))
                 ))
         );
    }
}
