using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x02000048 RID: 72
public class QuestTimePoint : MonoBehaviour
{
	// Token: 0x060008BC RID: 2236 RVA: 0x000BE4B0 File Offset: 0x000BC6B0
	public void Setup(Library.Characters c)
	{
		this.characterRow = c;
		this.listenForNearPoint = true;
		this.alwaysOn = true;
		if (c._Quest == "C6_Dan")
		{
			this.type = "Willowmaid";
			this.ignoreNodeIDs = true;
			this.character.alwaysOn = true;
			if (this.character.inactive)
			{
				this.character.TurnScriptsOn();
			}
		}
		if (!this.listenForNearPoint)
		{
			base.enabled = false;
		}
		if (this.inTimeJump)
		{
			this.Update();
		}
	}

	// Token: 0x060008BD RID: 2237 RVA: 0x000BE538 File Offset: 0x000BC738
	private void Update()
	{
		if (!this.done && this.listenForNearPoint && this.character && this.character.IsSentient() && !this.character.hasActions)
		{
			if (Records.x.editor)
			{
				this.character.running = true;
			}
			if (this.ignoreNodeIDs)
			{
				if (!Links.x.GetNearestParty(this.character.currentPosition, null, 20f))
				{
					this.character.ignoreNodeIDs = true;
				}
				else
				{
					this.character.ignoreNodeIDs = false;
				}
			}
			if (this.alwaysOn)
			{
				this.character.alwaysOn = true;
				if (this.character.inactive)
				{
					this.character.TurnScriptsOn();
				}
			}
			this.distance = (this.character.currentPosition - this.destination).sqrMagnitude;
			if (this.distance < 16f || this.instantFinish)
			{
				this.done = true;
				if (this.type == "Willowmaid")
				{
					base.StartCoroutine(this.Willowmaid());
				}
			}
		}
	}

	// Token: 0x060008BE RID: 2238 RVA: 0x000BE672 File Offset: 0x000BC872
	public void Ending()
	{
		if (!this.done && !this.busy && !this.instantFinish && this.type == "Willowmaid")
		{
			this.character.StartQuip("Oh! Just on a stroll. Funny you're here too, I'll just head back.");
		}
	}

