using MotorPlantContracts.BindingModels;
using MotorPlantContracts.ViewModels;
using MotorPlantDataModels.Models;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace MotorPlantFileImplement.Models
{
    [DataContract]
    public class Engine : IEngineModel
    {
        [DataMember]
        public int Id { get; private set; }
        [DataMember]
        public string EngineName { get; private set; } = string.Empty;
        [DataMember]
        public double Price { get; private set; }
        public Dictionary<int, int> Components { get; private set; } = new();
        private Dictionary<int, (IComponentModel, int)>? _EngineComponents = null;
        public Dictionary<int, (IComponentModel, int)> EngineComponents
        {
            get
            {
                if (_EngineComponents == null)
                {
                    var source = DataFileSingleton.GetInstance();
                    _EngineComponents = Components.ToDictionary(x => x.Key, y =>
                    ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
                    y.Value));
                }
                return _EngineComponents;
            }
        }
        public static Engine? Create(EngineBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new Engine()
            {
                Id = model.Id,
                EngineName = model.EngineName,
                Price = model.Price,
                Components = model.EngineComponents.ToDictionary(x => x.Key, x
               => x.Value.Item2)
            };
        }
        public static Engine? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Engine()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                EngineName = element.Element("EngineName")!.Value,
                Price = Convert.ToDouble(element.Element("Price")!.Value),
                Components =
           element.Element("EngineComponents")!.Elements("EngineComponent")
            .ToDictionary(x =>
           Convert.ToInt32(x.Element("Key")?.Value), x =>
           Convert.ToInt32(x.Element("Value")?.Value))
            };
        }
        public void Update(EngineBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            EngineName = model.EngineName;
            Price = model.Price;
            Components = model.EngineComponents.ToDictionary(x => x.Key, x =>
           x.Value.Item2);
            _EngineComponents = null;
        }
        public EngineViewModel GetViewModel => new()
        {
            Id = Id,
            EngineName = EngineName,
            Price = Price,
            EngineComponents = EngineComponents
        };
        public XElement GetXElement => new("Engine",
            new XAttribute("Id", Id),
            new XElement("EngineName", EngineName),
            new XElement("Price", Price.ToString()),
            new XElement("EngineComponents", Components.Select(x => new XElement("EngineComponent", new XElement("Key", x.Key), new XElement("Value", x.Value)))

       .ToArray()));
    }
}
