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

namespace SoftwareInstallationFileImplement.Models
{
    public class Order : IOrderModel
    {
        public int Id { get; private set; }

        public int PackageId { get; private set; }

        public int ClientId { get; set; }

		public int? ImplementerId { get; set; }

		public int Count { get; private set; }

        public double Sum { get; private set; }

        public OrderStatus Status { get; private set; }

        public DateTime DateCreate { get; private set; }

        public DateTime? DateImplement { 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,
                Id = model.Id,
				ImplementerId = model.ImplementerId,
			};
        }
        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),
                DateImplement = string.IsNullOrEmpty(dateImplement) ? null : Convert.ToDateTime(dateImplement),
				ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value),
			};
        }

        public void Update(OrderBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            PackageId = model.PackageId;
            Count = model.Count;
            Sum = model.Sum;
            Status = model.Status;
            DateCreate = model.DateCreate;
            DateImplement = model.DateImplement;
			ImplementerId = model.ImplementerId;
			Id = model.Id;
        }
        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,
			ImplementerFIO = DataFileSingleton.GetInstance().Implementers.FirstOrDefault(x => x.Id == ImplementerId)?.ImplementerFIO ?? string.Empty,
			PackageId = PackageId,
            Count = Count,
            Sum = Sum,
            Status = Status,
            DateCreate = DateCreate,
            DateImplement = DateImplement,
            Id = Id,
			ImplementerId = ImplementerId,
		};
        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)
		);
    }
}
