using JewelryStoreContracts.BindingModels;
using JewelryStoreContracts.ViewModels;
using JewelryStoreDataModels.Enums;
using JewelryStoreDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JewelryStoreDatabaseImplement.Models
{
    public class Order : IOrderModel
    {
        public int Id { get; private set; }
        [Required]
        public int JewelId { get; set; }
        [Required]
        public int Count { get; set; }
        [Required]
        public double Sum { get; set; }
        [Required]
        public OrderStatus Status { get; set; }
        [Required]
        public int ClientId { get; set; }
        [Required]
        public DateTime DateCreate { get; set; }
        public DateTime? DateImplement { get; set; }
        public Jewel Jewel { get; set; }
        public Client Client { get; set; }
        public int? ImplementerId { get; private set; }
        public Implementer? Implementer { get; set; }


        public static Order? Create(OrderBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Order()
            {
                Id = model.Id,
                JewelId = model.JewelId,
                ClientId = model.ClientId,
                ImplementerId = model.ImplementerId,
                Count = model.Count,
                Sum = model.Sum,
                Status = model.Status,
                DateCreate = model.DateCreate,
                DateImplement = model.DateImplement
            };
        }
        public void Update(OrderBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Status = model.Status;
            ImplementerId = model.ImplementerId;
            DateImplement = model.DateImplement;
        }
        public OrderViewModel GetViewModel
        {
            get
            {
                using var context = new JewelryStoreDatabase();
                return new OrderViewModel
                {
                    Id = Id,
                    JewelId = JewelId,
                    ClientId = ClientId,
                    ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
                    ImplementerId = ImplementerId,
                    ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty,
                    JewelName = context.Jewels.FirstOrDefault(x => x.Id == JewelId)?.JewelName ?? string.Empty,
                    Count = Count,
                    Sum = Sum,
                    Status = Status,
                    DateCreate = DateCreate,
                    DateImplement = DateImplement
                };
            }

        }
    }
}
