using System;
using PixelCrushers.DialogueSystem;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

// Token: 0x020000C7 RID: 199
public class OverHeadUI : MonoBehaviour
{
	// Token: 0x06001287 RID: 4743 RVA: 0x001669DC File Offset: 0x00164BDC
	private void Awake()
	{
		this.tr = base.transform;
		this.originalScale = this.tr.localScale;
		if (this.img)
		{
			this.img.enabled = false;
			if (this.sprite1 != null)
			{
				this.img.sprite = this.sprite1;
			}
		}
		if (this.img2)
		{
			this.img2.enabled = false;
		}
		if (this.text)
		{
			this.text.enabled = false;
			this.text.text = "";
		}
		if (this.textWorld)
		{
			this.textWorld.enabled = false;
			this.textWorld.text = "";
		}
		if (!this.camTr)
		{
			this.camTr = Links.x.worldCamera.gameObject.transform;
		}
		this.forw = Vector3.forward;
		this.up = Vector3.up;
	}

	// Token: 0x06001288 RID: 4744 RVA: 0x00166AE8 File Offset: 0x00164CE8
	public void Setup(Body playerBody)
	{
		if (!playerBody)
		{
			return;
		}
		this.canvas = Links.x.hudCanvas;
		this.body = playerBody;
		this.player = this.body.character;
		this.tr.localScale = this.originalScale;
		if (this.img)
		{
			this.img.enabled = true;
		}
		if (this.img2)
		{
			this.img2.enabled = true;
		}
		if (this.text)
		{
			this.text.enabled = true;
			if (!this.textRT)
			{
				this.textRT = this.text.gameObject.GetComponent<RectTransform>();
			}
			this.textRT.sizeDelta = new Vector2(this.text.preferredWidth, this.text.preferredHeight);
			this.rt.sizeDelta = new Vector2(this.textRT.sizeDelta.x, this.textRT.sizeDelta.y);
			this.textSize = new Vector2(this.text.preferredWidth, this.text.preferredHeight);
		}
		if (this.textWorld)
		{
			this.textWorld.enabled = true;
			this.l = (float)this.textWorld.text.Length;
		}
		this.startTime = Time.timeSinceLevelLoad;
		this.startGameTime = Links.x.gameplay.seconds;
		this.opacity = 1f;
		this.animTime = Time.realtimeSinceStartup;
		this.currentAnimIndex = 0;
		if (this.doAnimation)
		{
			this.img.enabled = false;
			this.img.sprite = this.sprite1;
			this.textRT.localScale = new Vector3(0f, 0f, 0f);
			this.spriteSizeMultiplierX = this.textRT.sizeDelta.x / this.sprite6size.x;
			this.spriteSizeMultiplierY = this.textRT.sizeDelta.y / this.sprite6size.y;
			this.rt.sizeDelta = this.sprite1size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
		}
		this.ending = false;
	}

	// Token: 0x06001289 RID: 4745 RVA: 0x00166D48 File Offset: 0x00164F48
	public void ReturnToCellar()
	{
		this.emphasize = false;
		this.txt = "";
		if (!this.menu)
		{
			this.tr.position = new Vector3(10000f, 100000f, 0f);
		}
		if (this.img)
		{
			this.img.enabled = false;
			if (this.sprite1 != null)
			{
				this.img.sprite = this.sprite1;
			}
		}
		if (this.img2)
		{
			this.img2.enabled = false;
		}
		if (this.text)
		{
			this.text.enabled = false;
			this.txt = "";
			this.text.text = "";
		}
		if (this.textWorld)
		{
			this.textWorld.enabled = false;
			this.textWorld.text = "";
		}
		if (this.typewriter)
		{
			this.typewriter.enabled = false;
		}
		if (this.typewriter)
		{
			Object.Destroy(this.typewriter);
		}
	}

	// Token: 0x0600128A RID: 4746 RVA: 0x00166E70 File Offset: 0x00165070
	public void UpdateImage2(int state)
	{
		if (state == 0 || !Records.x.InCombat(false))
		{
			if (this.img2.enabled)
			{
				this.img2.enabled = false;
				return;
			}
		}
		else
		{
			if (state == 2)
			{
				this.img2.enabled = true;
				return;
			}
			if (state == 1)
			{
				this.img2.enabled = true;
				this.img2.color = Links.x.GetPartyColorRGB(this.player);
			}
		}
	}

