using System;
using UnityEngine;
using UnityEngine.UI;

// Token: 0x020000DE RID: 222
public class TimelineIcon : MonoBehaviour
{
	// Token: 0x060014AD RID: 5293 RVA: 0x001902D1 File Offset: 0x0018E4D1
	private void Awake()
	{
		this.tr = base.transform;
		this.SetAsActive(false);
		this.speed = 0.5f;
	}

	// Token: 0x060014AE RID: 5294 RVA: 0x001902F1 File Offset: 0x0018E4F1
	public void Hover(bool state)
	{
		if (state)
		{
			this.portrait.color = this.hoverColor;
			return;
		}
		this.portrait.color = this.normalColor;
	}

	// Token: 0x060014AF RID: 5295 RVA: 0x0019031C File Offset: 0x0018E51C
	public void Setup(Character newCharacter, int index, bool setIconAtBeginning)
	{
		this.character = newCharacter;
		this.offsetTime = 0f;
		if (this.character.npc)
		{
			this.start = Links.x.hudControl.startEnemy;
			this.end = Links.x.hudControl.endEnemy;
			this.action = Links.x.hudControl.actionEnemy;
		}
		else
		{
			this.start = Links.x.hudControl.startParty;
			this.end = Links.x.hudControl.endParty;
			this.action = Links.x.hudControl.actionParty;
		}
		Vector3.Distance(this.start.position, this.end.position);
		this.t = Random.Range(0f, 0.75f);
		if (!Records.x.turnBased)
		{
			this.t = 0f;
			this.updateTime = 0f;
		}
		this.tr.position = Vector3.Lerp(this.start.position, this.end.position, this.t);
		bool flag = false;
		if (Records.x.editor && flag)
		{
			Links.x.renderPortraits.AddCharacter(this.character, this.character.stats.xmlName, null, true, this, null, null, null);
			if (this.portrait && this.texturesIdle != null)
			{
				this.portrait.texture = this.texturesIdle;
			}
		}
		this.SetAsActive(true);
		this.Hover(false);
		this.character.timelineIcon = this;
		if (this.character == Links.x.combat.characterStartingAttack)
		{
			this.tr.position = this.end.position;
		}
		if (!Records.x.turnBased && !this.character.moving && setIconAtBeginning)
		{
			this.Beginning();
			return;
		}
		if (!Records.x.turnBased && !setIconAtBeginning)
		{
			this.waitingToPickAction = true;
		}
	}

	// Token: 0x060014B0 RID: 5296 RVA: 0x00190538 File Offset: 0x0018E738
	public void SetAsActive(bool state)
	{
		Image[] array = this.images;
		for (int i = 0; i < array.Length; i++)
		{
			array[i].enabled = state;
		}
		this.portrait.enabled = state;
		base.enabled = state;
	}

	// Token: 0x060014B1 RID: 5297 RVA: 0x00190578 File Offset: 0x0018E778
	public void End()
	{
		if (!this.character)
		{
			return;
		}
		this.SetAsActive(false);
		if (this.character.npc)
		{
			Links.x.cellar.ReturnTimelineIconEnemy(this);
		}
		else
		{
			Links.x.cellar.ReturnTimelineIcon(this);
		}
		this.character = null;
		this.animator.Stop();
		this.sheen.enabled = false;
		Links.x.combat.RemoveTimelineIcon(this);
	}

