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

namespace DiplomovaPrace.Web.Admin.Pages.ProductCategories
{
	public partial class UpdateProductCategory
	{
		[Parameter]
		public int ProductCategoryID { get; set; }

		[Inject]
		public IState<ProductCategoryState> ProductCategoryState { get; set; }

		[Inject]
		private IProductCategoryService ProductCategoryService { get; set; }

		private ProductCategoryDto productCategory = new ProductCategoryDto();

		private List<ProductCategoryDto> productCategories = new List<ProductCategoryDto>();

		protected override async Task OnInitializedAsync()
		{
			await base.OnInitializedAsync().ConfigureAwait(false);
			await ProductCategoryService.RequestProductCategoriesAsync().ConfigureAwait(false);

			productCategories = ProductCategoryState.Value.ProductCategories;

			// ?? new()
			productCategory = ProductCategoryState.Value.ProductCategories.Find(i => i.ID == ProductCategoryID);
		}

		private async Task HandleSubmitAsync()
		{
			await ProductCategoryService.UpdateProductCategoryAsync(productCategory).ConfigureAwait(false);
		}
	}
}
