using FishFactoryContracts.BindingModels;
using FishFactoryContracts.ViewModels;
using FishFactoryDataModels.Models;
using System.Xml.Linq;

namespace FishFactoryFileImplement.Models
{
    public class Canned : ICannedModel
    {
        public int Id { get; private set; }
        public string CannedName { get; private set; } = string.Empty;
        public double Price { get; private set; }
        public Dictionary<int, int> Ingredients { get; private set; } = new();
        private Dictionary<int, (IIngredientModel, int)>? _productIngredients = null;
        public Dictionary<int, (IIngredientModel, int)> CannedIngredients
        {
            get
            {
                if(_productIngredients == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _productIngredients = Ingredients.ToDictionary(x => x.Key, y => ((source.Ingredients.FirstOrDefault(z => z.Id == y.Key) as IIngredientModel)!, y.Value));
                }
                return _productIngredients;
            }
        }

        public static Canned? Create(CannedBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new Canned()
            {
                Id = model.Id,
                CannedName = model.CannedName,
                Price = model.Price,
                Ingredients = model.CannedIngredients.ToDictionary(x => x.Key, x => x.Value.Item2)
            };
        }

        public static Canned? Create(XElement element)
        {
            if(element == null)
            {
                return null;
            }
            return new Canned()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                CannedName = element.Element("CannedName")!.Value,
                Price = Convert.ToDouble(element.Element("Price")!.Value),
                Ingredients = element.Element("CannedIngredients")!.Elements("CannedIngredient").ToDictionary(x =>
                    Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
            };
        }

        public void Update(CannedBindingModel model)
        {
            if(model == null)
            {
                return;
            }
            CannedName = model.CannedName;
            Price = model.Price;
            Ingredients = model.CannedIngredients.ToDictionary(x => x.Key, x => x.Value.Item2);
            _productIngredients = null;
        }

        public CannedViewModel GetViewModel => new()
        {
            Id = Id,
            CannedName = CannedName,
            Price = Price,
            CannedIngredients = CannedIngredients,
        };

        public XElement GetXElement => new("Canned",
            new XAttribute("Id", Id),
            new XElement("CannedName", CannedName),
            new XElement("Price", Price.ToString()),
            new XElement("CannedIngredients", Ingredients.Select(x => new XElement("CannedIngredient",
                new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
    }
}