	// Token: 0x060008BF RID: 2239 RVA: 0x000BE6AE File Offset: 0x000BC8AE
	private IEnumerator Willowmaid()
	{
		this.busy = true;
		Library.Inventory invRow = Links.x.library.GetInvRowFromName("DryadFlower");
		int hasItem = this.character.HasItem(invRow._ID);
		List<int> bag = this.character.bag;
		List<Vector4> durability = this.character.durability;
		List<Vector3> sockets = this.character.sockets;
		Debug.Log("Willowmaid " + this.hour.ToString() + " " + hasItem.ToString());
		Vector3 dropPoint = this.character.tr.position;
		this.distance = (this.character.currentPosition - this.destination).sqrMagnitude;
		if (this.instantFinish && this.distance > 16f)
		{
			dropPoint = this.destination;
		}
		if (hasItem > -1 && this.hour == 17)
		{
			float timeToLerp = 5f;
			float percentage = 0f;
			float startTime = Time.time;
			bool canSeeParty = Links.x.sensory.CanSeeOrHearWholeParty(this.creatures, true, false);
			if (!this.instantFinish)
			{
				while (this.character.moving)
				{
					yield return null;
					if (!canSeeParty && Links.x.sensory.CanSeeOrHearWholeParty(this.creatures, true, false))
					{
						canSeeParty = true;
					}
				}
				while (percentage < 1f && !this.instantFinish)
				{
					if (!this.character.turning && !this.character.moving && !this.character.Pivoting())
					{
						this.character.PatrolRotate();
					}
					percentage = (Time.time - startTime) / timeToLerp;
					if (!canSeeParty && Links.x.sensory.CanSeeOrHearWholeParty(this.creatures, true, false))
					{
						canSeeParty = true;
					}
					yield return null;
				}
			}
			if (!canSeeParty || this.instantFinish)
			{
				timeToLerp = 0.5f;
				percentage = 0f;
				startTime = Time.time;
				if (!this.instantFinish)
				{
					this.character.PlayAnimation("Use", 0f);
					this.character.StartQuip("Topa-du");
					while (percentage < 1f && !this.instantFinish)
					{
						percentage = (Time.time - startTime) / timeToLerp;
						yield return null;
					}
				}
				Debug.Log("Drop item");
				int num = hasItem;
				Item item2 = Links.x.inventory.groundBag.DropLoot(bag[num], bag[num + 1], sockets[num], sockets[num + 1], durability[num / 2], this.character, dropPoint, true, false, -1);
				if (item2)
				{
					Vector3 vector = dropPoint;
					RaycastHit raycastHit;
					if (Physics.Linecast(vector + new Vector3(0f, 10f, 0f), vector - new Vector3(0f, 20f, 0f), out raycastHit, 4194305))
					{
						item2.tr.position = raycastHit.point;
					}
					else
					{
						item2.tr.position = new Vector3(Random.Range(-1f, 1f), 0f, Random.Range(-1f, 1f));
					}
					item2.durability = new Vector4(Mathf.Floor((float)Links.x.gameplay.GetCurrentGameTimeHours()) + (float)item2.invRow._Life, item2.durability.y, item2.durability.z, item2.durability.w);
				}
				bag[num] = 0;
				bag[num + 1] = 0;
				durability[num / 2] = Vector4.zero;
				sockets[num] = Vector3.zero;
				sockets[num + 1] = Vector3.zero;
			}
			else
			{
				this.character.StartQuip("Oh! Just on a stroll. Funny you're here too, I'll just head back.");
				durability[hasItem / 2] = new Vector4(Mathf.Floor((float)Links.x.gameplay.GetCurrentGameTimeHours()) + (float)invRow._Life, durability[hasItem / 2].y, durability[hasItem / 2].z, durability[hasItem / 2].w);
			}
			this.creatures.hasTimePoint = -1;
			this.creatures.timeNode = null;
			this.creatures.WalkToNextPoint(this.character);
		}
		if (hasItem == -1 && this.hour == 20)
		{
			int id = invRow._ID;
			int num2 = 20;
			Item item = Links.x.diorama.CheckGroundForItem(null, 0, id, dropPoint, this.character, num2);
			string text = "Found item? ";
			Item item3 = item;
			Debug.Log(text + ((item3 != null) ? item3.ToString() : null));
			Item itemDead = Links.x.diorama.CheckGroundForItem(null, 0, Links.x.library.GetInvRowFromName("DryadFlowerDead")._ID, dropPoint, this.character, num2);
			string text2 = "Found item? ";
			Item item4 = itemDead;
			Debug.Log(text2 + ((item4 != null) ? item4.ToString() : null));
			if (item || itemDead)
			{
				if (itemDead)
				{
					item = itemDead;
				}
				float startTime = 0.5f;
				float percentage = 0f;
				float timeToLerp = Time.time;
				if (!this.instantFinish)
				{
					this.character.PlayAnimation("Use", 0f);
					while (percentage < 1f && !this.instantFinish)
					{
						percentage = (Time.time - timeToLerp) / startTime;
						yield return null;
					}
				}
				int num3 = this.character.FreeBagID();
				if (num3 == -1)
				{
					bag.Add(0);
					bag.Add(0);
					sockets.Add(Vector3.zero);
					sockets.Add(Vector3.zero);
					durability.Add(Vector4.zero);
					num3 = this.character.FreeBagID();
				}
				bag[num3] = item.ID;
				bag[num3 + 1] = item.stackSize;
				if (!itemDead)
				{
					item.durability.x = Mathf.Floor((float)Links.x.gameplay.GetCurrentGameTimeHours()) + (float)item.invRow._Life;
				}
				durability[num3 / 2] = item.durability;
				sockets[num3] = item.socketA;
				sockets[num3 + 1] = item.socketB;
				item.DestroyItem(false, false);
				this.creatures.hasTimePoint = -1;
				this.creatures.timeNode = null;
				this.creatures.WalkToNextPoint(this.character);
				if (!this.instantFinish && !itemDead)
				{
					float num4 = Random.Range(2f, 4f);
					yield return new WaitForSeconds(num4);
					int num5 = Random.Range(0, 5);
					if (num5 == 0)
					{
						this.character.StartQuip("I missed you");
					}
					if (num5 == 1)
					{
						this.character.StartQuip("Quiet out tonight");
					}
					if (num5 == 2)
					{
						this.character.StartQuip("How was the water?");
					}
					if (num5 == 3)
					{
						this.character.StartQuip("I wouldn't forget");
					}
					if (num5 == 4)
					{
						this.character.StartQuip("I had curry again for dinner");
					}
				}
				if (itemDead)
				{
				}
			}
			else
			{
				this.creatures.hasTimePoint = -1;
				this.creatures.timeNode = null;
				this.creatures.WalkToNextPoint(this.character);
			}
			item = null;
			itemDead = null;
		}
		this.destroyOnComplete = true;
		if (!this.instantFinish)
		{
			yield return new WaitForSeconds(20f);
		}
		this.character.alwaysOn = false;
		if (this.destroyOnComplete)
		{
			Object.Destroy(this);
		}
		yield break;
	}

	// Token: 0x04000DBB RID: 3515
	public Vector3 destination;

	// Token: 0x04000DBC RID: 3516
	public Character character;

	// Token: 0x04000DBD RID: 3517
	public CreatureActions creatures;

	// Token: 0x04000DBE RID: 3518
	public int hour;

	// Token: 0x04000DBF RID: 3519
	public bool done;

	// Token: 0x04000DC0 RID: 3520
	public bool busy;

	// Token: 0x04000DC1 RID: 3521
	public bool destroyOnComplete;

	// Token: 0x04000DC2 RID: 3522
	public float distance;

	// Token: 0x04000DC3 RID: 3523
	private Library.Characters characterRow;

	// Token: 0x04000DC4 RID: 3524
	public string type;

	// Token: 0x04000DC5 RID: 3525
	private bool listenForNearPoint;

	// Token: 0x04000DC6 RID: 3526
	private bool alwaysOn;

	// Token: 0x04000DC7 RID: 3527
	private bool ignoreNodeIDs;

	// Token: 0x04000DC8 RID: 3528
	private bool inTimeJump;

	// Token: 0x04000DC9 RID: 3529
	public bool instantFinish;
}
