using Microsoft.EntityFrameworkCore;
using ComputersShopContracts.BindingModels;
using ComputersShopContracts.SearchModels;
using ComputersShopContracts.StoragesContracts;
using ComputersShopContracts.ViewModels;
using ComputersShopDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComputersShopDataBaseImplement.Implements
{
    public class ComputerStorage : IComputerStorage
    {
        public List<ComputerViewModel> GetFullList()
        {
            using var context = new ComputersShopDataBase();
            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 ComputersShopDataBase();
            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 ComputersShopDataBase();
            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 ComputersShopDataBase();
            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 ComputersShopDataBase();
            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 ComputersShopDataBase();
            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;
        }

    }
}
