using GiftShopContracts.BindingModels;
using GiftShopContracts.ViewModels;
using GiftShopDataModels.Models;
using System.Xml.Linq;

namespace GiftShopFileImplement.Models
{
    internal class Gift : IGiftModel
    {
        public int Id { get; private set; }

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

        public double Price { get; private set; }

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

        private Dictionary<int, (IComponentModel, int)>? _giftComponents = null;

        public Dictionary<int, (IComponentModel, int)> GiftComponents 
        {
            get 
            {
                if (_giftComponents == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _giftComponents = Components.ToDictionary(x => x.Key,
                        y => ((source.Components.FirstOrDefault(z => z.Id == y.Key)
                        as IComponentModel)!, y.Value));
                }
                return _giftComponents;
            }
        }

        public static Gift? Create(GiftBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Gift()
            {
                Id = model.Id,
                GiftName = model.GiftName,
                Price = model.Price,
                Components = model.GiftComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
            };
        }

        public static Gift? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Gift()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                GiftName = element.Element("GiftName")!.Value,
                Price = Convert.ToDouble(element.Element("Price")!.Value),
                Components = element.Element("GiftComponents")!.Elements("GiftComponent").ToDictionary
                    (x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
            };
        }

        public void Update(GiftBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            GiftName = model.GiftName;
            Price = model.Price;
            Components = model.GiftComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
            _giftComponents = null;
        }

        public GiftViewModel GetViewModel => new()
        {
            Id = Id,
            GiftName = GiftName,
            Price = Price,
            GiftComponents = GiftComponents
        };

        public XElement GetXElement => new("Gift",
                        new XAttribute("Id", Id),
                        new XElement("GiftName", GiftName),
                        new XElement("Price", Price.ToString()),
                        new XElement("GiftComponents", Components.Select(x => new XElement("GiftComponent",
                                                                              new XElement("Key", x.Key),
                                                                              new XElement("Value", x.Value)))
            .ToArray()));
    }
}
