using System;
using System.Collections;
using UnityEngine;

// Token: 0x020000F9 RID: 249
public class LandingSpot : MonoBehaviour
{
	// Token: 0x06001597 RID: 5527 RVA: 0x0019A913 File Offset: 0x00198B13
	private void Awake()
	{
		if (this._transformCache == null)
		{
			this._transformCache = base.transform;
		}
		this.landingPoint = this._transformCache.position;
		this.landingRot = this._transformCache.rotation;
	}

	// Token: 0x06001598 RID: 5528 RVA: 0x0019A954 File Offset: 0x00198B54
	public void Start()
	{
		if (this._transformCache == null)
		{
			this._transformCache = base.transform;
		}
		if (this._controller == null)
		{
			this._controller = this._transformCache.parent.GetComponent<LandingSpotController>();
		}
		if (this._controller._autoCatchDelay.x > 0f)
		{
			base.StartCoroutine(this.GetFlockChild(this._controller._autoCatchDelay.x, this._controller._autoCatchDelay.y));
		}
		if (this._controller._randomRotate && this._controller._parentBirdToSpot)
		{
			LandingSpotController controller = this._controller;
			Debug.LogWarning(((controller != null) ? controller.ToString() : null) + "\nEnabling random rotate and parent bird to spot is not yet available, disabling random rotate");
			this._controller._randomRotate = false;
		}
		this.landingPoint = this._transformCache.position;
		this.landingRot = this._transformCache.rotation;
	}

	// Token: 0x06001599 RID: 5529 RVA: 0x0019AA4C File Offset: 0x00198C4C
	public IEnumerator GetFlockChild(float minDelay, float maxDelay)
	{
		yield return new WaitForSeconds(Random.Range(minDelay, maxDelay));
		if (this._controller._flock.gameObject.activeInHierarchy && this._landingChild == null)
		{
			FlockChild flockChild = null;
			for (int i = 0; i < this._controller._flock._roamers.Count; i++)
			{
				FlockChild flockChild2 = this._controller._flock._roamers[i];
				if (flockChild2 != null && !flockChild2._landing && flockChild2.gameObject.activeInHierarchy)
				{
					flockChild2._landingSpot = this;
					this._distance = Vector3.Distance(flockChild2._thisT.position, this._transformCache.position);
					if (!this._controller._onlyBirdsAbove)
					{
						if (flockChild == null && this._controller._maxBirdDistance > this._distance && this._controller._minBirdDistance < this._distance)
						{
							flockChild = flockChild2;
							if (!this._controller._takeClosest)
							{
								break;
							}
						}
						else if (flockChild != null && Vector3.Distance(flockChild._thisT.position, this._transformCache.position) > this._distance)
						{
							flockChild = flockChild2;
						}
					}
					else if (flockChild == null && flockChild2._thisT.position.y > this._transformCache.position.y && this._controller._maxBirdDistance > this._distance && this._controller._minBirdDistance < this._distance)
					{
						flockChild = flockChild2;
						if (!this._controller._takeClosest)
						{
							break;
						}
					}
					else if (flockChild != null && flockChild2._thisT.position.y > this._transformCache.position.y && Vector3.Distance(flockChild._thisT.position, this._transformCache.position) > this._distance)
					{
						flockChild = flockChild2;
					}
				}
			}
			if (flockChild != null)
			{
				if (this._controller._abortLanding)
				{
					base.Invoke("ReleaseFlockChild", this._controller._abortLandingTimer);
				}
				this._landingChild = flockChild;
				if (this._controller._parentBirdToSpot)
				{
					this._landingChild.transform.SetParent(base.transform);
				}
				this._landing = true;
				this._landingChild._landing = true;
				if (this._controller._autoDismountDelay.x > 0f)
				{
					base.Invoke("ReleaseFlockChild", Random.Range(this._controller._autoDismountDelay.x, this._controller._autoDismountDelay.y));
				}
				this._controller._activeLandingSpots++;
				this.RandomRotate();
			}
			else if (this._controller._autoCatchDelay.x > 0f)
			{
				base.StartCoroutine(this.GetFlockChild(this._controller._autoCatchDelay.x, this._controller._autoCatchDelay.y));
			}
		}
		yield break;
	}

