using ShipyardContracts.BindingModels;
using ShipyardContracts.ViewModels;
using ShipyardDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ShipyardFileImplement.Models
{
    [DataContract]
    public class Detail:IDetailModel
    {
        [DataMember]
        public int Id { get; private set; }

        [DataMember]
        public string DetailName { get; private set; } = string.Empty;

        [DataMember]
        public double Cost { get; set; }
        public static Detail? Create(DetailBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new Detail()
            {
                Id = model.Id,
                DetailName = model.DetailName,
                Cost = model.Cost
            };
        }
        public static Detail? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Detail()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                DetailName = element.Element("DetailName")!.Value,
                Cost = Convert.ToDouble(element.Element("Cost")!.Value)
            };
        }
        public void Update(DetailBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            DetailName = model.DetailName;
            Cost = model.Cost;
        }
        public DetailViewModel GetViewModel => new()
        {
            Id = Id,
            DetailName = DetailName,
            Cost = Cost
        };
        public XElement GetXElement => new("Detail",
        new XAttribute("Id", Id),
        new XElement("DetailName", DetailName),
        new XElement("Cost", Cost.ToString()));
    }

}

