using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using IceCreamShopDataModels.Models;
using System.Xml.Linq;

namespace IceCreamShopFileImplement.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 DateOpening { get; private set; }

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

        private Dictionary<int, (IIceCreamModel, int)>? _shopIceCreams = null;

        public Dictionary<int, (IIceCreamModel, int)> ShopIceCreams
        {
            get
            {
                if (_shopIceCreams == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _shopIceCreams = IceCreams.ToDictionary(x => x.Key,
                        y => ((source.IceCreams.FirstOrDefault(z => z.Id == y.Key) as IIceCreamModel)!, y.Value));
                }
                return _shopIceCreams;
            }
        }

        public int IceCreamsMaximum { get; private set; }

        public static Shop? Create(ShopBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = model.Id,
                ShopName = model.ShopName,
                Address = model.Address,
                DateOpening = model.DateOpening,
                IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2),
                IceCreamsMaximum = model.IceCreamsMaximum
            };
        }

        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,
                DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
                IceCreamsMaximum = Convert.ToInt32(element.Element("IceCreamsMaximum")!.Value),
                IceCreams = element.Element("ShopIceCreams")!.Elements("ShopIceCream")
                                .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;
            DateOpening = model.DateOpening;
            IceCreamsMaximum =  model.IceCreamsMaximum;
            IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2);
            _shopIceCreams = null;
        }

        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            ShopName = ShopName,
            Address = Address,
            DateOpening = DateOpening,
            ShopIceCreams = ShopIceCreams,
            IceCreamsMaximum = IceCreamsMaximum
        };

        public XElement GetXElement => new("Shop",
                        new XAttribute("Id", Id),
                        new XElement("ShopName", ShopName),
                        new XElement("Address", Address),
                        new XElement("DateOpening", DateOpening.ToString()),
                        new XElement("IceCreamsMaximum", IceCreamsMaximum.ToString()),
                        new XElement("ShopIceCreams",
                            IceCreams.Select(x => new XElement("ShopIceCream",
                                                                new XElement("Key", x.Key),
                                                                new XElement("Value", x.Value))).ToArray()));
    }
}
