using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StoragesContracts;
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 ComputerStorage : IComputerStorage
    {
        public List<ComputerViewModel> GetFullList()
        {
            using var context = new ComputerShopDatabase();
            return context.Computers
            .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<ComputerViewModel> GetFilteredList(ComputerSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ComputerName))
            {
                return new();
            }
            using var context = new ComputerShopDatabase();
            return context.Computers
            .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .Where(x => x.ComputerName.Contains(model.ComputerName))
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public ComputerViewModel? GetElement(ComputerSearchModel model)
        {
            if (string.IsNullOrEmpty(model.ComputerName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new ComputerShopDatabase();
            return context.Computers
            .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) &&
            x.ComputerName == model.ComputerName) ||
            (model.Id.HasValue && x.Id ==
            model.Id))
            ?.GetViewModel;
        }
        public ComputerViewModel? Insert(ComputerBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            var newProduct = Computer.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Computers.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;
        }
        public ComputerViewModel? Update(ComputerBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.Computers.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 ComputerViewModel? Delete(ComputerBindingModel model)
        {
            using var context = new ComputerShopDatabase();
            var element = context.Computers
            .Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Computers.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}