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

namespace ComputerShopDatabaseImplement.Implements
{
    public class AssemblyStorage : IAssemblyStorage
    {
        public List<AssemblyViewModel> GetFullList()
        {
            using var context = new ComputerShopDatabase();
            return context.Assemblies
            .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<AssemblyViewModel> GetFilteredList(AssemblySearchModel model)
        {
            if (string.IsNullOrEmpty(model.AssemblyName) && model.ClientId == null)
            {
                return new();
            }
            using var context = new ComputerShopDatabase();
            if (model.ClientId != null)
                 return context.Assemblies
                .Include(x => x.Components)
                .ThenInclude(x => x.Component)
                .Where(x => x.ClientId == model.ClientId)
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            else
            {
                 return context.Assemblies
                .Include(x => x.Components)
                .ThenInclude(x => x.Component)
                .Where(x => x.AssemblyName.Contains(model.AssemblyName))
                .ToList()
                .Select(x => x.GetViewModel)
                .ToList();
            }
        }
        public AssemblyViewModel? GetElement(AssemblySearchModel model)
        {
            if (string.IsNullOrEmpty(model.AssemblyName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new ComputerShopDatabase();
            return context.Assemblies
            .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .FirstOrDefault(x => (!string.IsNullOrEmpty(model.AssemblyName) &&
            x.AssemblyName == model.AssemblyName) ||
            (model.Id.HasValue && x.Id ==
            model.Id))
            ?.GetViewModel;
        }
        public AssemblyViewModel? Insert(AssemblyBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            var newProduct = Assembly.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Assemblies.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;
        }
        public AssemblyViewModel? Update(AssemblyBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.Assemblies.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (product == null)
                {
                    return null;
                }
                product.Update(model);
                context.SaveChanges();
                product.UpdateComponents(context, model);
                transaction.Commit();
                return product.GetViewModel;
            }
            catch
            {
                transaction.Rollback();
                throw;
            }
        }
        public AssemblyViewModel? Delete(AssemblyBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            var element = context.Assemblies
            .Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Assemblies.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
