using System.IO;
using System.Threading.Tasks;
using Blazored.TextEditor;
using DiplomovaPrace.Contracts.Products.InputModels;
using DiplomovaPrace.Web.Admin.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;

namespace DiplomovaPrace.Web.Admin.Pages.Products
{
	public partial class CreateProduct
	{
		[Inject]
		private IProductService ProductService { get; set; }

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

		private ProductIM productIM = new();

		private BlazoredTextEditor quillHtml;

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

			await ProductCategoryService.RequestProductCategoriesAsync().ConfigureAwait(false);
		}

		private async Task HandleSubmitAsync()
		{
			productIM.Description = await quillHtml.GetHTML();
			await ProductService.CreateProductAsync(productIM).ConfigureAwait(false);
		}

		private async Task OpenFileAsync(InputFileChangeEventArgs e)
		{
			MemoryStream ms = new();
			var resizedFile = await e.File.RequestImageFileAsync("image/png", 1000, 1200);

			await resizedFile.OpenReadStream(resizedFile.Size).CopyToAsync(ms);

			productIM.Image = ms.ToArray();
			productIM.FileName = e.File.Name;
		}
	}
}
