using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Dalamud.Game.Command;
using Dalamud.Game.Text;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Hooking;
using Dalamud.Plugin;
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Component.GUI;

namespace AntiDickheadChatmodule;

public sealed class Plugin : IDalamudPlugin, IDisposable
{
	private unsafe delegate void* AgentShow(void* a1);

	private const string commandName = "/adh";

	public string Name => "Anti-Dickhead chatmodule";

	private IDalamudPluginInterface PluginInterface { get; init; }

	private ICommandManager CommandManager { get; init; }

	private Configuration Configuration { get; init; }

	private PluginUI PluginUi { get; init; }

	public long rcStartTime { get; private set; }

	private Hook<AgentShow>? DutyFinderHook { get; set; }

	public unsafe AtkUnitBase* dutyFinderAtkUnitBase { get; private set; }

	public string DataPath { get; } = string.Empty;

	public Plugin(IDalamudPluginInterface pluginInterface, ICommandManager commandManager)
	{
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Expected O, but got Unknown
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Expected O, but got Unknown
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Expected O, but got Unknown
		PluginInterface = pluginInterface;
		CommandManager = commandManager;
		pluginInterface.Create<Service>(Array.Empty<object>());
		Configuration = (PluginInterface.GetPluginConfig() as Configuration) ?? new Configuration();
		Configuration.Initialize(PluginInterface);
		PluginUi = new PluginUI(Configuration, DataPath);
		CommandManager.AddHandler("/adh", new CommandInfo(new HandlerDelegate(OnCommand))
		{
			HelpMessage = "Show the tracker UI"
		});
		PluginInterface.UiBuilder.Draw += DrawUI;
		PluginInterface.UiBuilder.OpenConfigUi += DrawConfigUI;
		Service.Chat.ChatMessage += new OnMessageDelegate(Chat_ChatMessage1);
	}

	private void Chat_ChatMessage1(XivChatType type, int timestamp, ref SeString sender, ref SeString message, ref bool isHandled)
	{
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		if (!new List<XivChatType>
		{
			(XivChatType)15,
			(XivChatType)37,
			(XivChatType)101,
			(XivChatType)102,
			(XivChatType)103,
			(XivChatType)104,
			(XivChatType)105,
			(XivChatType)106,
			(XivChatType)107,
			(XivChatType)14,
			(XivChatType)11,
			(XivChatType)10,
			(XivChatType)30,
			(XivChatType)24,
			(XivChatType)13,
			(XivChatType)27
		}.Contains(type))
		{
			return;
		}
		try
		{
			bool flag = false;
			foreach (Payload payload in message.Payloads)
			{
				TextPayload val = (TextPayload)(object)((payload is TextPayload) ? payload : null);
				if (val == null)
				{
					continue;
				}
				if (flag)
				{
					flag = false;
					continue;
				}
				string text = val.Text;
				Service.PluginLog.Debug(text ?? string.Empty, Array.Empty<object>());
				if (string.IsNullOrEmpty(text) || text.Contains('\ue0bb'))
				{
					Service.PluginLog.Debug("Skipping, because empty or symbol", Array.Empty<object>());
					flag = true;
					continue;
				}
				string text2 = text;
				if (text2.Contains(".") || char.IsUpper(text2[0]))
				{
					string[] array = text2.Split(" ");
					for (int i = 0; i < array.Length; i++)
					{
						if (array[i].Length > 1 && char.IsUpper(array[i][0]) && !char.IsUpper(array[i][1]) && array[i][1] != '\'')
						{
							array[i] = array[i].ToLower();
						}
						if (!array[i].EndsWith("."))
						{
							continue;
						}
						if (i + 1 < array.Length)
						{
							string pattern = "((?::|;|=)(?:-)?(?:\\)|D|P))";
							if (Regex.Match(array[i + 1], pattern, RegexOptions.IgnoreCase).Success)
							{
								array[i] = array[i].Substring(0, array[i].Length - 1);
							}
						}
						else
						{
							array[i] = array[i].Substring(0, array[i].Length - 1);
						}
					}
					string text3 = "";
					string[] array2 = array;
					foreach (string text4 in array2)
					{
						text3 = text3 + text4 + " ";
					}
					text2 = text3;
				}
				if (!text.Equals(text2))
				{
					val.Text = text2;
				}
			}
		}
		catch
		{
			Service.PluginLog.Debug($"Failed to process message: {message}.", Array.Empty<object>());
		}
	}

	public void Dispose()
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		PluginUi.Dispose();
		CommandManager.RemoveHandler("/adh");
		Service.Chat.ChatMessage -= new OnMessageDelegate(Chat_ChatMessage1);
	}

	private void OnCommand(string command, string args)
	{
		PluginUi.Visible = true;
	}

	private void DrawUI()
	{
		PluginUi.Draw();
	}

	private void DrawConfigUI()
	{
		PluginUi.SettingsVisible = true;
	}
}
