using SecuritySystemContracts.BindingModels;
using SecuritySystemContracts.ViewModels;
using SecuritySystemDataModels.Models;
using System.Reflection;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace SecuritySystemFileImplement.Models
{
    [DataContract]
    public class MessageInfo : IMessageInfoModel
    {
        [DataMember]
        public string MessageId { get; set; } = string.Empty;
        [DataMember]
        public int? ClientId { get; set; }
        [DataMember]
        public string SenderName { get; set; } = string.Empty;
        [DataMember]
        public DateTime DateDelivery { get; set; } = DateTime.Now;
        [DataMember]
        public string Subject { get; set; } = string.Empty;
        [DataMember]
        public string Body { get; set; } = string.Empty;

        public static MessageInfo? Create(MessageInfoBindingModel model)
        {
            if (model == null)
            {
                return null;
            }
            return new MessageInfo()
            {
                MessageId = model.MessageId,
                ClientId = model.ClientId,
                SenderName = model.SenderName,
                DateDelivery = model.DateDelivery,
                Subject = model.Subject,
                Body = model.Body,
            };
        }
        public static MessageInfo? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new MessageInfo()
            {
                MessageId = element.Attribute("MessageId")!.Value,
                ClientId = string.IsNullOrEmpty(element.Element("ClientId")!.Value) ? 
                    null : Convert.ToInt32(element.Element("ClientId")!.Value),
                SenderName = element.Element("SenderName")!.Value,
                DateDelivery = Convert.ToDateTime(element.Element("DateDelivery")!.Value),
                Subject = element.Element("Subject")!.Value,
                Body = element.Element("Body")!.Value,
            };
        }
        public void Update(MessageInfoBindingModel model)
        {
            if (model == null)
            {
                return;
            }
            MessageId = model.MessageId;
            ClientId = model.ClientId;
            SenderName = model.SenderName;
            DateDelivery = model.DateDelivery;
            Subject = model.Subject;
            Body = model.Body;
        }
        public MessageInfoViewModel GetViewModel => new()
        {
            MessageId = MessageId,
            ClientId = ClientId,
            SenderName = SenderName,
            DateDelivery = DateDelivery,
            Subject = Subject,
            Body = Body,
        };
        public XElement GetXElement => new("MessageInfo",
            new XAttribute("MessageId", MessageId),
            new XElement("ClientId", ClientId.ToString()),
            new XElement("SenderName", SenderName),
            new XElement("DateDelivery", DateDelivery.ToString()),
            new XElement("Subject", Subject),
            new XElement("Body", Body)
        );

        public int Id => throw new NotImplementedException();
    }
}