	// Token: 0x060014B2 RID: 5298 RVA: 0x001905F8 File Offset: 0x0018E7F8
	private void Update()
	{
		if (!this.character.hasActions)
		{
			return;
		}
		if (((!this.character.turning && !this.character.stats.pinned && !this.knockedOut && !this.downed && !this.character.downed && !this.character.body.IsHit() && !Links.x.combat.combatCinematics && !this.waitingForAction && !this.waitingToPickAction && !this.character.moving && this.character.waitingForPath == 0 && this.character.NodesCount() == 0 && !this.character.actions.ai.findingAction && !Records.x.paused && !this.character.isHit && !Records.x.dialogue && !this.character.actions.cannotInterrupt && !this.character.inQueuedAbility) || (this.character.mainSelected && this.overridePause && !this.character.body.animationHold)) && (!Records.x.pocketPause || (Records.x.pocketPause && ((this.character.mainSelected && this.overridePause && !this.character.body.animationHold) || this.character == Links.x.combat.rallyCharacter))))
		{
			if (Records.x.turnBased)
			{
				this.t += Time.deltaTime / this.speed;
				this.tr.position = Vector3.Lerp(this.start.position, this.end.position, this.t);
				if (this.tr.position == this.end.position)
				{
					if (Records.x.turnBased)
					{
						this.PickAction();
					}
					else
					{
						this.DoAction();
					}
					this.waitingForAction = true;
				}
			}
			else
			{
				this.doingRepath = false;
				this.updateTime += Time.deltaTime;
				if (this.updateTime > 0.25f)
				{
					this.updateTime = 0f;
					if (this.character.hasActions)
					{
						this.doingRepath = this.character.actions.CheckForRepathWhileWaitingToAttack();
					}
				}
				if (this.hasAction)
				{
					if (!this.doingRepath)
					{
						if (this.doingAction)
						{
							this.t += Time.deltaTime / this.speed;
							if (this.character.ActionAnimID() && this.needsSpeed && this.character.animator)
							{
								this.clipInfo = this.character.animator.GetCurrentAnimatorClipInfo(0);
								if (this.clipInfo.Length != 0)
								{
									this.clip = this.clipInfo[0].clip;
									if (this.clip)
									{
										this.clipInfoName = this.clip.name;
										this.clipLength = this.clip.length;
										this.events = this.clipInfo[0].clip.events;
										if (this.events != null)
										{
											for (int i = 0; i < this.events.Length; i++)
											{
												this.stateInfo = this.character.animator.GetCurrentAnimatorStateInfo(0);
												if (this.events[i].functionName.Contains("ttack") || this.events[i].functionName.Contains("ast") || this.events[i].functionName.Contains("inket"))
												{
													this.speed = this.clipLength;
													if (this.character.mainSelected && this.character.stats.HandCount() > 1f)
													{
														this.speed += 0.1f;
													}
													this.needsSpeed = false;
													this.stateInfo = this.character.animator.GetCurrentAnimatorStateInfo(0);
													this.currentHash = this.stateInfo.shortNameHash;
													this.character.body.recoveryTime = this.clipLength - this.events[i].time;
												}
											}
										}
									}
								}
							}
							this.tr.position = Vector3.Lerp(this.action.position, this.end.position, this.t);
							if (this.tr.position == this.end.position)
							{
								this.offsetTime = 0f;
								this.overridePause = false;
								if (this.character.hasActions)
								{
									if (this.character.IsAttacking(this.currentHash))
									{
										this.character.actions.EndAttackTime(0f);
									}
									else if (this.character.IsCasting(this.currentHash) || this.casting)
									{
										if (this.character.animID == 14)
										{
											this.character.animID = 0;
										}
									}
									else if (!this.character.IsTrinketing(this.currentHash))
									{
										if (!Links.x.combat.miniEvent && Records.x.editor && this.currentHash != 0)
										{
											string[] array = new string[9];
											array[0] = "ERROR ";
											int num = 1;
											Character character = this.character;
											array[num] = ((character != null) ? character.ToString() : null);
											array[2] = "'s clip ";
											array[3] = this.clipInfoName;
											array[4] = " ";
											array[5] = this.character.animID.ToString();
											array[6] = " ";
											array[7] = this.currentHash.ToString();
											array[8] = " did not trigger end of action";
											Debug.Log(string.Concat(array));
										}
										if (this.character.actions && !Links.x.combat.miniEvent && Records.x.editor && this.currentHash != 0)
										{
											string text = "Last action for ";
											Character character2 = this.character;
											Debug.Log(text + ((character2 != null) ? character2.ToString() : null) + " :" + this.character.actions.ai.lastAction);
										}
									}
									if (this.character && this.character.actions)
									{
										this.character.actions.EndTurnAfterAction();
									}
								}
							}
						}
						else
						{
							this.t += Time.deltaTime / this.speed;
							this.tr.position = Vector3.Lerp(this.start.position, this.action.position, this.t);
							if (this.tr.position == this.action.position)
							{
								if (this.character.hasActions)
								{
									this.updateTime += Time.deltaTime;
									if (this.updateTime > 0.25f)
									{
										this.updateTime = 0f;
										if (this.character.hasActions)
										{
											this.doingRepath = this.character.actions.CheckForRepathWhileWaitingToAttack();
										}
									}
								}
								if (!this.doingRepath)
								{
									this.needsSpeed = true;
									this.doingAction = true;
									this.speed = 1f;
									this.t = 0f;
									this.DoAction();
								}
							}
						}
					}
				}
				else if (this.character && this.character.actions && this.speed > 0f)
				{
					this.t += Time.deltaTime / this.speed;
					this.tr.position = Vector3.Lerp(this.start.position, this.action.position, this.t);
					if (this.tr.position == this.action.position)
					{
						this.DoAction();
					}
				}
			}
		}
		if (this.character)
		{
			if (this.character.actions.castingSpell && this.character.actions.spellCasterFX)
			{
				this.character.actions.spellCasterFX.transform.position = this.character.tr.position + new Vector3(0f, 0.2f, 0f);
			}
			if (this.character.dead || !this.character.hasActions || this.character.stunned)
			{
				this.End();
			}
		}
	}

