using System.IO;
using System.Threading.Tasks;
using DiplomovaPrace.Facades.Products;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace DiplomovaPrace.Web.Server.Controllers
{
	[Route("api/productImage")]
	[ApiController]
	public class ProductImageController : ControllerBase
	{
		private readonly IProductFacade productFacade;

		public ProductImageController(IProductFacade productFacade)
		{
			this.productFacade = productFacade;
		}

		[AllowAnonymous]
		[HttpGet("{imageID}")]
		public async Task<IActionResult> GetImage([FromRoute] int imageID)
		{
			FileStream image = await productFacade.GetProductImageAsync(imageID).ConfigureAwait(false);

			if (image is not null)
			{
				return File(image, MimeMapping.MimeUtility.GetMimeMapping(image.Name));
			}

			return NotFound();
		}
	}
}
