using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Enums;
using SoftwareInstallationDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

namespace SoftwareInstallationDatabaseImplement.Models
{
	[DataContract]
	public class Order : IOrderModel
    {
        public int Id { get; private set; }

        [Required]
		[DataMember]
		public int PackageId { get; private set; }

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

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

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

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

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

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

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

        public Package Package { get; private set; }

        public Client Client { get; private set; }

		public Implementer? Implementer { 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,
				ImplementerId = model.ImplementerId,
				DateImplement = model.DateImplement,
                Id = model.Id,
            };
        }

        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
        { 
            get
            {
                var context = new SoftwareInstallationDatabase();
                return new()
                {
                    PackageName = Package?.PackageName ?? string.Empty,
                    ClientId = ClientId,
                    ClientFIO = Client?.ClientFIO ?? string.Empty,
					ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty,
					PackageId = PackageId,
                    Count = Count,
                    Sum = Sum,
                    Status = Status,
                    DateCreate = DateCreate,
                    DateImplement = DateImplement,
					ImplementerId = ImplementerId,
					Id = Id,
                };
            }
        }
    }
}