using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Blazored.Modal;
using Blazored.Modal.Services;
using Blazored.TextEditor;
using Blazored.Toast.Services;
using DiplomovaPrace.Contracts.Products.InputModels;
using DiplomovaPrace.Contracts.Products.ViewModels;
using DiplomovaPrace.Web.Admin.Infrastructure;
using DiplomovaPrace.Web.Admin.Services;
using DiplomovaPrace.Web.Admin.Shared.Common;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;

namespace DiplomovaPrace.Web.Admin.Pages.Products
{
	public partial class UpdateProduct
	{
		[Parameter]
		public int ProductID { get; set; }

		[CascadingParameter]
		public IModalService Modal { get; set; }

		private const int MaxAllowedImages = 10;

		private const long MaxAllowedImageSize = 1024 * 1024 * 10; // 10MB

		[Inject]
		private IProductService ProductService { get; set; }

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

		[Inject]
		private IToastService ToastService { get; set; }

		private List<IBrowserFile> imagesToUpload = new();

		private List<IBrowserFile> badImages = new(); // iamge size > 10MB

		private ProductDto productIM = new();

		private BlazoredTextEditor quillHtml;

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

			await ProductCategoryService.RequestProductCategoriesAsync().ConfigureAwait(false);
			productIM = await ProductService.GetProductAsync(ProductID).ConfigureAwait(false);

			await quillHtml.LoadHTMLContent(productIM.Description);
		}

		private async Task HandleSubmitAsync()
		{
			if (imagesToUpload is not null)
			{
				foreach (IBrowserFile image in imagesToUpload)
				{
					var fileContent = new StreamContent(image.OpenReadStream(MaxAllowedImageSize));

					productIM.ProductImages.Add(new ProductImageIM
					{
						ProductID = ProductID,
						FileName = image.Name,
						Image = await fileContent.ReadAsByteArrayAsync().ConfigureAwait(false)
					});
				}
			}

			productIM.Description = await quillHtml.GetHTML();
			await ProductService.UpdateProductAsync(productIM).ConfigureAwait(false);
		}

		private async Task OpenFile(InputFileChangeEventArgs e)
		{
			var images = e.GetMultipleFiles(MaxAllowedImages);

			foreach (var image in images)
			{
				if (image is not null)
				{
					var resizedImage = await image.RequestImageFileAsync("image/png", 1000, 1200);
					if (resizedImage.Size > MaxAllowedImageSize)
					{
						badImages.Add(resizedImage);
						continue;
					}

					imagesToUpload.Add(resizedImage);
				}
			}
		}

		private string GetImage(int imageID)
		{
			// TODO: otestovat
			return Path.Combine(NavigationManager.BaseUri, ApiRoutes.ProductImageController_GetImage(imageID));
		}

		private async Task ShowDeleteConfirmModalAsync(int imageID)
		{
			ModalOptions options = new()
			{
				DisableBackgroundCancel = true,
				Animation = ModalAnimation.FadeInOut(0.25),
				HideCloseButton = true
			};
			ModalParameters parameters = new();

			parameters.Add("ConfirmButtonText", "Smazat");
			parameters.Add("CancelButtonText", "Zrušit");
			parameters.Add("BodyText", "Opravdu chcete smazat zvolený obrázek?");

			ModalResult result = await Modal.Show<ConfirmModal>("Smazání obrázku", parameters, options).Result.ConfigureAwait(false);

			if (result.Cancelled)
			{
				return;
			}

			bool deleted = await ProductService.DeleteProductImageAsync(imageID).ConfigureAwait(true);
			if (deleted)
			{
				productIM.ProductImageIDs.Remove(imageID);
				ToastService.ShowSuccess("Obrázek byl úspěšně smazán.", "POVEDLO SE");
			}
			else
			{
				ToastService.ShowError("Obrázek se nepodařilo smazat.", "CHYBA");
			}
		}
	}
}