	// Token: 0x060014B3 RID: 5299 RVA: 0x00190F28 File Offset: 0x0018F128
	public void AddSecondAttack()
	{
		if (this.character && this.character.animator)
		{
			this.speed = 0f;
			this.clipInfo = this.character.animator.GetCurrentAnimatorClipInfo(0);
			if (this.clipInfo.Length != 0)
			{
				this.clip = this.clipInfo[0].clip;
				if (this.clip)
				{
					this.events = this.clipInfo[0].clip.events;
					if (this.events != null)
					{
						for (int i = 0; i < this.events.Length; i++)
						{
							this.stateInfo = this.character.animator.GetCurrentAnimatorStateInfo(0);
							if (this.events[i].functionName.Contains("ttack") || this.events[i].functionName.Contains("ast") || this.events[i].functionName.Contains("inket"))
							{
								this.clipInfoName = this.clip.name;
								this.clipLength = this.events[i].time + 0.2f;
								this.speed = this.clipLength;
							}
						}
					}
				}
			}
			this.tr.position = this.action.position;
			if (this.speed < 0.01f)
			{
				this.speed = 1.5f;
			}
			this.t = 0f;
		}
	}

	// Token: 0x060014B4 RID: 5300 RVA: 0x001910C0 File Offset: 0x0018F2C0
	public void DoAction()
	{
		if (this.character.actions)
		{
			if (this.character.actions.cannotInterrupt)
			{
				this.character.actions.EndTurnAfterAction();
				this.Beginning();
				return;
			}
			if (this.hasAction)
			{
				this.character.actions.ContinueAction();
				return;
			}
			this.Beginning();
		}
	}

	// Token: 0x060014B5 RID: 5301 RVA: 0x00191128 File Offset: 0x0018F328
	public void PickAction()
	{
		if (Records.x.combat)
		{
			Links.x.combat.GetNextAction(this.character);
			return;
		}
		if (this.character.actions)
		{
			this.character.actions.ChooseAction();
			return;
		}
		this.End();
	}

	// Token: 0x060014B6 RID: 5302 RVA: 0x00191180 File Offset: 0x0018F380
	public void RecordOffset()
	{
		if (this.character.npc)
		{
			this.offsetTime = 1f - this.t;
		}
	}

	// Token: 0x060014B7 RID: 5303 RVA: 0x001911A4 File Offset: 0x0018F3A4
	public void Clear()
	{
		this.tr.position = this.start.position;
		this.speed = 0f;
		this.casting = false;
		this.doingRepath = false;
		this.doingAction = false;
		this.waitingForAction = false;
		this.hasAction = false;
		this.waitingToPickAction = true;
		this.overridePause = false;
	}

	// Token: 0x060014B8 RID: 5304 RVA: 0x00191204 File Offset: 0x0018F404
	public void SetAtBeginningPosition()
	{
		this.waitingForAction = false;
		this.casting = false;
		this.tr.position = this.start.position;
		this.hasAction = false;
		if (!Records.x.turnBased)
		{
			this.waitingToPickAction = true;
			this.PickAction();
		}
	}

