using DressAtelierContracts.BindingModels;
using DressAtelierContracts.ViewModels;
using DressAtelierDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DressAtelierFileImplement.Models
{
    public class Material : IMaterialModel
    {
        public string ComponentName { get; private set; } = string.Empty;

        public double Cost { get; private set; }

        public int ID { get; private set; }

        public static Material? Create(MaterialBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Material()
            {
                ID = model.ID,
                ComponentName = model.ComponentName,
                Cost = model.Cost
            };
        }

        public static Material? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Material()
            {
                ID = Convert.ToInt32(element.Attribute("ID")!.Value),
                ComponentName = element.Element("ComponentName")!.Value,
                Cost = Convert.ToDouble(element.Element("Cost")!.Value)
            };
        }

        public void Update(MaterialBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            ComponentName = model.ComponentName;
            Cost = model.Cost;
        }
        public MaterialViewModel GetViewModel => new()
        {
            ID = ID,
            ComponentName = ComponentName,
            Cost = Cost
        };

        public XElement GetXElement => new("Component",new XAttribute("ID",ID),
                                                       new XElement("ComponentName", ComponentName),
                                                       new XElement("Cost", Cost.ToString()));

    }
}
