using SoftwareInstallationContracts.BindingModels;
using SoftwareInstallationContracts.ViewModels;
using SoftwareInstallationDataModels.Enums;
using SoftwareInstallationDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

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

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

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

		public int? ImplementerId { get; private set; }

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


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

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

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

        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,
                Count = model.Count,
                Sum = model.Sum,
                Status = model.Status,
                DateCreate = model.DateCreate,
                DateImplement = model.DateImplement,
				ImplementerId = model.ImplementerId,
				Id = model.Id,
                ClientId = model.ClientId,
            };
        }

        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 = context.Packages.FirstOrDefault(x => x.Id == PackageId)?.PackageName ?? string.Empty,
                    PackageId = PackageId,
                    Count = Count,
                    Sum = Sum,
                    Status = Status,
                    DateCreate = DateCreate,
                    DateImplement = DateImplement,
					ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty,
					ImplementerId = ImplementerId,
					Id = Id,
                    ClientId = ClientId,
                    ClientFIO = Client?.ClientFIO ?? string.Empty,
                };
            }
        }
    }
}
