using System;
using RootMotion.FinalIK;
using UnityEngine;

// Token: 0x02000037 RID: 55
public class LookAtController : MonoBehaviour
{
	// Token: 0x060007DA RID: 2010 RVA: 0x000ADC8D File Offset: 0x000ABE8D
	private void Awake()
	{
		this.Setup();
	}

	// Token: 0x060007DB RID: 2011 RVA: 0x000ADC98 File Offset: 0x000ABE98
	private void Setup()
	{
		this.body = base.gameObject.GetComponent<Body>();
		this.lookIK = this.body.GetLookIK();
		if (!this.lookIK)
		{
			base.enabled = false;
		}
		if (!this.thisTarget && this.lookIK)
		{
			GameObject gameObject = new GameObject("Aim IK Target");
			this.thisTarget = gameObject.transform;
			if (Links.x.fellowship)
			{
				this.thisTarget.transform.SetParent(Links.x.fellowship.transform);
			}
		}
		if (this.lookIK)
		{
			this.lookIK.solver.IKPositionWeight = 0f;
		}
	}

	// Token: 0x060007DC RID: 2012 RVA: 0x000ADD5E File Offset: 0x000ABF5E
	private void Start()
	{
	}

	// Token: 0x060007DD RID: 2013 RVA: 0x000ADD60 File Offset: 0x000ABF60
	public void SetAsActive(bool state)
	{
		if (this.lookIK)
		{
			base.enabled = state;
			if (state)
			{
				return;
			}
			this.lookIK.enabled = false;
		}
	}

	// Token: 0x060007DE RID: 2014 RVA: 0x000ADD88 File Offset: 0x000ABF88
	public void EndLook()
	{
		this.target = null;
		this.hasTarget = false;
		if (this.body.headAnim)
		{
			if (this.body.character && (this.body.character.sleeping || this.body.character.stunned || this.body.character.downed))
			{
				return;
			}
			this.body.headAnim.PlayAnimation(FaceAnimations.AnimationNames.Blink);
		}
	}

	// Token: 0x060007DF RID: 2015 RVA: 0x000ADE10 File Offset: 0x000AC010
	public void EndLookFast(bool playFace)
	{
		this.target = null;
		this.hasTarget = false;
		if (this.body.headAnim && playFace)
		{
			if (this.body.character && (this.body.character.sleeping || this.body.character.stunned || this.body.character.downed))
			{
				return;
			}
			this.body.headAnim.PlayAnimation(FaceAnimations.AnimationNames.Blink);
		}
		if (this.lookIK)
		{
			this.lookIK.solver.IKPositionWeight = 0f;
		}
	}

	// Token: 0x060007E0 RID: 2016 RVA: 0x000ADEBC File Offset: 0x000AC0BC
	public void SetNewTargetTestForCloser(Transform newTarget, float targetTime, float newDelay, bool useHand)
	{
		if (!this.hasTarget || !this.target)
		{
			this.SetNewTarget(newTarget, targetTime, newDelay, false);
			return;
		}
		if ((newTarget.position - this.body.transform.position).sqrMagnitude < (this.target.position - this.body.transform.position).sqrMagnitude)
		{
			this.SetNewTarget(newTarget, targetTime, newDelay, false);
		}
	}