	// Token: 0x0600159A RID: 5530 RVA: 0x0019AA6C File Offset: 0x00198C6C
	public void InstantLand()
	{
		if (this._controller._flock.gameObject.activeInHierarchy && this._landingChild == null)
		{
			FlockChild flockChild = null;
			for (int i = 0; i < this._controller._flock._roamers.Count; i++)
			{
				FlockChild flockChild2 = this._controller._flock._roamers[i];
				if (!flockChild2._landing)
				{
					flockChild = flockChild2;
				}
			}
			if (flockChild != null)
			{
				this._landingChild = flockChild;
				if (this._controller._parentBirdToSpot)
				{
					this._landingChild.transform.SetParent(base.transform);
				}
				this._landingChild._move = false;
				flockChild._speed = 0f;
				flockChild._targetSpeed = 0f;
				flockChild._landingSpot = this;
				this._landing = true;
				this._controller._activeLandingSpots++;
				this._landingChild._landing = true;
				this._landingChild._thisT.position = this._landingChild.GetLandingSpotPosition();
				if (this._controller._randomRotate)
				{
					this._landingChild._thisT.Rotate(Vector3.up, Random.Range(0f, 360f));
				}
				else
				{
					this._landingChild._thisT.rotation = this._transformCache.rotation;
				}
				if (!this._landingChild._animationIsBaked)
				{
					if (!this._landingChild._animator)
					{
						this._landingChild._modelAnimation.Play(this._landingChild._spawner._idleAnimation);
					}
					else
					{
						this._landingChild._animator.Play(this._landingChild._spawner._idleAnimation);
					}
				}
				else
				{
					this._landingChild._bakedAnimator.SetAnimation(2, -1);
				}
				if (this._controller._autoDismountDelay.x > 0f)
				{
					base.Invoke("ReleaseFlockChild", Random.Range(this._controller._autoDismountDelay.x, this._controller._autoDismountDelay.y));
					return;
				}
			}
			else if (this._controller._autoCatchDelay.x > 0f)
			{
				base.StartCoroutine(this.GetFlockChild(this._controller._autoCatchDelay.x, this._controller._autoCatchDelay.y));
			}
		}
	}

	// Token: 0x0600159B RID: 5531 RVA: 0x0019ACD8 File Offset: 0x00198ED8
	public void ReleaseFlockChild()
	{
		if (this._controller._flock.gameObject.activeInHierarchy && this._landingChild != null)
		{
			this.EmitFeathers();
			this._landingChild._modelT.localEulerAngles = new Vector3(0f, 0f, 0f);
			this._landing = false;
			this._landingChild._avoid = true;
			this._landingChild._closeToSpot = false;
			this._landingChild._damping = this._landingChild._spawner._maxDamping;
			this._landingChild._targetSpeed = Random.Range(this._landingChild._spawner._minSpeed, this._landingChild._spawner._maxSpeed);
			this._landingChild._move = true;
			this._landingChild._landing = false;
			this._landingChild.currentAnim = -1;
			this._landingChild.Flap(0.1f);
			if (this._controller._parentBirdToSpot)
			{
				this._landingChild._spawner.AddChildToParent(this._landingChild._thisT);
			}
			this._landingChild._wayPoint = new Vector3(this._landingChild._wayPoint.x + 5f, this._transformCache.position.y + 5f, this._landingChild._wayPoint.z + 5f);
			this._landingChild._damping = 0.1f;
			if (this._controller._autoCatchDelay.x > 0f)
			{
				base.StartCoroutine(this.GetFlockChild(this._controller._autoCatchDelay.x + 0.1f, this._controller._autoCatchDelay.y + 0.1f));
			}
			this._controller._activeLandingSpots--;
			if (this._controller._abortLanding)
			{
				base.CancelInvoke("ReleaseFlockChild");
				if (this._landingChild.currentAnim != 2)
				{
					this._landingChild.FindWaypoint();
				}
			}
			this._landingChild = null;
		}
	}

	// Token: 0x0600159C RID: 5532 RVA: 0x0019AF00 File Offset: 0x00199100
	public void RandomRotate()
	{
		if (this._controller._randomRotate)
		{
			Quaternion rotation = this._transformCache.rotation;
			Vector3 eulerAngles = rotation.eulerAngles;
			eulerAngles.y = (float)Random.Range(-180, 180);
			rotation.eulerAngles = eulerAngles;
			this._transformCache.rotation = rotation;
		}
	}

	// Token: 0x0600159D RID: 5533 RVA: 0x0019AF5C File Offset: 0x0019915C
	public void EmitFeathers()
	{
		if (this._controller._featherPS == null)
		{
			return;
		}
		this._controller._featherPS.position = this._landingChild._thisT.position;
		if (this._controller._featherParticles)
		{
			this._controller._featherParticles.Emit(Random.Range(0, 3));
		}
	}

	// Token: 0x040025AE RID: 9646
	[HideInInspector]
	public FlockChild _landingChild;

	// Token: 0x040025AF RID: 9647
	[HideInInspector]
	public bool _landing;

	// Token: 0x040025B0 RID: 9648
	public LandingSpotController _controller;

	// Token: 0x040025B1 RID: 9649
	public Transform _transformCache;

	// Token: 0x040025B2 RID: 9650
	public Vector3 _preLandWaypoint;

	// Token: 0x040025B3 RID: 9651
	public float _distance;

	// Token: 0x040025B4 RID: 9652
	public Vector3 landingPoint;

	// Token: 0x040025B5 RID: 9653
	public Quaternion landingRot;
}
