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

namespace SecuritySystemFileImplement.Models
{
    [DataContract]
    public class Order : IOrderModel
    {
        [DataMember]
        public int Id { get; private set; }
        [DataMember]
        public int SecureId { get; private set; }
        [DataMember]
        public int ClientId { get; private set; }
        [DataMember]
        public int? ImplementerId { get; private set; }
        [DataMember]
        public int Count { get; private set; }
        [DataMember]
        public double Sum { get; private set; }
        [DataMember]
        public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
        [DataMember]
        public DateTime DateCreate { get; private set; } = DateTime.Now;
        [DataMember]
        public DateTime? DateImplement { get; private set; }

        public static Order? Create(OrderBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Order
            {
                Id = model.Id,
                SecureId = model.SecureId,
                Count = model.Count,
                Sum = model.Sum,
                Status = model.Status,
                DateCreate = model.DateCreate,
                DateImplement = model.DateImplement,
                ImplementerId = model.ImplementerId
            };
        }

        public static Order? Create(XElement element)
        {
            if (element == null)
            {
                return null;
            }
            return new Order()
            {
                Id = Convert.ToInt32(element.Attribute("Id")!.Value),
                SecureId = Convert.ToInt32(element.Element("SecureId")!.Value),
                Sum = Convert.ToDouble(element.Element("Sum")!.Value),
                Count = Convert.ToInt32(element.Element("Count")!.Value),
                Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value),
                DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
                DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ?
                    null : Convert.ToDateTime(element.Element("DateImplement")!.Value),
                ImplementerId = string.IsNullOrEmpty(element.Element("ImplementerId")!.Value) ?
                    null : Convert.ToInt32(element.Element("ImplementerId")!.Value),
            };
        }

        public void Update(OrderBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Status = model.Status;
            DateImplement = model.DateImplement;
            ImplementerId = model.ImplementerId;
        }

        public OrderViewModel GetViewModel => new()
        {
            Id = Id,
            SecureId = SecureId,
            Count = Count,
            Sum = Sum,
            DateCreate = DateCreate,
            DateImplement = DateImplement,
            Status = Status,
            ImplementerId = ImplementerId,
        };

        public XElement GetXElement => new(
            "Order",
             new XAttribute("Id", Id),
             new XElement("SecureId", SecureId.ToString()),
             new XElement("Count", Count.ToString()),
             new XElement("Sum", Sum.ToString()),
             new XElement("Status", Status.ToString()),
             new XElement("DateCreate", DateCreate.ToString()),
             new XElement("DateImplement", DateImplement.ToString()),
             new XElement("ImplementerId", ImplementerId.ToString())
        );
    }
}
