using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Models;
using SoftwareInstallationDataModels.Enums;
using System.Xml.Linq;
using System.Runtime.Serialization;

namespace SoftwareInstallationFileImplement.Models
{
	[DataContract]
	public class Order : IOrderModel
    {
		[DataMember]
		public int PackageId { get; private set; }

		[DataMember]
		public int ClientId { get; set; }

		[DataMember]
		public int? ImplementerId { get; set; }

		[DataMember]
		public int Count { get; private set; }

		[DataMember]
		public double Sum { get; private set; }

		[DataMember]
		public OrderStatus Status { get; private set; }

		[DataMember]
		public DateTime DateCreate { get; private set; }

		[DataMember]
		public DateTime? DateImplement { get; private set; }

		[DataMember]
		public int Id { get; private set; }
        
        public static Order? Create(OrderBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Order()
            {
                PackageId = model.PackageId,
                ClientId = model.ClientId,
                Count = model.Count,
                Sum = model.Sum,
                Status = model.Status,
                DateCreate = model.DateCreate,
                DateImplement = model.DateImplement,
				ImplementerId = model.ImplementerId,
				Id = model.Id,
            };
        }

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

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

		public OrderViewModel GetViewModel => new()
        {
            PackageName = DataFileSingleton.GetInstance().Packages.FirstOrDefault(x => x.Id == PackageId)?.PackageName ?? string.Empty,
            ClientFIO = DataFileSingleton.GetInstance().Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
            PackageId = PackageId,
            ClientId = ClientId,
            Count = Count,
            Sum = Sum,
            Status = Status,
            DateCreate = DateCreate,
            DateImplement = DateImplement,
			ImplementerId = ImplementerId,
			Id = Id,
        };
        public XElement GetXElement => new("Order",
            new XAttribute("Id", Id),
            new XElement("PackageId", PackageId),
            new XElement("ClientId", ClientId),
            new XElement("Count", Count),
            new XElement("Sum", Sum.ToString()),
            new XElement("Status", (int)Status),
            new XElement("DateCreate", DateCreate),
            new XElement("DateImplement", DateImplement),
			new XElement("ImplementerId", ImplementerId)
		);
    }
}