using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.IO;
using System.Text;

namespace TeslaConsole;

public class PlasmaBitmaps
{
	public const string DefaultFontFamily = "Microsoft Sans Serif";

	private static bool sEnableCustomBitmaps;

	public static bool EnableCustomBitmaps
	{
		get
		{
			return sEnableCustomBitmaps;
		}
		set
		{
			sEnableCustomBitmaps = value;
		}
	}

	public static void GenerateBitmaps(out Bitmap large, out Bitmap small, string str)
	{
		GenerateBitmaps(out large, out small, "Microsoft Sans Serif", str);
	}

	public static void GenerateBitmaps(out Bitmap large, out Bitmap small, string fontName, string str)
	{
		large = GenerateBitmap(128, 32, fontName, str);
		small = GenerateBitmap(64, 16, fontName, str);
	}

	public static Bitmap GenerateBitmap(int width, int height, string str)
	{
		return GenerateBitmap(width, height, "Microsoft Sans Serif", str);
	}

	public static Bitmap GenerateBitmap(int width, int height, string fontName, string str)
	{
		if (sEnableCustomBitmaps)
		{
			string text = Path.Combine(Program.GetCommonAppDataDirectory(), $"Plasma Images\\{str}_{width}x{height}.bmp");
			if (File.Exists(text))
			{
				try
				{
					Bitmap bitmap = (Bitmap)Image.FromFile(text);
					if (bitmap.Width == width && bitmap.Height == height)
					{
						return bitmap;
					}
					bitmap.Dispose();
				}
				catch
				{
				}
			}
		}
		Bitmap bitmap2 = new Bitmap(width, height);
		using Graphics graphics = Graphics.FromImage(bitmap2);
		graphics.SmoothingMode = SmoothingMode.None;
		graphics.CompositingQuality = CompositingQuality.AssumeLinear;
		graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
		graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
		graphics.Clear(Color.Black);
		int num = 24;
		while (true)
		{
			using Font font = new Font(fontName, num);
			SizeF sizeF = graphics.MeasureString(str, font);
			if (sizeF.Width <= (float)bitmap2.Width && sizeF.Height <= (float)bitmap2.Height)
			{
				Rectangle rectangle = new Rectangle(0, 0, bitmap2.Width, bitmap2.Height);
				StringFormat stringFormat = new StringFormat();
				stringFormat.Alignment = StringAlignment.Center;
				stringFormat.LineAlignment = StringAlignment.Center;
				graphics.DrawString(str, font, Brushes.White, rectangle, stringFormat);
				return bitmap2;
			}
			num = ((num <= 2) ? (num / 2) : (num - 2));
		}
	}

	public static void GenerateStrings(out string large, out string small, string str)
	{
		GenerateStrings(out large, out small, "Microsoft Sans Serif", str);
	}

	public static void GenerateStrings(out string large, out string small, string fontName, string str)
	{
		large = GenerateString(128, 32, fontName, str);
		small = GenerateString(64, 16, fontName, str);
	}

	public static string GenerateString(int width, int height, string str)
	{
		return GenerateString(width, height, "Microsoft Sans Serif", str);
	}

	public static string GenerateString(int width, int height, string fontName, string str)
	{
		using Bitmap bitmap = GenerateBitmap(width, height, fontName, str);
		return ConvertBitmap(bitmap);
	}

	public static string ConvertBitmap(Bitmap bitmap)
	{
		if (bitmap.Width % 8 != 0 || bitmap.Height % 8 != 0)
		{
			throw new ArgumentOutOfRangeException("bitmap");
		}
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < bitmap.Height; i++)
		{
			stringBuilder.Append("bitmap=");
			for (int j = 0; j < bitmap.Width / 8; j++)
			{
				byte b = 0;
				for (int k = 0; k < 8; k++)
				{
					if (bitmap.GetPixel(j * 8 + k, i).GetBrightness() >= 0.5f)
					{
						b = (byte)(b | (byte)(128 >> k));
					}
				}
				stringBuilder.Append(b.ToString("X2"));
			}
			stringBuilder.Append("\n");
		}
		stringBuilder.AppendFormat("x={0}\n", bitmap.Width);
		stringBuilder.AppendFormat("y={0}\n", bitmap.Height);
		return stringBuilder.ToString();
	}
}
