using BarContracts.BindingModels;
using BarContracts.ViewModels;
using BarDataModels.Models;
using System.Xml.Linq;

namespace BarFileImplement.Models
{
    public class Cocktail : ICocktailModel
    {
        public int Id { get; private set; }

        public string CocktailName { 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)>? _CocktailComponents = null;

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

        public static Cocktail? Create(CocktailBindingModel Model)
        {
            if (Model is null)
                return null;

            return new Cocktail()
            {
                Id = Model.Id,
                CocktailName = Model.CocktailName,
                Price = Model.Price,
                Components = Model.CocktailComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
            };
        }
        public static Cocktail? Create(XElement Element)
        {
            if (Element is null)
                return null;

            return new Cocktail()
            {
                Id = Convert.ToInt32(Element.Attribute("Id")!.Value),
                CocktailName = Element.Element("CocktailName")!.Value,
                Price = Convert.ToDouble(Element.Element("Price")!.Value),
                Components = Element.Element("CocktailComponents")!.Elements("CocktailComponent").ToDictionary(x =>
                    Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
            };
        }

        public void Update(CocktailBindingModel Model)
        {
            if (Model is null)
                return;

            CocktailName = Model.CocktailName;
            Price = Model.Price;
            Components = Model.CocktailComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
            _CocktailComponents = null;
        }

        public CocktailViewModel GetViewModel => new()
        {
            Id = Id,
            CocktailName = CocktailName,
            Price = Price,
            CocktailComponents = CocktailComponents
        };

        public XElement GetXElement => new(
            "Cocktail",
            new XAttribute("Id", Id),
            new XElement("CocktailName", CocktailName),
            new XElement("Price", Price.ToString()),
            new XElement("CocktailComponents", Components.Select(x =>
                new XElement("CocktailComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())
        );
    }
}
