using Exsersewo.Common.Utilities;
using ImageMagick;
using Kynareth.Attributes;
using Kynareth.Helpers;
using Microsoft.AspNetCore.Mvc;
using Kynareth.Extensions;

namespace Kynareth.Controllers;

[Route("amogus")]
public class AmogusController : BaseController<AmogusController>
{
	public AmogusController(IConfiguration configuration, ILoggerFactory loggerFactory, IWebHostEnvironment appEnvironment) : base(configuration, loggerFactory, appEnvironment)
	{
	}

	[HttpGet("eject")]
	public async Task<object> Eject([FromQuery] string name, [FromQuery] AmogusRole role = AmogusRole.Unknown, [FromQuery] AmogusColour colour = AmogusColour.Red, [FromQuery] AmogusSkin skin = AmogusSkin.None)
	{
		if (string.IsNullOrWhiteSpace(name)) return StatusCode(StatusCodes.Status400BadRequest, EventResult.FromFailure(nameof(name) + " needs to be provided."));
		
		using var result = AmogusHelper.GenerateEjectionGif(name, colour, role, skin);

		if (result is MagickImageCollection data)
		{
			var memStream = new MemoryStream();
			data.Write(memStream, data.Count <= 1 ? MagickFormat.Png : MagickFormat.Gif);
			
			return HttpContext.SendStream(memStream, data.Count <= 1 ? "image/png" : "image/gif");
		}
		
		return StatusCode(StatusCodes.Status500InternalServerError);
	}
	
	[HttpGet("roles")]
	[IgnoreToken]
	public async Task<object> GetAmongUsRoles()
		=> Ok(EventResult.FromSuccess(Enum.GetNames<AmogusRole>().Except("None")));
	
	[HttpGet("colours")]
	[IgnoreToken]
	public async Task<object> GetAmongUsColours()
		=> Ok(EventResult.FromSuccess(Enum.GetNames<AmogusColour>().Except("None")));

	[HttpGet("skins")]
	[IgnoreToken]
	public async Task<object> GetAmongUsSkins()
		=> Ok(EventResult.FromSuccess(Enum.GetNames<AmogusSkin>().Except("None")));
}