using System;
using System.Collections.Generic;
using DarkTonic.MasterAudio;
using UnityEngine;

// Token: 0x02000050 RID: 80
public class Stamina : MonoBehaviour
{
	// Token: 0x0600090F RID: 2319 RVA: 0x000C2704 File Offset: 0x000C0904
	public void Setup(Character c)
	{
		this.character = c;
		if (!this.tr)
		{
			this.tr = base.transform;
		}
		if (!this.camTr)
		{
			this.camTr = Links.x.worldCamera.gameObject.transform;
		}
		this.staminaSprites1 = Links.x.archives.staminaSprites1;
		this.staminaSprites2 = Links.x.archives.staminaSprites2;
		this.staminaRecovering = Links.x.archives.staminaRecover;
		this.staminaVulnerable = Links.x.archives.staminaVulnerable;
		Vector3 eulerAngles = Quaternion.LookRotation(this.character.currentPosition - this.camTr.position).eulerAngles;
		eulerAngles.x = 0f;
		eulerAngles.z = 0f;
		this.tr.rotation = Quaternion.Euler(eulerAngles);
		this.UpdateStamina();
	}

	// Token: 0x06000910 RID: 2320 RVA: 0x000C2804 File Offset: 0x000C0A04
	public void SetupArmor(Character c, int max, int curr)
	{
		this.str = new StringFast(32);
		this.character = c;
		this.currentArmor = curr;
		this.maxArmor = max;
		this.armorSprites = Links.x.archives.armorSprites;
		this.armorSpritesBreak = Links.x.archives.armorSpritesBreak;
		this.armorSpritesVulnerable = Links.x.archives.armorVulnerable;
		this.armorSpritesBuffed = Links.x.archives.armorBuffed;
		if (this.character.stats.xmlName.Contains("Briss"))
		{
			this.armorYAdd = 0.3f;
		}
		for (int i = 0; i < 1; i++)
		{
			GameObject gameObject = new GameObject("Armor");
			Transform transform = gameObject.transform;
			transform.SetParent(this.character.body.GetHip());
			this.armorPositions[0] = new Vector3(0f, 0.25f, 0f);
			transform.localPosition = this.armorPositions[0];
			transform.localScale = Vector3.one * 0.7f;
			gameObject.layer = 3;
			SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
			spriteRenderer.sprite = this.armorSprites[6];
			spriteRenderer.material = Links.x.spriteEffectMaterial;
			this.armors.Add(spriteRenderer);
			this.spriteIndexArmor.Add(0);
		}
		for (int j = 0; j < 7; j++)
		{
			this.armorState.Add(0);
		}
		this.hasArmor = true;
		this.UpdateArmor(curr);
		this.LateUpdate();
	}

