using System.Numerics;
using System.Text;
using ImageMagick;
using Kynareth.Models;

namespace Kynareth.Extensions;

public static class MagickImageExtensions
{
	public static MagickImage WriteText(this MagickImage image, string text, PositionRect rect, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest)
	{
		using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings()
		{
			Width = rect.Width,
			Height = rect.Height,
			BackgroundColor = MagickColors.Transparent,
			FillColor = fontColor,
			TextGravity = textGravity,
			Font = font,
			TextEncoding = encoding,
			StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black),
			StrokeWidth = strokeWidth,
		});
		
		image.Composite(label, rect.X, rect.Y, CompositeOperator.Over);
		
		return image;
	}

	public static MagickImage WriteText(this MagickImage image, string text, PositionRect rect, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest)
	{
		using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings()
		{
			Width = rect.Width,
			Height = rect.Height,
			BackgroundColor = MagickColors.Transparent,
			FillColor = fontColor,
			TextGravity = textGravity,
			FontPointsize = fontSize,
			Font = font,
			TextEncoding = encoding,
			StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black),
			StrokeWidth = strokeWidth
		});

		image.Composite(label, rect.X, rect.Y, CompositeOperator.Over);
		
		return image;
	}
	
	public static MagickImage WriteText(this MagickImage image, string text, Vector2 position, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest)
	{
		using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings()
		{
			Width = image.Width,
			Height = image.Height,
			BackgroundColor = MagickColors.Transparent,
			FillColor = fontColor,
			TextGravity = textGravity,
			Font = font,
			TextEncoding = encoding,
			StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black),
			StrokeWidth = strokeWidth
		});

		image.Composite(label, (int)position.X, (int)position.Y, CompositeOperator.Over);
		
		return image;
	}
	
	public static MagickImage WriteText(this MagickImage image, string text, Vector2 position, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest)
	{
		using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings()
		{
			Width = image.Width,
			Height = image.Height,
			BackgroundColor = MagickColors.Transparent,
			FillColor = fontColor,
			TextGravity = textGravity,
			FontPointsize = fontSize,
			Font = font,
			TextEncoding = encoding,
			StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black),
			StrokeWidth = strokeWidth
		});

		image.Composite(label, (int)position.X, (int)position.Y, CompositeOperator.Over);
		
		return image;
	}
	
	public static MagickImage WriteText(this MagickImage image, string text, PointD position, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest)
	{
		using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings()
		{
			Width = image.Width,
			Height = image.Height,
			BackgroundColor = MagickColors.Transparent,
			FillColor = fontColor,
			TextGravity = textGravity,
			Font = font,
			TextEncoding = encoding,
			StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black),
			StrokeWidth = strokeWidth
		});

		image.Composite(label, (int)position.X, (int)position.Y, CompositeOperator.Over);
		
		return image;
	}
	
	public static MagickImage WriteText(this MagickImage image, string text, PointD position, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest)
	{
		using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings()
		{
			Width = image.Width,
			Height = image.Height,
			BackgroundColor = MagickColors.Transparent,
			FillColor = fontColor,
			TextGravity = textGravity,
			FontPointsize = fontSize,
			Font = font,
			TextEncoding = encoding,
			StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black),
			StrokeWidth = strokeWidth
		});

		image.Composite(label, (int)position.X, (int)position.Y, CompositeOperator.Over);
		
		return image;
	}
	
	public static MagickImage WriteText(this MagickImage image, string text, int x, int y, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest)
	{
		using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings()
		{
			Width = image.Width,
			Height = image.Height,
			BackgroundColor = MagickColors.Transparent,
			FillColor = fontColor,
			TextGravity = textGravity,
			Font = font,
			TextEncoding = encoding,
			StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black),
			StrokeWidth = strokeWidth
		});

		image.Composite(label, x, y, CompositeOperator.Over);
		
		return image;
	}
	
	public static MagickImage WriteText(this MagickImage image, string text, int x, int y, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest)
	{
		using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings()
		{
			Width = image.Width,
			Height = image.Height,
			BackgroundColor = MagickColors.Transparent,
			FillColor = fontColor,
			TextGravity = textGravity,
			FontPointsize = fontSize,
			Font = font,
			TextEncoding = encoding,
			StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black),
			StrokeWidth = strokeWidth
		});

		image.Composite(label, x, y, CompositeOperator.Over);
		
		return image;
	}
}