	// Token: 0x060014B9 RID: 5305 RVA: 0x00191255 File Offset: 0x0018F455
	public bool Nothing()
	{
		return !this.waitingForAction && !this.hasAction && !this.doingAction;
	}

	// Token: 0x060014BA RID: 5306 RVA: 0x00191278 File Offset: 0x0018F478
	public void Beginning()
	{
		if (!this.character)
		{
			return;
		}
		if (this.animator.enabled)
		{
			this.animator.Stop();
		}
		this.sheen.enabled = false;
		this.t = 0f - this.offsetTime;
		this.updateTime = 0f;
		this.speed = 1f;
		this.tr.position = this.start.position;
		this.doingAction = false;
		this.waitingForAction = false;
		this.hasAction = false;
		if (!Records.x.turnBased)
		{
			this.waitingToPickAction = true;
			if (!this.character.mainSelected || (this.character.mainSelected && (Records.x.freeAttack || Records.x.cyclingAttacks)))
			{
				this.PickAction();
			}
		}
		this.overridePause = false;
	}

	// Token: 0x060014BB RID: 5307 RVA: 0x0019135C File Offset: 0x0018F55C
	public void WaitForAction(float actionSpeed, bool doActionAtEnd, bool skipReset)
	{
		if (this.animator.enabled)
		{
			this.animator.Stop();
		}
		if (!this.character)
		{
			return;
		}
		this.sheen.enabled = false;
		this.casting = false;
		this.doingAction = false;
		if (!skipReset)
		{
			this.tr.position = this.start.position;
			this.t = 0f - this.offsetTime;
			this.updateTime = 0f;
		}
		this.speed = actionSpeed;
		this.waitingToPickAction = false;
		this.waitingForAction = false;
		this.hasAction = doActionAtEnd;
	}

	// Token: 0x040023C4 RID: 9156
	public Character character;

	// Token: 0x040023C5 RID: 9157
	public Transform start;

	// Token: 0x040023C6 RID: 9158
	public Transform end;

	// Token: 0x040023C7 RID: 9159
	public Transform action;

	// Token: 0x040023C8 RID: 9160
	public float speed;

	// Token: 0x040023C9 RID: 9161
	public bool knockedOut;

	// Token: 0x040023CA RID: 9162
	public bool downed;

	// Token: 0x040023CB RID: 9163
	public bool waitingForAction;

	// Token: 0x040023CC RID: 9164
	public bool waitingToPickAction;

	// Token: 0x040023CD RID: 9165
	public bool hasAction;

	// Token: 0x040023CE RID: 9166
	public bool doingAction;

	// Token: 0x040023CF RID: 9167
	public bool doingRepath;

	// Token: 0x040023D0 RID: 9168
	private Transform tr;

	// Token: 0x040023D1 RID: 9169
	public float t;

	// Token: 0x040023D2 RID: 9170
	public float updateTime;

	// Token: 0x040023D3 RID: 9171
	public Image[] images;

	// Token: 0x040023D4 RID: 9172
	public RawImage portrait;

	// Token: 0x040023D5 RID: 9173
	public float offsetTime;

	// Token: 0x040023D6 RID: 9174
	public Color normalColor;

	// Token: 0x040023D7 RID: 9175
	public Color hoverColor;

	// Token: 0x040023D8 RID: 9176
	public RenderTexture texturesIdle;

	// Token: 0x040023D9 RID: 9177
	private AnimatorStateInfo stateInfo;

	// Token: 0x040023DA RID: 9178
	private AnimatorClipInfo[] clipInfo;

	// Token: 0x040023DB RID: 9179
	private AnimationClip clip;

	// Token: 0x040023DC RID: 9180
	private AnimationEvent[] events;

	// Token: 0x040023DD RID: 9181
	public bool needsSpeed;

	// Token: 0x040023DE RID: 9182
	public Image sheen;

	// Token: 0x040023DF RID: 9183
	public HannahAnimator animator;

	// Token: 0x040023E0 RID: 9184
	public string clipInfoName;

	// Token: 0x040023E1 RID: 9185
	private int currentHash;

	// Token: 0x040023E2 RID: 9186
	public float clipLength;

	// Token: 0x040023E3 RID: 9187
	public bool overridePause;

	// Token: 0x040023E4 RID: 9188
	public bool casting;
}
