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

namespace AbstractFoodOrdersDatabaseImplement.Implements
{
    public class DishStorage : IDishStorage
    {
        public List<DishViewModel> GetFullList()
        {
            using var context = new AbstractFoodOrdersDatabase();
            return context.Dishes
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public List<DishViewModel> GetFilteredList(DishSearchModel model)
        {
            if (string.IsNullOrEmpty(model.DishName))
            {
                return new();
            }
            using var context = new AbstractFoodOrdersDatabase();
            return context.Dishes
             .Include(x => x.Components)
            .ThenInclude(x => x.Component)
            .Where(x => x.DishName.Contains(model.DishName))
            .ToList()
            .Select(x => x.GetViewModel)
            .ToList();
        }
        public DishViewModel? GetElement(DishSearchModel model)
        {
            if (string.IsNullOrEmpty(model.DishName) &&
            !model.Id.HasValue)
            {
                return null;
            }
            using var context = new AbstractFoodOrdersDatabase();
            return context.Dishes
             .Include(x => x.Components)
             .ThenInclude(x => x.Component)
             .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) &&
            x.DishName == model.DishName) ||
            (model.Id.HasValue && x.Id ==
            model.Id))
            ?.GetViewModel;
        }
        public DishViewModel? Insert(DishBindingModel model)
        {
            using var context = new AbstractFoodOrdersDatabase();
            var newProduct = Dish.Create(context, model);
            if (newProduct == null)
            {
                return null;
            }
            context.Dishes.Add(newProduct);
            context.SaveChanges();
            return newProduct.GetViewModel;
        }
        public DishViewModel? Update(DishBindingModel model)
        {
            using var context = new AbstractFoodOrdersDatabase();
            using var transaction = context.Database.BeginTransaction();
            try
            {
                var product = context.Dishes.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 DishViewModel? Delete(DishBindingModel model)
        {
            using var context = new AbstractFoodOrdersDatabase();
            var element = context.Dishes
            .Include(x => x.Components)
            .FirstOrDefault(rec => rec.Id == model.Id);
            if (element != null)
            {
                context.Dishes.Remove(element);
                context.SaveChanges();
                return element.GetViewModel;
            }
            return null;
        }
    }
}
