using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.ViewModels; using PrecastConcretePlantDataModels.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace PrecastConcretePlantFileImplement.Models { public class Reinforced : IReinforcedModel { public int Id { get; private set; } public string ReinforcedName { get; private set; } = string.Empty; public double Price { get; private set; } public Dictionary Components { get; private set; } = new(); private Dictionary? _ReinforcedComponents = null; public Dictionary ReinforcedComponents { get { if (_ReinforcedComponents == null) { var source = DataFileSingleton.GetInstance(); _ReinforcedComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); } return _ReinforcedComponents; } } public static Reinforced? Create(ReinforcedBindingModel model) { if (model == null) { return null; } return new Reinforced() { Id = model.Id, ReinforcedName = model.ReinforcedName, Price = model.Price, Components = model.ReinforcedComponents.ToDictionary(x => x.Key, x => x.Value.Item2) }; } public static Reinforced? Create(XElement element) { if (element == null) { return null; } return new Reinforced() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), ReinforcedName = element.Element("ReinforcedName")!.Value, Price = Convert.ToDouble(element.Element("Price")!.Value), Components = element.Element("ReinforcedComponents")!.Elements("ReinforcedComponent") .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) }; } public void Update(ReinforcedBindingModel model) { if (model == null) { return; } ReinforcedName = model.ReinforcedName; Price = model.Price; Components = model.ReinforcedComponents.ToDictionary(x => x.Key, x => x.Value.Item2); _ReinforcedComponents = null; } public ReinforcedViewModel GetViewModel => new() { Id = Id, ReinforcedName = ReinforcedName, Price = Price, ReinforcedComponents = ReinforcedComponents }; public XElement GetXElement => new("Reinforced", new XAttribute("Id", Id), new XElement("ReinforcedName", ReinforcedName), new XElement("Price", Price.ToString()), new XElement("ReinforcedComponents", Components.Select(x => new XElement("ReinforcedComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))) .ToArray())); } }