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

namespace ComputerShopDatabaseImplement.Models
{
    internal class Purchase : IPurchaseModel
    {
        public string ComponentName { get; private set; } = String.Empty;
        public int Id { get; private set; }
        [Required]
        public int ComponentId { get; private set; }
        [Required]
        public int ClientId { get; private set; }
        [Required]
        public int Count { get; private set; }
        [Required]
        public double Sum { get; private set; }
        [Required]
        public PurchaseStatus Status { get; private set; } = PurchaseStatus.Неизвестен;
        [Required]
        public DateTime DateCreate { get; private set; } = DateTime.Now;

        public DateTime? DateImplement { get; private set; }
        public virtual Component Component { get; set; }
        public virtual Client Client { get; set; }

        public static Purchase? Create(PurchaseBindingModel? model)
        {
            if (model == null)
            {
                return null;
            }
            return new Purchase
            {
                ComponentId = model.ComponentId,
                ComponentName = model.ComponentName,
                Count = model.Count,
                Sum = model.Sum,
                Status = model.Status,
                DateCreate = model.DateCreate,
                DateImplement = model.DateImplement,
                Id = model.Id,
                ClientId = model.ClientId
            };
        }

        public void Update(PurchaseBindingModel? model)
        {
            if (model == null)
            {
                return;
            }
            Status = model.Status;
            DateImplement = model.DateImplement;
        }

        public PurchaseViewModel GetViewModel
        {
            get
            {
                var context = new ComputerShopDatabase();
                return new()
                {
                    ComponentId = ComponentId,
                    ClientId = ClientId,
                    Count = Count,
                    Sum = Sum,
                    DateCreate = DateCreate,
                    DateImplement = DateImplement,
                    Id = Id,
                    Status = Status,
                    ComponentName = context.Components.FirstOrDefault(x => x.Id == ComponentId)?.ComponentName ?? string.Empty,
                    ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty
                };
            }
        }
    }
}
