using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComputerShopDatabaseImplement.Implements
{
    public class EquipmentReceivingStorage : IEquipmentReceivingStorage
    {
        public EquipmentReceivingViewModel? Delete(EquipmentReceivingBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            var element = context.EquipmentReceivings.FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.EquipmentReceivings.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }

        public EquipmentReceivingViewModel? GetElement(EquipmentReceivingSearchModel model)
        {
            if (!model.Id.HasValue)
            {
                return null;
            }
            using var context = new ComputerShopDatabase();
            return context.EquipmentReceivings.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
		}

        public List<EquipmentReceivingViewModel> GetFilteredList(EquipmentReceivingSearchModel model)
        {
            if (!model.Id.HasValue && !model.ClientId.HasValue && !model.DateImplement.HasValue && !model.Status.HasValue)
            {
                return new();
            }
            using var context = new ComputerShopDatabase();

            if (model.DateImplement.HasValue)
                return context.EquipmentReceivings
                    .Where(x => x.DateImplement == model.DateImplement)
                    .Select(x => x.GetViewModel)
                    .ToList();
            if (model.ClientId.HasValue)
            {
                return context.EquipmentReceivings
                    .Include(x => x.Client)
                    .Where(x => x.ClientId == model.ClientId)
                    .Select(x => x.GetViewModel)
                    .ToList();
            }
            return context.EquipmentReceivings
            .Where(x => x.Id == model.Id)
            .Select(x => x.GetViewModel)
            .ToList();
            
        }

        public List<EquipmentReceivingViewModel> GetFullList()
        {
            using var context = new ComputerShopDatabase();
            return context.EquipmentReceivings.Select(x => x.GetViewModel).ToList();
        }

        public EquipmentReceivingViewModel? Insert(EquipmentReceivingBindingModel model)
        {
            var newOrder = EquipmentReceiving.Create(model);
            if (newOrder == null)
            {
                return null;
            }
            using var context = new ComputerShopDatabase();
            context.EquipmentReceivings.Add(newOrder);
            context.SaveChanges();
            return context.EquipmentReceivings.FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel;
        }

        public EquipmentReceivingViewModel? Update(EquipmentReceivingBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            var order = context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id);
            if (order == null)
            {
                return null;
            }
            order.Update(model);
            context.SaveChanges();
            return context.EquipmentReceivings.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
        }
    }
}
