using System;
using System.Collections;
using DarkTonic.MasterAudio;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

// Token: 0x020000DD RID: 221
public class TextParser : MonoBehaviour
{
	// Token: 0x060014A0 RID: 5280 RVA: 0x0018FD94 File Offset: 0x0018DF94
	private void Awake()
	{
		this.inputField.text = "";
		this.rt = base.gameObject.GetComponent<RectTransform>();
		this.animator = base.gameObject.GetComponent<Animator>();
		this.img = base.gameObject.transform.GetChild(1).gameObject.GetComponent<Image>();
		this.img2 = base.gameObject.transform.GetChild(0).gameObject.GetComponent<Image>();
		this.img2.enabled = false;
		this.img.gameObject.SetActive(true);
		this.img2.gameObject.SetActive(true);
		this.contents1.SetActive(false);
		this.contents2.SetActive(false);
		this.img.enabled = false;
		this.animator.enabled = false;
	}

	// Token: 0x060014A1 RID: 5281 RVA: 0x0018FE74 File Offset: 0x0018E074
	public void StartMenu(Portrait portrait)
	{
		if (this.cannotInterrupt)
		{
			return;
		}
		this.character = portrait.character;
		if (this.character.stunned || this.character.dead)
		{
			return;
		}
		if (Links.x.mk)
		{
			EventSystem.current.SetSelectedGameObject(this.inputField.gameObject);
		}
		if (this.parsing != null)
		{
			base.StopCoroutine(this.parsing);
			this.parsing = null;
		}
		this.inputField.text = "";
		Records.x.SetTypingState(true);
		if (!this.character.mainSelected)
		{
			this.character.portrait.SelectCharacter();
		}
		base.gameObject.transform.position = portrait.queuedIconBorder.gameObject.transform.position;
		base.gameObject.transform.SetAsLastSibling();
		this.contents1.SetActive(true);
		this.contents2.SetActive(true);
		Records.x.shouting = true;
		this.animator.enabled = true;
		this.img2.enabled = true;
		this.animator.Play("TextParserStart");
		MasterAudio.PlaySoundAndForget("Paper Open 2", Random.Range(0.2f, 0.3f), new float?(1f), 0f, "", null);
		this.parsedWord = "";
		this.open = true;
		this.inputField.interactable = true;
		this.inputField.OnSelect(null);
		if (Links.x.joy)
		{
			Links.x.partyColliderAdventure.ToggleMenu(false);
			Links.x.keyboardScript.StartMenu(this.inputField, null, base.gameObject, "Shout");
			return;
		}
		base.StartCoroutine(this.SelectInput());
	}

	// Token: 0x060014A2 RID: 5282 RVA: 0x00190051 File Offset: 0x0018E251
	private IEnumerator SelectInput()
	{
		yield return new WaitForSeconds(0.1f);
		this.inputField.OnSelect(null);
		yield break;
	}

	// Token: 0x060014A3 RID: 5283 RVA: 0x00190060 File Offset: 0x0018E260
	public void EndFromRaycast()
	{
		if (Links.x.mk)
		{
			this.EndMenuStart();
		}
	}

	// Token: 0x060014A4 RID: 5284 RVA: 0x00190074 File Offset: 0x0018E274
	public void KeyboardParse()
	{
		if (Links.x.mk)
		{
			this.Parse();
		}
	}

	// Token: 0x060014A5 RID: 5285 RVA: 0x00190088 File Offset: 0x0018E288
	public void EndMenuStart()
	{
		if (this.parsedWord != "")
		{
			return;
		}
		this.animator.Play("TextParserEnd");
		this.inputField.interactable = false;
		this.img2.enabled = false;
		if (Links.x.joy)
		{
			Links.x.gameplay.endDockNavigation = true;
			Links.x.hudControl.JoyBox(false);
			Links.x.partyColliderAdventure.ToggleMenu(true);
		}
	}

	// Token: 0x060014A6 RID: 5286 RVA: 0x0019010C File Offset: 0x0018E30C
	public void StartEndMenu()
	{
	}

	// Token: 0x060014A7 RID: 5287 RVA: 0x00190110 File Offset: 0x0018E310
	public void EndMenu()
	{
		this.open = false;
		Records.x.SetTypingState(false);
		Records.x.shouting = false;
		this.contents1.SetActive(false);
		this.contents2.SetActive(false);
		this.img.enabled = false;
		this.animator.enabled = false;
		this.img2.enabled = false;
	}

	// Token: 0x060014A8 RID: 5288 RVA: 0x00190178 File Offset: 0x0018E378
	public void Parse()
	{
		string text = this.inputField.text;
		this.originalWord = text;
		if (Links.x.mk && (text == "" || text == " " || text == "  " || text == "    "))
		{
			return;
		}
		this.parsedWord = text.ToLower();
		this.animator.Play("TextParserDone");
		Debug.Log("Parse");
	}

	// Token: 0x060014A9 RID: 5289 RVA: 0x001901FF File Offset: 0x0018E3FF
	public void ParseSuccess()
	{
		if (this.parsing != null)
		{
			base.StopCoroutine(this.parsing);
			this.parsing = null;
		}
		this.parsing = this.Parsing();
		base.StartCoroutine(this.Parsing());
	}

	// Token: 0x060014AA RID: 5290 RVA: 0x00190238 File Offset: 0x0018E438
	private void PauseVisual()
	{
		string text = this.parsedWord;
		if (text.Length >= 3)
		{
			this.parsedWord.Substring(0, 3);
		}
		text += "...";
		this.inputField.text = text;
		this.animator.Play("TextParserWaitingForEnd");
		this.img2.enabled = false;
		Records.x.SetTypingState(false);
		Records.x.shouting = false;
	}