	// Token: 0x060007E1 RID: 2017 RVA: 0x000ADF40 File Offset: 0x000AC140
	public void SetNewTarget(Transform newTarget, float targetTime, float newDelay, bool useHand)
	{
		if (!this.lookIK)
		{
			return;
		}
		if (!this.body.character && !this.body.inMenu)
		{
			return;
		}
		if (newTarget == this.target && this.hasTarget && targetTime == this.targetTimer)
		{
			return;
		}
		this.target;
		if (Records.x.inMenus)
		{
			this.lastDialogueUpdateTime = Time.realtimeSinceStartup;
		}
		else
		{
			this.lastDialogueUpdateTime = Time.timeSinceLevelLoad;
		}
		if (this.body.character)
		{
			if (this.body.character.sleeping || this.body.character.stunned || this.body.character.downed)
			{
				return;
			}
			if (this.body.character.stats.animal)
			{
				return;
			}
		}
		this.SetAsActive(true);
		this.target = newTarget;
		this.speedTarget = 1f;
		this.speedWeight = 2f;
		this.speedFall = 2f;
		if (!this.body.inMenu)
		{
			if (newTarget == this.body.character.lookAtTurnTarget)
			{
				this.speedTarget = 2f;
				this.speedWeight = 2f;
				this.speedFall = 2f;
			}
		}
		else
		{
			if (Links.x.questEnd.enabled)
			{
				this.speedTarget = 5f;
			}
			else if (base.enabled)
			{
				this.speedTarget = 0.5f;
			}
			else
			{
				this.speedTarget = 1.5f;
			}
			this.speedFall = 0.5f;
		}
		this.lookIK.solver.target = this.thisTarget;
		this.lookIK.enabled = true;
		this.delay = newDelay;
		if (this.thisTarget && this.target && !this.hasTarget)
		{
			this.thisTarget.position = this.target.position;
			this.SetTargetYToHeadBone();
		}
		this.left = false;
		this.right = false;
		Vector3 vector = new Vector3(this.thisTarget.position.x, this.body.tr.position.y, this.thisTarget.position.z) - this.body.tr.position;
		if (this.AngleDir(this.body.tr.forward, Vector3.Normalize(vector), this.body.tr.up) < 0f)
		{
			this.left = true;
		}
		else
		{
			this.right = true;
		}
		if (this.left && this.body.headAnim)
		{
			this.body.headAnim.PlayAnimation(FaceAnimations.AnimationNames.LookLeftLoop);
		}
		if (this.right && this.body.headAnim)
		{
			this.body.headAnim.PlayAnimation(FaceAnimations.AnimationNames.LookRightLoop);
		}
		this.hasTarget = true;
		this.startTime = Time.timeSinceLevelLoad;
		if (targetTime > 0f)
		{
			this.targetTimer = targetTime;
			return;
		}
		this.targetTimer = 0f;
	}

	// Token: 0x060007E2 RID: 2018 RVA: 0x000AE278 File Offset: 0x000AC478
	public void SetTargetYToHeadBone()
	{
		Vector3 position = this.thisTarget.position;
		if (this.body.headBone)
		{
			this.thisTarget.position = new Vector3(position.x, this.body.headBone.position.y - 1f, position.z);
		}
	}