	// Token: 0x0600128B RID: 4747 RVA: 0x00166EE4 File Offset: 0x001650E4
	public void End()
	{
		if (this.doAnimation)
		{
			this.ending = true;
			this.animTime = Time.realtimeSinceStartup - 0.03f;
			this.currentAnimIndex = 0;
			this.img.sprite = this.sprite5;
			this.rt.sizeDelta = this.sprite5size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
			if (!this.textRT)
			{
				this.textRT = this.text.gameObject.GetComponent<RectTransform>();
			}
			this.textRT.localScale = new Vector3(0f, 0f, 0f);
			if (this.typewriter)
			{
				Object.Destroy(this.typewriter);
				return;
			}
		}
		else
		{
			this.EndFinal();
		}
	}

	// Token: 0x0600128C RID: 4748 RVA: 0x00166FB4 File Offset: 0x001651B4
	public void EndFinal()
	{
		base.gameObject.SetActive(false);
		this.player = null;
		this.body = null;
		this.ending = false;
		this.currentAnimIndex = 0;
		if (this.doAnimation)
		{
			if (this.img)
			{
				this.img.sprite = this.sprite1;
			}
			if (this.textRT)
			{
				this.textRT.localScale = new Vector3(0f, 0f, 0f);
				this.spriteSizeMultiplierX = this.textRT.sizeDelta.x / this.sprite6size.x;
				this.spriteSizeMultiplierY = this.textRT.sizeDelta.y / this.sprite6size.y;
			}
			if (this.rt)
			{
				this.rt.sizeDelta = this.sprite1size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
			}
		}
		if (this.img)
		{
			this.img.enabled = false;
		}
		if (this.img2)
		{
			this.img2.enabled = false;
		}
		if (this.text)
		{
			this.text.enabled = false;
			this.txt = "";
			this.text.text = "";
		}
		if (this.textWorld)
		{
			this.textWorld.enabled = false;
			this.textWorld.text = "";
		}
		if (this.typewriter)
		{
			this.typewriter.enabled = false;
		}
		if (this.toCellar)
		{
			this.ReturnToCellar();
		}
		if (this.typewriter)
		{
			Object.Destroy(this.typewriter);
		}
	}

	// Token: 0x0600128D RID: 4749 RVA: 0x00167184 File Offset: 0x00165384
	private void LateUpdate()
	{
		if (Links.x.gameplay.seconds > this.startGameTime + 144f)
		{
			this.EndFinal();
			return;
		}
		this.Position();
	}

