using DiplomovaPrace.Contracts.Customer.ProductCategories.ViewModels;
using DiplomovaPrace.DataLayer.Repositories.Products;
using DiplomovaPrace.Model.Products;
using Havit.Data.Patterns.DataLoaders;
using Havit.Extensions.DependencyInjection.Abstractions;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DiplomovaPrace.Facades.Customer.ProductCategories
{
	[Service]
	public class ProductCategoryFacade : IProductCategoryFacade
	{
		private readonly IDataLoader dataLoader;
		private readonly IProductCategoryRepository productCategoryRepository;

		public ProductCategoryFacade(IDataLoader dataLoader, IProductCategoryRepository productCategoryRepository)
		{
			this.dataLoader = dataLoader;
			this.productCategoryRepository = productCategoryRepository;
		}

		public async Task<List<ProductCategoryVM>> GetAllProductCategoriesAsync()
		{
			List<ProductCategory> productCategories = await productCategoryRepository.GetAllAsync();
			await dataLoader.LoadAllAsync(productCategories, i => i.Descendants);

			if (productCategories.Count > 0)
			{
				//TODO: poresit vraceni nacitani a vraceni potomku
				return productCategories.Where(i => i.ID != (int)ProductCategory.Entry.Root).Select(i => new ProductCategoryVM
				{
					ID = i.ID,
					Name = i.Name,
					Description = i.Description,
					ParentID = i.ParentID,
					Descendants = i.Descendants.Select(i => new ProductCategoryVM
					{
						ID = i.ID,
						Name = i.Name,
						Description = i.Description,
						ParentID = i.ParentID,
					}).ToList()
				}).ToList();
			}

			return null;
		}
	}
}
