using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using AbstractIceCreamShopDataModels.Models;
using System.Xml.Linq;
using System.Reflection.Metadata;


namespace IceCreamShopFileImplement.Models
{
    public class Shop : IShopModel
    {
        public int Id { get; private set; }

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

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

        public DateTime OpeningDate { get; private set; }
       
        public int IceCreamMaxCount { 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 static Shop? Create(ShopBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Shop()
            {
                Id = model.Id,
                Name = model.Name,
                Adress = model.Adress,
                IceCreamMaxCount = model.IceCreamMaxCount,
                OpeningDate = model.OpeningDate,
                IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2)
            };
        }

        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,
                Adress = element.Element("Adress")!.Value,
                IceCreamMaxCount = Convert.ToInt32(element.Element("IceCreamMaxCount")!.Value),
                OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.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;
            }
            Name = model.Name;
            Adress = model.Adress;
            OpeningDate = model.OpeningDate;
            IceCreamMaxCount = model.IceCreamMaxCount;
            if(model.ShopIceCreams.Count > 0)
            {
                IceCreams = model.ShopIceCreams.ToDictionary(x => x.Key, x => x.Value.Item2);
                _shopIceCreams = null;
            }
        }

        public ShopViewModel GetViewModel => new()
        {
            Id = Id,
            Name = Name,
            Adress = Adress,
            OpeningDate = OpeningDate,
            IceCreamMaxCount = IceCreamMaxCount,
            ShopIceCreams = ShopIceCreams
        };

        public XElement GetXElement => new("Shop",
             new XAttribute("Id", Id),
             new XElement("Name", Name),
             new XElement("Adress", Adress),
             new XElement("OpeningDate", OpeningDate),
             new XElement("IceCreamMaxCount", IceCreamMaxCount),
             new XElement("ShopIceCreams", IceCreams
                 .Select(x => new XElement("ShopIceCream",
                     new XElement("Key", x.Key),
                     new XElement("Value", x.Value))
                 ).ToArray()));
    }
}