	// Token: 0x060014AB RID: 5291 RVA: 0x001902AD File Offset: 0x0018E4AD
	private IEnumerator Parsing()
	{
		this.inputField.interactable = false;
		bool changeAnim = false;
		float currentTime = this.animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
		string word = this.parsedWord;
		string abilityName = "";
		bool wordOfPower = false;
		while (Records.x.paused)
		{
			if (!changeAnim)
			{
				this.PauseVisual();
				changeAnim = true;
				if (wordOfPower && this.character.actions)
				{
					while (this.character.inRecovery)
					{
						yield return null;
					}
					if (this.character.actions)
					{
						this.character.actions.ClearSelectedActionRealTime();
					}
					if (this.character.actions)
					{
						this.character.actions.Interrupt();
					}
					if (this.character.actions)
					{
						this.character.actions.CheckIfRunningAI();
					}
					this.character.actions.hasWaypoint = false;
					this.character.actions.ClearTargetCharacters();
					this.character.actions.RemoveAbility();
					this.character.actions.Ability(abilityName, "", false, true);
					this.character.timelineIcon.WaitForAction(this.character.actions.GetActionSpeed("Word"), true, true);
				}
			}
			yield return null;
		}
		if (Records.x.pocketPause)
		{
			Links.x.gameplay.PocketPause(false);
		}
		this.parsedWord = "";
		if (changeAnim)
		{
			this.inputField.text = word;
			this.animator.Play("TextParserDone", 0, currentTime + 0.1f);
		}
		this.cannotInterrupt = true;
		MasterAudio.PlaySoundAndForget("Feedback", 0.2f, new float?(1f), 0f, "Box Close", null);
		bool doQuip = false;
		bool flag = false;
		if (word == "bribe" || word == "sorry" || word == "stop")
		{
			if (Records.x.BribeBadgeCount() > 0)
			{
				Character character = Links.x.sensory.BribeCharacterNear(this.character, false);
				this.character.StartQuip(word + "!");
				if (character)
				{
					Links.x.gameplay.StartDialogue(character, this.character, null, null, "Party/Bribe");
				}
				else
				{
					Links.x.gameFeed.ShowNotice("No one heard you");
				}
			}
			else
			{
				Character bribeCharacter = Links.x.sensory.BribeCharacterNear(this.character, false);
				this.character.StartQuip(word + "!");
				Links.x.gameFeed.AddFeed("No need to bribe anyone at this time");
				if (bribeCharacter)
				{
					yield return new WaitForSeconds(1f);
					bribeCharacter.StartQuip("No need");
				}
				bribeCharacter = null;
			}
			flag = true;
		}
		if (word == "topa-din" || word == "topadin" || word == "topa din")
		{
			this.character.StartQuip("Topa-din");
			Character bribeCharacter = Links.x.sensory.NearestNPC(this.character);
			if (bribeCharacter)
			{
				yield return new WaitForSeconds(0.75f);
				bribeCharacter.StartQuip("Topa-du");
				if (!bribeCharacter.inCombat)
				{
					bribeCharacter.SetRotation(Quaternion.LookRotation(this.character.tr.position - bribeCharacter.tr.position), true, false);
				}
			}
			flag = true;
			bribeCharacter = null;
		}
		if (word == "topa du" || word == "topadu" || word == "topa-du")
		{
			this.character.StartQuip("Topa-du");
			Character bribeCharacter = Links.x.sensory.NearestNPC(this.character);
			if (bribeCharacter)
			{
				yield return new WaitForSeconds(0.75f);
				bribeCharacter.StartQuip("Topa-din");
				if (!bribeCharacter.inCombat)
				{
					bribeCharacter.SetRotation(Quaternion.LookRotation(this.character.tr.position - bribeCharacter.tr.position), true, false);
				}
			}
			flag = true;
			bribeCharacter = null;
		}
		if (Links.x.gaia.boroControls && word == Records.x.boroName.ToLower())
		{
			Links.x.gameplay.CallBoro();
		}
		if (doQuip)
		{
			this.character.StartQuip(this.originalWord + "!");
		}
		if (word.Contains("deadhands526"))
		{
			Records.x.secrets = true;
			Links.x.inputControls.enabled = true;
			Links.x.inputControls.Lists();
			Links.x.inputControls.NewScene();
			Links.x.inputControls.child.SetActive(true);
		}
		if (!flag)
		{
			this.character.StartQuip(this.originalWord + "!");
		}
		Records.x.SetShout(this.originalWord, Time.timeSinceLevelLoad);
		this.cannotInterrupt = false;
		this.parsing = null;
		if (Links.x.joy || Links.x.mk)
		{
			this.EndMenuStart();
		}
		yield break;
	}

	// Token: 0x040023B5 RID: 9141
	public TMP_InputField inputField;

	// Token: 0x040023B6 RID: 9142
	public bool open;

	// Token: 0x040023B7 RID: 9143
	private Portrait portrait;

	// Token: 0x040023B8 RID: 9144
	private Character character;

	// Token: 0x040023B9 RID: 9145
	private RectTransform rt;

	// Token: 0x040023BA RID: 9146
	private Animator animator;

	// Token: 0x040023BB RID: 9147
	public string parsedWord;

	// Token: 0x040023BC RID: 9148
	public string originalWord;

	// Token: 0x040023BD RID: 9149
	public GameObject contents1;

	// Token: 0x040023BE RID: 9150
	public GameObject contents2;

	// Token: 0x040023BF RID: 9151
	private Image img;

	// Token: 0x040023C0 RID: 9152
	private Image img2;

	// Token: 0x040023C1 RID: 9153
	public bool cannotInterrupt;

	// Token: 0x040023C2 RID: 9154
	private IEnumerator parsing;

	// Token: 0x040023C3 RID: 9155
	private StringFast str = new StringFast(64);
}