	// Token: 0x0600128E RID: 4750 RVA: 0x001671B0 File Offset: 0x001653B0
	private void Position()
	{
		float num = this.yHeight;
		Camera camera = Links.x.worldCamera;
		if (this.menu)
		{
			camera = Links.x.camera3D;
		}
		if (this.menu)
		{
			num += 2f;
		}
		if (this.doAnimation)
		{
			if (Time.realtimeSinceStartup > this.animTime + 0.03f && (this.ending || this.currentAnimIndex < 6))
			{
				this.animTime = Time.realtimeSinceStartup;
				this.currentAnimIndex++;
				if (this.ending)
				{
					if (this.currentAnimIndex >= 5)
					{
						this.EndFinal();
						return;
					}
					if (this.currentAnimIndex == 1)
					{
						this.img.sprite = this.sprite4;
						this.rt.sizeDelta = this.sprite4size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
					}
					if (this.currentAnimIndex == 2)
					{
						this.img.sprite = this.sprite3;
						this.rt.sizeDelta = this.sprite3size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
					}
					if (this.currentAnimIndex == 3)
					{
						this.img.sprite = this.sprite2;
						this.rt.sizeDelta = this.sprite2size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
					}
					if (this.currentAnimIndex == 4)
					{
						this.img.sprite = this.sprite1;
						this.rt.sizeDelta = this.sprite1size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
					}
				}
				else
				{
					if (this.currentAnimIndex == 1)
					{
						this.img.enabled = true;
						this.img.sprite = this.sprite2;
						this.rt.sizeDelta = this.sprite2size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
					}
					if (this.currentAnimIndex == 2)
					{
						this.img.enabled = true;
						this.img.sprite = this.sprite3;
						this.rt.sizeDelta = this.sprite3size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
					}
					if (this.currentAnimIndex == 3)
					{
						this.img.enabled = true;
						this.img.sprite = this.sprite4;
						this.rt.sizeDelta = this.sprite4size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
					}
					if (this.currentAnimIndex == 4)
					{
						this.img.enabled = true;
						this.img.sprite = this.sprite5;
						this.rt.sizeDelta = this.sprite5size * new Vector2(this.spriteSizeMultiplierX, this.spriteSizeMultiplierY);
					}
					if (this.currentAnimIndex == 5)
					{
						this.img.enabled = true;
						this.img.sprite = this.sprite6;
						this.textRT.localScale = new Vector3(1f, 1f, 1f);
						this.rt.sizeDelta = this.textRT.sizeDelta;
						this.img.type = Image.Type.Sliced;
					}
				}
			}
			if (this.textRT)
			{
				this.spriteSizeMultiplierX = this.textRT.sizeDelta.x / this.sprite6size.x;
				this.spriteSizeMultiplierY = this.textRT.sizeDelta.y / this.sprite6size.y;
				if (this.textRT.sizeDelta.y != this.textSize.y)
				{
					this.textRT.sizeDelta = new Vector2(this.textSize.x, this.textSize.y);
					this.rt.sizeDelta = new Vector2(this.textRT.sizeDelta.x, this.textRT.sizeDelta.y);
				}
			}
		}
		if (this.emphasize && !this.menu && Time.timeSinceLevelLoad > this.startTime + 0.4f && this.tr.localScale == this.originalScale)
		{
			this.tr.localScale = this.originalScale * 1.15f;
			Links.x.itemPickupFX.gameObject.SetActive(true);
			Links.x.itemPickupFX.gameObject.GetComponent<HannahAnimator>().Play("Idle");
			Vector3 position = this.tr.GetChild(0).position;
			Vector3 vector = Links.x.worldCamera.WorldToScreenPoint(position);
			vector.z = 0f;
			vector.x = vector.x * (1920f / (float)Screen.width) - 960f;
			vector.y = vector.y * (Links.x.canvasRT.sizeDelta.y / (float)Screen.height) - Links.x.canvasRT.sizeDelta.y / 2f;
			Links.x.itemPickupFX.localPosition = vector;
		}
		if (this.body)
		{
			Transform head = this.body.GetHead();
			if (head)
			{
				Vector3 vector2 = head.position;
				vector2 += new Vector3(this.xOffset, num, 0f);
				if (!Links.x.worldCamera.orthographic)
				{
					vector2.y += 0.3f;
				}
				if (this.body.character && this.body.character.stats.pinning)
				{
					vector2.y += 0.5f;
				}
				if (!this.menu)
				{
					if (!this.useWorldUI)
					{
						this.screenPos = camera.WorldToScreenPoint(vector2);
						if (this.screenPos.y / (float)Screen.height > 0.95f && !Links.x.worldCamera.orthographic)
						{
							this.screenPos.y = this.screenPos.y - 150f;
						}
						this.screenPos.z = 0f;
						this.screenPos.x = this.screenPos.x * (1920f / (float)Screen.width) - 960f;
						this.screenPos.y = this.screenPos.y * (Links.x.canvasRT.sizeDelta.y / (float)Screen.height) - Links.x.canvasRT.sizeDelta.y / 2f;
						this.rt.localPosition = this.screenPos;
					}
					else
					{
						this.tr.position = vector2;
						this.tr.LookAt(this.tr.position + this.camTr.rotation * this.forw, this.camTr.rotation * this.up);
					}
				}
			}
		}
		if (this.player)
		{
			if ((this.player.dead || this.player.missing || this.player.stunned) && !this.ending)
			{
				this.End();
			}
			if (Links.x.hasMain && this.player.indoorID != Links.x.main.indoorID)
			{
				this.ReturnToCellar();
			}
		}
		if (this.textWorld)
		{
			if (Links.x.characterSheet.open || Records.x.dialogue)
			{
				if (this.text)
				{
					Color color = this.text.color;
					if (!this.menu)
					{
						color.a = 0f;
						this.text.color = color;
						return;
					}
					color.a = this.opacity;
					this.text.color = color;
					return;
				}
				else if (this.textWorld)
				{
					Color color2 = this.textWorld.color;
					if (!this.menu)
					{
						color2.a = 0f;
						this.textWorld.color = color2;
						return;
					}
					color2.a = this.opacity;
					this.textWorld.color = color2;
					return;
				}
			}
			else
			{
				Color color3 = this.textWorld.color;
				if (Time.timeSinceLevelLoad > this.startTime + (0.8f + this.l * 0.05f))
				{
					this.opacity = Mathf.Lerp(this.opacity, 0f, Time.deltaTime * 2f);
				}
				color3.a = this.opacity;
				this.textWorld.color = color3;
			}
		}
	}

