using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Blazored.Toast.Services;
using DiplomovaPrace.Contracts.ProductCategories;
using DiplomovaPrace.Contracts.ProductCategories.InputModels;
using DiplomovaPrace.Contracts.ProductCategories.ViewModels;
using DiplomovaPrace.Web.Admin.Store.ProductCategoryUseCase.Actions;
using Fluxor;
using Microsoft.AspNetCore.Components;

namespace DiplomovaPrace.Web.Admin.Services
{
	public class ProductCategoryService : IProductCategoryService
	{
		private readonly IProductCategoryFacadeContract productCategoryFacadeContract;
		private readonly IDispatcher dispatcher;
		private readonly IToastService toastService;
		private readonly NavigationManager navigationManager;

		public ProductCategoryService(IProductCategoryFacadeContract productCategoryFacadeContract, IDispatcher dispatcher, NavigationManager navigationManager, IToastService toastService)
		{
			this.productCategoryFacadeContract = productCategoryFacadeContract;
			this.dispatcher = dispatcher;
			this.navigationManager = navigationManager;
			this.toastService = toastService;
		}

		private async Task<List<ProductCategoryDto>> ReceiveProductCategoriesInternalAsync()
		{
			return await productCategoryFacadeContract.GetAllProductCategoriesAsync().ConfigureAwait(true);
		}

		public async Task CreateProductCategoryAsync(ProductCategoryIM inputModel)
		{
			ProductCategoryDto productCategory = await productCategoryFacadeContract.CreateProductCategoryAsync(inputModel).ConfigureAwait(true);
			dispatcher.Dispatch(new CreateProductCategoryAction(productCategory));

			toastService.ShowSuccess("Kategorie úspěšně vytvořena!", "ÚSPĚCH");
			navigationManager.NavigateTo("productcategories");
		}

		public async Task RequestProductCategoriesAsync()
		{
			dispatcher.Dispatch(new RequestProductCategoriesAction());
			dispatcher.Dispatch(new ReceiveProductCategoriesAction(await ReceiveProductCategoriesInternalAsync().ConfigureAwait(true)));
		}

		public Task<ProductCategoryDto> GetProductCategoryByIDAsync(int productCategoryID)
		{
			throw new NotImplementedException();
		}

		public async Task<List<ProductCategoryDto>> GetCategoryDescendantsAsync(int categoryID)
		{
			GetCategoryDescendantsResult result = await productCategoryFacadeContract.GetCategoryDescendantsAsync(new GetCategoryDescendantsRequest { ID = categoryID }).ConfigureAwait(false);

			if (result.IsSuccessful)
			{
				return result.Descendants;
			}

			return new List<ProductCategoryDto>();
		}

		public async Task UpdateProductCategoryAsync(ProductCategoryDto inputModel)
		{
			await productCategoryFacadeContract.UpdateProductCategoryAsync(inputModel).ConfigureAwait(false);
			dispatcher.Dispatch(new UpdateProductCategoryAction(inputModel.ID, inputModel));

			toastService.ShowSuccess("Kategorie úspěšně změněna!", "ÚSPĚCH");
			navigationManager.NavigateTo("productcategories");
		}

		public async Task<bool> DeleteProductCategoryAsync(int productCategoryID)
		{
			DeleteProductCategoryResult result = await productCategoryFacadeContract.DeleteProductCategoryAsync(new DeleteProductCategoryRequest { ID = productCategoryID }).ConfigureAwait(false);
			dispatcher.Dispatch(new DeleteProductCategoryAction(result.DeletedCategoryIDs));

			return result.IsSuccessful;
		}
	}
}
