using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using System.Text;
using Dalamud.Bindings.ImGui;
using Dalamud.Interface;
using Dalamud.Plugin.Services;

namespace OmicronMountMusicFixer;

internal static class Util
{
	public static List<string> AvailableTitleLanguages => new List<string> { "en" };

	internal static bool TryScanText(this ISigScanner scanner, string sig, out nint result)
	{
		result = IntPtr.Zero;
		try
		{
			result = scanner.ScanText(sig);
			return true;
		}
		catch (KeyNotFoundException)
		{
			return false;
		}
	}

	public static string Lang()
	{
		return "en";
	}

	private unsafe static byte[] ReadTerminatedBytes(byte* ptr)
	{
		if (ptr == null)
		{
			return new byte[0];
		}
		List<byte> list = new List<byte>();
		while (*ptr != 0)
		{
			list.Add(*ptr);
			ptr++;
		}
		return list.ToArray();
	}

	internal unsafe static string ReadTerminatedString(byte* ptr)
	{
		return Encoding.UTF8.GetString(ReadTerminatedBytes(ptr));
	}

	internal static bool ContainsIgnoreCase(this string haystack, string needle)
	{
		return CultureInfo.InvariantCulture.CompareInfo.IndexOf(haystack, needle, CompareOptions.IgnoreCase) >= 0;
	}

	internal static bool IconButton(FontAwesomeIcon icon, string id)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		ImGui.PushFont(UiBuilder.IconFont);
		ImU8String val = default(ImU8String);
		((ImU8String)(ref val))._002Ector(2, 2);
		((ImU8String)(ref val)).AppendFormatted<string>(FontAwesomeExtensions.ToIconString(icon));
		((ImU8String)(ref val)).AppendLiteral("##");
		((ImU8String)(ref val)).AppendFormatted<string>(id);
		bool result = ImGui.Button(val, default(Vector2));
		ImGui.PopFont();
		return result;
	}
}