	// Token: 0x06000911 RID: 2321 RVA: 0x000C299C File Offset: 0x000C0B9C
	public void UpdateArmor(int curr)
	{
		if (this.hasArmor)
		{
			SpriteRenderer spriteRenderer = null;
			this.currentArmor = curr;
			float num = 7f;
			float num2 = (float)this.maxArmor / num;
			float num3 = (float)curr;
			int num4 = 0;
			for (int i = 0; i < 7; i++)
			{
				float num5 = (float)i * num2;
				if (num3 > num5)
				{
					num4++;
				}
				else if (this.armorState[i] == 0)
				{
					this.updateTime = Time.timeSinceLevelLoad;
					spriteRenderer = this.armors[0];
					this.spriteIndexArmor[0] = -1;
					this.armors[0].sprite = this.armorSpritesBreak[0];
				}
			}
			if (num4 == 0)
			{
				this.armorState[0] = 1;
			}
			else
			{
				this.armors[0].sprite = this.armorSprites[num4 - 1];
			}
			if (spriteRenderer)
			{
				MasterAudio.PlaySound3DAtVector3AndForget("Armor Break", this.character.torsoPosition, 0.75f, new float?(1f), 0f, "", null);
				RectTransform hannahAnimator = Links.x.cellar.GetHannahAnimator(1);
				hannahAnimator.gameObject.SetActive(true);
				hannahAnimator.gameObject.GetComponent<HannahAnimator>().Play("Idle");
				Vector3 vector = Links.x.worldCamera.WorldToScreenPoint(spriteRenderer.transform.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;
				hannahAnimator.localPosition = vector;
				GameObject pooledGameObject = Links.x.cellar.GetPooledGameObject(119);
				pooledGameObject.transform.position = this.character.torsoPosition;
				pooledGameObject.SetActive(true);
				this.str.Clear();
				this.str.Append(this.character.stats.GetName());
				this.str.Append("'s armor is damaged");
				Links.x.gameFeed.AddFeed(this.str.ToString());
			}
		}
	}

	// Token: 0x06000912 RID: 2322 RVA: 0x000C2C00 File Offset: 0x000C0E00
	public void DebuffArmor()
	{
		if (!this.armorDebuffed)
		{
			GameObject gameObject = new GameObject("Armor Debuff");
			Transform transform = gameObject.transform;
			transform.SetParent(this.armors[0].transform);
			transform.localPosition = new Vector3(0f, 0f, -0.05f);
			transform.localScale = Vector3.one * 1f;
			transform.localRotation = Quaternion.Euler(Vector3.zero);
			gameObject.layer = 3;
			SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
			this.debuffIndex = 0;
			spriteRenderer.sprite = this.armorSpritesVulnerable[0];
			spriteRenderer.material = Links.x.spriteEffectMaterial;
			this.armorDebuffSprite = spriteRenderer;
			this.armorDebuffed = true;
		}
	}

	// Token: 0x06000913 RID: 2323 RVA: 0x000C2CBD File Offset: 0x000C0EBD
	public void RemoveArmorDebuff()
	{
		this.armorDebuffed = false;
		if (this.armorDebuffSprite)
		{
			Object.Destroy(this.armorDebuffSprite.gameObject);
		}
	}

	// Token: 0x06000914 RID: 2324 RVA: 0x000C2CE4 File Offset: 0x000C0EE4
	public void BuffArmor()
	{
		if (!this.armorBuffed)
		{
			GameObject gameObject = new GameObject("Armor Buff");
			Transform transform = gameObject.transform;
			transform.SetParent(this.armors[0].transform);
			transform.localPosition = new Vector3(0f, 0f, -0.07f);
			transform.localScale = Vector3.one * 1f;
			transform.localRotation = Quaternion.Euler(Vector3.zero);
			gameObject.layer = 3;
			SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
			this.debuffIndex = 0;
			spriteRenderer.sprite = this.armorSpritesBuffed[0];
			spriteRenderer.material = Links.x.spriteEffectMaterial;
			this.armorBuffSprite = spriteRenderer;
			this.armorBuffed = true;
		}
	}

	// Token: 0x06000915 RID: 2325 RVA: 0x000C2DA1 File Offset: 0x000C0FA1
	public void RemoveArmorBuff()
	{
		this.armorBuffed = false;
		if (this.armorBuffSprite)
		{
			Object.Destroy(this.armorBuffSprite.gameObject);
		}
	}

	// Token: 0x06000916 RID: 2326 RVA: 0x000C2DC8 File Offset: 0x000C0FC8
	public void Deactivate()
	{
		base.gameObject.SetActive(false);
		if (this.armors.Count > 0 && this.armors[0])
		{
			this.armors[0].gameObject.SetActive(false);
		}
	}

	// Token: 0x06000917 RID: 2327 RVA: 0x000C2E1C File Offset: 0x000C101C
	public void Activate()
	{
		base.gameObject.SetActive(true);
		if (this.armors.Count > 0 && this.armors[0])
		{
			this.armors[0].gameObject.SetActive(true);
		}
	}

	// Token: 0x06000918 RID: 2328 RVA: 0x000C2E70 File Offset: 0x000C1070
	public void DebuffStamina()
	{
		if (!this.staminaDebuffed)
		{
			GameObject gameObject = new GameObject("Stamina Debuff");
			Transform transform = gameObject.transform;
			transform.SetParent(base.gameObject.transform);
			transform.localPosition = this.debuffPosition;
			transform.localScale = Vector3.one * this.normalScale;
			gameObject.layer = 3;
			SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
			this.debuffIndex = 0;
			spriteRenderer.sprite = this.staminaVulnerable[0];
			spriteRenderer.material = Links.x.spriteEffectMaterial;
			this.staminaDebuffSprite = spriteRenderer;
			this.staminaDebuffed = true;
			this.timeSinceAddedStaminaDebuff = Time.timeSinceLevelLoad;
			MasterAudio.PlaySound3DAtVector3AndForget("Paper", this.character.torsoPosition, 1f, new float?(1f), 0f, "", null);
		}
	}

	// Token: 0x06000919 RID: 2329 RVA: 0x000C2F4D File Offset: 0x000C114D
	public void RemoveStaminaDebuff()
	{
		this.staminaDebuffed = false;
		if (this.staminaDebuffSprite)
		{
			Object.Destroy(this.staminaDebuffSprite.gameObject);
		}
	}

	// Token: 0x0600091A RID: 2330 RVA: 0x000C2F74 File Offset: 0x000C1174
	public void UpdateStamina()
	{
		if (this.staminas.Count == 0)
		{
			this.updateTime = Time.timeSinceLevelLoad;
		}
		float num = this.character.stats.StaminaPercent();
		int num2 = 0;
		if (num < 0.166f)
		{
			num2 = 5;
		}
		if (num >= 0.166f && num < 0.33f)
		{
			num2 = 4;
		}
		if (num >= 0.33f && num < 0.5f)
		{
			num2 = 3;
		}
		if (num >= 0.5f && num < 0.667f)
		{
			num2 = 2;
		}
		if ((double)num >= 0.667 && num < 0.883f)
		{
			num2 = 1;
		}
		if (this.staminas.Count != num2)
		{
			if (num2 > this.staminas.Count)
			{
				int num3 = num2 - this.staminas.Count;
				for (int i = 0; i < num3; i++)
				{
					int count = this.staminas.Count;
					GameObject gameObject = new GameObject("Stamina");
					Transform transform = gameObject.transform;
					transform.SetParent(base.gameObject.transform);
					transform.localPosition = this.positions[count];
					transform.localScale = Vector3.one * this.normalScale;
					gameObject.layer = 3;
					SpriteRenderer spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
					int num4 = 0;
					if (count == 1 || count == 3)
					{
						num4 = 1;
					}
					if (num4 == 0)
					{
						spriteRenderer.sprite = this.staminaSprites1[0];
					}
					else
					{
						spriteRenderer.sprite = this.staminaSprites2[1];
					}
					if (count == 1 || count == 3)
					{
						spriteRenderer.flipX = true;
					}
					spriteRenderer.material = Links.x.spriteEffectMaterial;
					this.staminas.Add(spriteRenderer);
					this.spriteIndex.Add(0);
					this.spriteVariation.Add(num4);
					this.indexDirection.Add(0);
					this.recovering.Add(0);
					this.timeSinceAdded.Add(Time.timeSinceLevelLoad);
				}
				return;
			}
			int num5 = this.staminas.Count - num2;
			int num6 = 0;
			for (int j = 0; j < this.staminas.Count; j++)
			{
				if (num6 < num5 && this.recovering[j] == 0)
				{
					num6++;
					this.recovering[j] = 1;
					this.spriteIndex[j] = 0;
					this.staminas[j].sprite = this.staminaRecovering[0];
					this.staminas[j].transform.localScale = Vector3.one * 1.75f;
				}
			}
		}
	}

	// Token: 0x0600091B RID: 2331 RVA: 0x000C31F8 File Offset: 0x000C13F8
	public void End()
	{
		for (int i = 0; i < this.staminas.Count; i++)
		{
			if (this.staminas[i])
			{
				Object.Destroy(this.staminas[i].gameObject);
			}
		}
		for (int j = 0; j < this.armors.Count; j++)
		{
			if (this.armors[j])
			{
				Object.Destroy(this.armors[j].gameObject);
			}
		}
		if (this.staminaDebuffSprite)
		{
			Object.Destroy(this.staminaDebuffSprite.gameObject);
		}
		Object.Destroy(this);
	}

	// Token: 0x0600091C RID: 2332 RVA: 0x000C32A8 File Offset: 0x000C14A8
	private void LateUpdate()
	{
		if (this.character)
		{
			Vector3 eulerAngles = Quaternion.LookRotation(this.character.currentPosition - this.camTr.position).eulerAngles;
			eulerAngles.x = 0f;
			eulerAngles.z = 0f;
			Vector3 eulerAngles2 = Quaternion.LookRotation(this.camTr.position - this.character.currentPosition).eulerAngles;
			eulerAngles2.x = 0f;
			eulerAngles2.z = 0f;
			this.tr.rotation = Quaternion.Euler(eulerAngles);
			float num = 0.1f;
			if (this.character.neck == this.character.body.tr)
			{
				this.tr.position = this.character.body.headBone.position + new Vector3(0f, num, 0f);
			}
			else
			{
				this.tr.position = this.character.neck.position + new Vector3(0f, num, 0f);
			}
			this.tr.position += Quaternion.Euler(eulerAngles2) * Vector3.forward * 0.25f;
			if (Time.timeSinceLevelLoad > this.updateTime + this.updateSpeed)
			{
				this.updateTime = Time.timeSinceLevelLoad;
				bool flag = false;
				for (int i = 0; i < this.staminas.Count; i++)
				{
					if (Random.Range(0, 3) == 0)
					{
						float num2 = 0.5f;
						this.staminas[i].transform.localPosition += new Vector3(Random.Range(-0.1f, 0.1f) * num2, Random.Range(-0.1f, 0.1f) * num2, Random.Range(-0.1f, 0.1f) * num2);
						if (Vector3.Distance(this.staminas[i].transform.localPosition, this.positions[i]) > 0.06f)
						{
							this.staminas[i].transform.localPosition = this.positions[i];
						}
						this.staminas[i].transform.localRotation = Quaternion.Euler(new Vector3(Random.Range(-5f, 5f), Random.Range(-5f, 5f), Random.Range(-5f, 5f)));
					}
					int num3 = this.spriteIndex[i];
					if (this.recovering[i] == 1)
					{
						num3++;
						if (num3 > 12)
						{
							flag = true;
							Object.Destroy(this.staminas[i].gameObject);
							this.staminas[i] = null;
							this.spriteIndex[i] = -2;
							this.spriteVariation[i] = -1;
							this.indexDirection[i] = -1;
							this.recovering[i] = -1;
							this.timeSinceAdded[i] = -1f;
						}
						else
						{
							this.staminas[i].sprite = this.staminaRecovering[num3];
							this.spriteIndex[i] = num3;
						}
					}
					else
					{
						if (this.indexDirection[i] == 0)
						{
							num3++;
							if (num3 > 8)
							{
								num3 = 7;
								this.indexDirection[i] = 1;
							}
						}
						else
						{
							num3--;
							if (num3 < 0)
							{
								num3 = 1;
								this.indexDirection[i] = 0;
							}
						}
						if (this.spriteVariation[i] == 0)
						{
							this.staminas[i].sprite = this.staminaSprites1[num3];
						}
						else
						{
							this.staminas[i].sprite = this.staminaSprites2[num3];
						}
						this.spriteIndex[i] = num3;
						if (this.timeSinceAdded[i] > 0f && Time.timeSinceLevelLoad > this.timeSinceAdded[i] + 2f)
						{
							float num4 = this.timeSinceAdded[i] + 3f - Time.timeSinceLevelLoad;
							if (num4 < 0f)
							{
								this.staminas[i].transform.localScale = Vector3.one * this.smallScale;
								this.timeSinceAdded[i] = 0f;
							}
							else
							{
								this.staminas[i].transform.localScale = Mathf.Lerp(this.normalScale, this.smallScale, 1f - num4) * Vector3.one;
							}
						}
					}
				}
				if (this.staminaDebuffed)
				{
					int num5 = this.debuffIndex;
					num5++;
					if (num5 > 8)
					{
						num5 = 0;
					}
					this.staminaDebuffSprite.sprite = this.staminaVulnerable[num5];
					this.debuffIndex = num5;
					this.staminaDebuffSprite.transform.localRotation = Quaternion.Euler(new Vector3(Random.Range(-5f, 5f), Random.Range(-5f, 5f), Random.Range(-5f, 5f)));
					if (this.timeSinceAddedStaminaDebuff > 0f && Time.timeSinceLevelLoad > this.timeSinceAddedStaminaDebuff + 2f)
					{
						float num6 = this.timeSinceAddedStaminaDebuff + 3f - Time.timeSinceLevelLoad;
						if (num6 < 0f)
						{
							this.staminaDebuffSprite.transform.localScale = Vector3.one * this.smallScale;
							this.timeSinceAddedStaminaDebuff = 0f;
						}
						else
						{
							this.staminaDebuffSprite.transform.localScale = Mathf.Lerp(this.normalScale, this.smallScale, 1f - num6) * Vector3.one;
						}
					}
				}
				if (flag)
				{
					this.staminas.RemoveAll((SpriteRenderer item) => item == null);
					this.spriteIndex.RemoveAll((int item) => item == -2);
					this.spriteVariation.RemoveAll((int item) => item == -1);
					this.indexDirection.RemoveAll((int item) => item == -1);
					this.recovering.RemoveAll((int item) => item == -1);
					this.timeSinceAdded.RemoveAll((float item) => item == -1f);
				}
				for (int j = 0; j < this.armors.Count; j++)
				{
					if (this.armorState[j] > -1)
					{
						Vector3 vector = Vector3.zero;
						Vector3 zero = Vector3.zero;
						if (Random.Range(0, 3) == 0)
						{
							float num7 = 0.5f;
							vector = new Vector3(Random.Range(-0.1f, 0.1f) * num7, Random.Range(-0.1f, 0.1f) * num7, Random.Range(-0.1f, 0.1f) * num7);
							if (Vector3.Distance(vector, this.armorPositions[j]) > 0.06f)
							{
								vector = Vector3.zero;
							}
							zero = new Vector3(Random.Range(-5f, 5f), Random.Range(-5f, 5f), Random.Range(-5f, 5f));
						}
						Vector3 vector2 = this.armors[j].transform.parent.position + this.armorPositions[j] + vector;
						Quaternion quaternion = Quaternion.Euler(eulerAngles2);
						vector2 += quaternion * Vector3.forward * 0.7f;
						vector2.y += this.armorYAdd;
						this.armors[j].transform.position = vector2;
						this.armors[j].transform.rotation = Quaternion.Euler(eulerAngles + zero);
						int num8 = this.spriteIndexArmor[j];
						num8++;
						if (this.armorState[j] == 1 && !Records.x.removeControls)
						{
							if (num8 > 8)
							{
								Object.Destroy(this.armors[j].gameObject);
								this.armors.Clear();
								this.spriteIndexArmor.Clear();
								this.armorState.Clear();
								this.hasArmor = false;
							}
							else
							{
								this.armors[j].sprite = this.armorSpritesBreak[num8];
								this.spriteIndexArmor[j] = num8;
							}
						}
					}
				}
			}
		}
	}

	// Token: 0x04000E3B RID: 3643
	private Transform tr;

	// Token: 0x04000E3C RID: 3644
	private Transform camTr;

	// Token: 0x04000E3D RID: 3645
	private Vector3 forw;

	// Token: 0x04000E3E RID: 3646
	private Vector3 up;

	// Token: 0x04000E3F RID: 3647
	public Character character;

	// Token: 0x04000E40 RID: 3648
	public float updateSpeed = 0.07f;

	// Token: 0x04000E41 RID: 3649
	[Header("Stamina")]
	public Vector3 debuffPosition = new Vector3(0.5f, 0f, 0.2f);

	// Token: 0x04000E42 RID: 3650
	public List<SpriteRenderer> staminas = new List<SpriteRenderer>();

	// Token: 0x04000E43 RID: 3651
	public List<int> spriteIndex = new List<int>();

	// Token: 0x04000E44 RID: 3652
	public List<int> spriteVariation = new List<int>();

	// Token: 0x04000E45 RID: 3653
	public List<int> indexDirection = new List<int>();

	// Token: 0x04000E46 RID: 3654
	public List<int> recovering = new List<int>();

	// Token: 0x04000E47 RID: 3655
	public List<float> timeSinceAdded = new List<float>();

	// Token: 0x04000E48 RID: 3656
	private float smallScale = 1f;

	// Token: 0x04000E49 RID: 3657
	private float normalScale = 1.5f;

	// Token: 0x04000E4A RID: 3658
	private float updateTime;

	// Token: 0x04000E4B RID: 3659
	private Sprite[] staminaSprites1;

	// Token: 0x04000E4C RID: 3660
	private Sprite[] staminaSprites2;

	// Token: 0x04000E4D RID: 3661
	private Sprite[] staminaVulnerable;

	// Token: 0x04000E4E RID: 3662
	private Sprite[] staminaRecovering;

	// Token: 0x04000E4F RID: 3663
	public bool staminaDebuffed;

	// Token: 0x04000E50 RID: 3664
	private float timeSinceAddedStaminaDebuff;

	// Token: 0x04000E51 RID: 3665
	public SpriteRenderer staminaDebuffSprite;

	// Token: 0x04000E52 RID: 3666
	public int debuffIndex;

	// Token: 0x04000E53 RID: 3667
	public Vector3[] positions = new Vector3[]
	{
		new Vector3(0.3f, 0f, 0.5f),
		new Vector3(0.4f, 0f, 0.3f),
		new Vector3(0.4f, 0f, 0f),
		new Vector3(0.1f, 0f, 0.1f),
		new Vector3(0.2f, 0f, -0.3f)
	};

	// Token: 0x04000E54 RID: 3668
	[Header("Armor")]
	public int maxArmor;

	// Token: 0x04000E55 RID: 3669
	public int currentArmor;

	// Token: 0x04000E56 RID: 3670
	public bool hasArmor;

	// Token: 0x04000E57 RID: 3671
	public List<SpriteRenderer> armors = new List<SpriteRenderer>();

	// Token: 0x04000E58 RID: 3672
	public List<int> spriteIndexArmor = new List<int>();

	// Token: 0x04000E59 RID: 3673
	public List<int> armorState = new List<int>();

	// Token: 0x04000E5A RID: 3674
	private StringFast str;

	// Token: 0x04000E5B RID: 3675
	private Sprite[] armorSprites;

	// Token: 0x04000E5C RID: 3676
	private Sprite[] armorSpritesBreak;

	// Token: 0x04000E5D RID: 3677
	private Sprite[] armorSpritesVulnerable;

	// Token: 0x04000E5E RID: 3678
	private Sprite[] armorSpritesBuffed;

	// Token: 0x04000E5F RID: 3679
	public Vector3[] armorPositions = new Vector3[]
	{
		new Vector3(0.7f, 1f, 0.5f),
		new Vector3(-0.6f, 1f, 0.9f),
		new Vector3(0.8f, 1f, 0f),
		new Vector3(0.4f, 1f, -0.8f),
		new Vector3(-0.9f, 1f, -0.3f)
	};

	// Token: 0x04000E60 RID: 3680
	public bool armorDebuffed;

	// Token: 0x04000E61 RID: 3681
	public bool armorBuffed;

	// Token: 0x04000E62 RID: 3682
	public SpriteRenderer armorDebuffSprite;

	// Token: 0x04000E63 RID: 3683
	public SpriteRenderer armorBuffSprite;

	// Token: 0x04000E64 RID: 3684
	private float armorYAdd;
}
