using BarContracts.BindingModels;
using BarContracts.ViewModels;
using BarDataModels.Models;
using System.Xml.Linq;

namespace BarFileImplement.Models
{
    public class Client : IClientModel
    {
        public int Id { get; private set; }
        
        public string ClientFIO { get; private set; } = string.Empty;
        
        public string Email { get; set; } = string.Empty;
        
        public string Password { get; set; } = string.Empty;
        
        public static Client? Create(ClientBindingModel Model)
        {
            if (Model == null)
                return null;

            return new Client()
            {
                Id = Model.Id,
                ClientFIO = Model.ClientFIO,
                Email = Model.Email,
                Password = Model.Password,
            };
        }
        
        public static Client? Create(XElement Element)
        {
            if (Element == null)
                return null;

            return new Client()
            {
                Id = Convert.ToInt32(Element.Attribute("Id")!.Value),
                ClientFIO = Element.Element("ClientFIO")!.Value,
                Email = Element.Element("Email")!.Value,
                Password = Element.Element("Password")!.Value,
            };
        }
        
        public void Update(ClientBindingModel Model)
        {
            if (Model == null)
                return;

            ClientFIO = Model.ClientFIO;
            Email = Model.Email;
            Password = Model.Password;
        }
        
        public ClientViewModel GetViewModel => new()
        {
            Id = Id,
            ClientFIO = ClientFIO,
            Email = Email,
            Password = Password
        };

        public XElement GetXElement => new(
            "Client",
            new XAttribute("Id", Id),
            new XElement("ClientFIO", ClientFIO),
            new XElement("Email", Email.ToString()),
            new XElement("Password", Password.ToString())
        );
    }
}
