using ComputersShopContracts.BindingModels;
using ComputersShopContracts.SearchModels;
using ComputersShopContracts.StoragesContracts;
using ComputersShopContracts.ViewModels;
using ComputersShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
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 newComputer = Computer.Create(context, model);
            if (newComputer == null)
            {
                return null;
            }
            context.Computers.Add(newComputer);
            context.SaveChanges();
            return newComputer.GetViewModel;
        }
        public ComputerViewModel? Update(ComputerBindingModel model)
        {
            using var context = new ComputersShopDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var Computer = context.Computers.FirstOrDefault(rec =>
                rec.Id == model.Id);
                if (Computer == null)
                {
                    return null;
                }
                Computer.Update(model);
                context.SaveChanges();
                Computer.UpdateComponents(context, model);
                transaction.Commit();
                return Computer.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;
        }

    }
}