	// Token: 0x04001E8B RID: 7819
	public Character player;

	// Token: 0x04001E8C RID: 7820
	public Body body;

	// Token: 0x04001E8D RID: 7821
	public float yHeight;

	// Token: 0x04001E8E RID: 7822
	public float xOffset;

	// Token: 0x04001E8F RID: 7823
	private Transform tr;

	// Token: 0x04001E90 RID: 7824
	private Canvas canvas;

	// Token: 0x04001E91 RID: 7825
	public Image img;

	// Token: 0x04001E92 RID: 7826
	public Image img2;

	// Token: 0x04001E93 RID: 7827
	public RectTransform rt;

	// Token: 0x04001E94 RID: 7828
	public RectTransform textRT;

	// Token: 0x04001E95 RID: 7829
	private Vector3 originalScale;

	// Token: 0x04001E96 RID: 7830
	public TextMeshProUGUI text;

	// Token: 0x04001E97 RID: 7831
	public TextMeshPro textWorld;

	// Token: 0x04001E98 RID: 7832
	public Vector3 screenPos;

	// Token: 0x04001E99 RID: 7833
	public string txt;

	// Token: 0x04001E9A RID: 7834
	public TextMeshProTypewriterEffect typewriter;

	// Token: 0x04001E9B RID: 7835
	public bool menu;

	// Token: 0x04001E9C RID: 7836
	public bool useWorldUI;

	// Token: 0x04001E9D RID: 7837
	private Transform camTr;

	// Token: 0x04001E9E RID: 7838
	private Vector3 forw;

	// Token: 0x04001E9F RID: 7839
	private Vector3 up;

	// Token: 0x04001EA0 RID: 7840
	public bool emphasize;

	// Token: 0x04001EA1 RID: 7841
	private float startTime;

	// Token: 0x04001EA2 RID: 7842
	public float startGameTime;

	// Token: 0x04001EA3 RID: 7843
	private float opacity;

	// Token: 0x04001EA4 RID: 7844
	private float l;

	// Token: 0x04001EA5 RID: 7845
	public bool doAnimation;

	// Token: 0x04001EA6 RID: 7846
	public bool toCellar;

	// Token: 0x04001EA7 RID: 7847
	public Sprite sprite1;

	// Token: 0x04001EA8 RID: 7848
	public Sprite sprite2;

	// Token: 0x04001EA9 RID: 7849
	public Sprite sprite3;

	// Token: 0x04001EAA RID: 7850
	public Sprite sprite4;

	// Token: 0x04001EAB RID: 7851
	public Sprite sprite5;

	// Token: 0x04001EAC RID: 7852
	public Sprite sprite6;

	// Token: 0x04001EAD RID: 7853
	public Vector2 sprite1size;

	// Token: 0x04001EAE RID: 7854
	public Vector2 sprite2size;

	// Token: 0x04001EAF RID: 7855
	public Vector2 sprite3size;

	// Token: 0x04001EB0 RID: 7856
	public Vector2 sprite4size;

	// Token: 0x04001EB1 RID: 7857
	public Vector2 sprite5size;

	// Token: 0x04001EB2 RID: 7858
	public Vector2 sprite6size;

	// Token: 0x04001EB3 RID: 7859
	public float spriteSizeMultiplierX = 0.5f;

	// Token: 0x04001EB4 RID: 7860
	public float spriteSizeMultiplierY = 0.25f;

	// Token: 0x04001EB5 RID: 7861
	public float animTime;

	// Token: 0x04001EB6 RID: 7862
	public int currentAnimIndex;

	// Token: 0x04001EB7 RID: 7863
	public bool ending;

	// Token: 0x04001EB8 RID: 7864
	public Vector2 textSize;
}