	// Token: 0x060007E3 RID: 2019 RVA: 0x000AE2DC File Offset: 0x000AC4DC
	private void Update()
	{
		float num = Time.deltaTime;
		if (Records.x.paused)
		{
			num = Time.unscaledDeltaTime;
			if (Records.x.filming)
			{
				num /= Records.x.filmingAnimatorSpeed;
			}
		}
		if (this.hasTarget)
		{
			if (!this.target)
			{
				this.hasTarget = false;
			}
			if (this.hasTarget)
			{
				this.dot = 0.5f;
				bool flag = false;
				if (Records.x.dialogue || Records.x.inMenus)
				{
					if ((this.body.gameObject.layer == 4 || this.body.gameObject.layer == 25) && Links.x.dialogue.npcTalking)
					{
						this.dot = 1f;
						if ((Records.x.dialogue && Time.timeSinceLevelLoad > this.lastDialogueUpdateTime + 0.08f) || (Records.x.inMenus && Time.realtimeSinceStartup > this.lastDialogueUpdateTime + 0.15f))
						{
							Vector3 vector = this.body.headBone.position + this.body.headBone.forward * 10f;
							if (Records.x.inMenus)
							{
								vector.y -= 1f;
							}
							if (this.body.headBone.position.y - this.body.tr.position.y < 2.7f)
							{
								vector.y += 2f;
							}
							else
							{
								vector.y -= 1f;
							}
							float num2 = -1f;
							float num3 = 0.5f;
							this.thisTarget.position = vector + new Vector3(Random.Range(num2, num3), Random.Range(num2, num3), Random.Range(num2, num3));
							Vector3 position = this.thisTarget.position;
							if (Mathf.Abs(position.x - vector.x) > 6f)
							{
								position.x = this.target.position.x;
							}
							if (Mathf.Abs(position.y - vector.y) > 6f)
							{
								position.y = this.target.position.y;
							}
							if (Mathf.Abs(position.z - vector.z) > 6f)
							{
								position.z = this.target.position.z;
							}
							this.thisTarget.position = position;
							if (Records.x.inMenus)
							{
								this.lastDialogueUpdateTime = Time.realtimeSinceStartup;
							}
							else
							{
								this.lastDialogueUpdateTime = Time.timeSinceLevelLoad;
							}
						}
						flag = true;
					}
					else if (this.body.gameObject.layer == 25 && !Links.x.dialogue.npcTalking)
					{
						Vector3 vector2 = this.body.headBone.position + this.body.headBone.forward * 10f;
						vector2.y -= 1f;
						if (this.body.headBone.position.y - this.body.tr.position.y < 2.7f)
						{
							vector2.y += 2f;
						}
						this.thisTarget.position = vector2;
					}
					if (Links.x.dialogue.noBobbling)
					{
						this.dot = -1f;
					}
				}
				if (this.body.character)
				{
					Vector3 vector3 = Vector3.Lerp(this.thisTarget.position, new Vector3(this.target.position.x, this.body.headBone.position.y, this.target.position.z), num * this.speedTarget);
					Vector3 forward = this.body.character.tr.forward;
					Vector3 vector4 = Vector3.Normalize(new Vector3(vector3.x, this.body.character.tr.position.y, vector3.z) - this.body.character.tr.position);
					this.dot = Vector3.Dot(forward, vector4);
					if (this.dot < -0.5f)
					{
						Vector3 vector5 = this.body.character.tr.right;
						vector3 += vector5 * 2f;
						vector4 = Vector3.Normalize(new Vector3(vector3.x, this.body.character.tr.position.y, vector3.z) - this.body.character.tr.position);
						this.dot = Vector3.Dot(forward, vector4);
						if (!flag)
						{
							this.thisTarget.position = vector3;
						}
					}
					else if (!flag)
					{
						this.thisTarget.position = vector3;
					}
					if ((this.body.character.animID != 0 && !Records.x.dialogue) || this.body.character.animator.GetBool("Combat"))
					{
						this.dot = -1f;
					}
					if (this.body.character.IsWalking())
					{
						this.dot = -1f;
					}
				}
				else if (!flag)
				{
					this.thisTarget.position = Vector3.Lerp(this.thisTarget.position, new Vector3(this.target.position.x, this.body.headBone.position.y, this.target.position.z), num * this.speedTarget);
				}
				if (!flag)
				{
					this.SetTargetYToHeadBone();
				}
				if (this.delay == 0f || Time.timeSinceLevelLoad > this.startTime + this.delay)
				{
					float num4 = 1f;
					if (this.dot < 0.25f)
					{
						num4 = this.lookIK.solver.IKPositionWeight;
					}
					if (this.dot == -1f)
					{
						num4 = 0f;
					}
					if (this.body.inMenu)
					{
						num4 *= 0.8f;
					}
					this.lookIK.solver.IKPositionWeight = Mathf.Lerp(this.lookIK.solver.IKPositionWeight, num4, num * this.speedWeight);
				}
				if (this.targetTimer > 0f && Time.timeSinceLevelLoad > this.startTime + this.targetTimer + this.delay)
				{
					this.hasTarget = false;
				}
			}
		}
		if (!this.hasTarget && this.lookIK.solver.IKPositionWeight > 0.01f)
		{
			this.lookIK.solver.IKPositionWeight = Mathf.Lerp(this.lookIK.solver.IKPositionWeight, 0f, num * this.speedFall);
			return;
		}
		if (!this.hasTarget)
		{
			this.SetAsActive(false);
		}
	}

	// Token: 0x060007E4 RID: 2020 RVA: 0x000AEA1C File Offset: 0x000ACC1C
	public float AngleDir(Vector3 fwd, Vector3 targetDir, Vector3 up)
	{
		float num = Vector3.Dot(Vector3.Cross(fwd, targetDir), up);
		if ((double)num > 0.0)
		{
			return 1f;
		}
		if ((double)num < 0.0)
		{
			return -1f;
		}
		return 0f;
	}

	// Token: 0x04000C3F RID: 3135
	private Body body;

	// Token: 0x04000C40 RID: 3136
	private LookAtIK lookIK;

	// Token: 0x04000C41 RID: 3137
	public Transform target;

	// Token: 0x04000C42 RID: 3138
	public bool hasTarget;

	// Token: 0x04000C43 RID: 3139
	public float targetTimer;

	// Token: 0x04000C44 RID: 3140
	private float startTime;

	// Token: 0x04000C45 RID: 3141
	public float delay;

	// Token: 0x04000C46 RID: 3142
	public Transform thisTarget;

	// Token: 0x04000C47 RID: 3143
	public Transform bodyTarget;

	// Token: 0x04000C48 RID: 3144
	private float speedTarget;

	// Token: 0x04000C49 RID: 3145
	private float speedWeight;

	// Token: 0x04000C4A RID: 3146
	private float speedFall;

	// Token: 0x04000C4B RID: 3147
	private bool left;

	// Token: 0x04000C4C RID: 3148
	private bool right;

	// Token: 0x04000C4D RID: 3149
	public float dot;

	// Token: 0x04000C4E RID: 3150
	private float lastDialogueUpdateTime;
}
