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


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

        public int JewelId { get; private set; }

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

        public string JewelName { get; private set; } = string.Empty;

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

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

        [Required]
        public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;

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

        public DateTime? DateImplement { get; private set; }

        public virtual Jewel Jewel { get; set; }
        public virtual Client Client { get; 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,
                JewelName = model.JewelName,
                Count = model.Count,
                Sum = model.Sum,
                Status = model.Status,
                DateCreate = model.DateCreate,
                DateImplement = model.DateImplement
            };
        }

        public void Update(OrderBindingModel? model)
        {
            if (model == null)
            {
                return;
            }

            JewelId = model.JewelId;
            JewelName = model.JewelName;
            Count = model.Count;
            Sum = model.Sum;
            Status = model.Status;
            DateCreate = model.DateCreate;
            DateImplement = model.DateImplement;
            ImplementerId = model.ImplementerId;

        }

        public OrderViewModel GetViewModel
        {
            get
            {
                using var context = new JewelryStoreDataBase();
                return new OrderViewModel
                {
                    Id = Id,
                    JewelId = JewelId,
                    ClientId = ClientId,
                    ImplementerId = ImplementerId,
                    ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
                    JewelName = context.Jewels.FirstOrDefault(x => x.Id == JewelId)?.JewelName ?? string.Empty,
                    Count = Count,
                    Sum = Sum,
                    Status = Status,
                    DateCreate = DateCreate,
                    DateImplement = DateImplement,
                    ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty
                };
            }
        }
    }
}