using System;
using System.Collections;
using UnityEngine;

public class RoomTracker : MonoBehaviour
{
	public TextRenderer text;

	public float SourceY = -2.5f;

	public float TargetY = -3.25f;

	private Collider2D playerCollider;

	private ContactFilter2D filter;

	private Collider2D[] buffer = new Collider2D[10];

	public ShipRoom LastRoom;

	private Coroutine slideInRoutine;

	public void Awake()
	{
		this.filter = default(ContactFilter2D);
		this.filter.layerMask = Constants.PlayersOnlyMask;
		this.filter.useLayerMask = true;
		this.filter.useTriggers = false;
	}

	public void OnDisable()
	{
		this.LastRoom = null;
		Vector3 localPosition = this.text.transform.localPosition;
		localPosition.y = this.TargetY;
		this.text.transform.localPosition = localPosition;
	}

	public void FixedUpdate()
	{
		ShipRoom[] array = null;
		if (LobbyBehaviour.Instance)
		{
			array = LobbyBehaviour.Instance.AllRooms;
		}
		if (ShipStatus.Instance)
		{
			array = ShipStatus.Instance.AllRooms;
		}
		if (array == null)
		{
			return;
		}
		ShipRoom shipRoom = null;
		if (this.LastRoom)
		{
			int hitCount = this.LastRoom.roomArea.OverlapCollider(this.filter, this.buffer);
			if (RoomTracker.CheckHitsForPlayer(this.buffer, hitCount))
			{
				shipRoom = this.LastRoom;
			}
		}
		if (!shipRoom)
		{
			foreach (ShipRoom shipRoom2 in array)
			{
				if (shipRoom2.roomArea)
				{
					int hitCount2 = shipRoom2.roomArea.OverlapCollider(this.filter, this.buffer);
					if (RoomTracker.CheckHitsForPlayer(this.buffer, hitCount2))
					{
						shipRoom = shipRoom2;
					}
				}
			}
		}
		if (shipRoom)
		{
			if (this.LastRoom != shipRoom)
			{
				this.LastRoom = shipRoom;
				if (this.slideInRoutine != null)
				{
					base.StopCoroutine(this.slideInRoutine);
				}
				if (shipRoom.RoomId != SystemTypes.Hallway)
				{
					this.slideInRoutine = base.StartCoroutine(this.CoSlideIn(shipRoom.RoomId));
					return;
				}
				this.slideInRoutine = base.StartCoroutine(this.SlideOut());
				return;
			}
		}
		else if (this.LastRoom)
		{
			this.LastRoom = null;
			if (this.slideInRoutine != null)
			{
				base.StopCoroutine(this.slideInRoutine);
			}
			this.slideInRoutine = base.StartCoroutine(this.SlideOut());
		}
	}

	private IEnumerator CoSlideIn(SystemTypes newRoom)
	{
		yield return this.SlideOut();
		Vector3 tempPos = this.text.transform.localPosition;
		Color tempColor = Color.white;
		this.text.Text = DestroyableSingleton<TranslationController>.Instance.GetString(newRoom);
		float timer = 0f;
		while (timer < 0.25f)
		{
			timer = Mathf.Min(0.25f, timer + Time.deltaTime);
			float t = timer / 0.25f;
			tempPos.y = Mathf.SmoothStep(this.TargetY, this.SourceY, t);
			tempColor.a = Mathf.Lerp(0f, 1f, t);
			this.text.transform.localPosition = tempPos;
			this.text.Color = tempColor;
			yield return null;
		}
		yield break;
	}

	private IEnumerator SlideOut()
	{
		Vector3 tempPos = this.text.transform.localPosition;
		Color tempColor = Color.white;
		float timer = FloatRange.ReverseLerp(tempPos.y, this.SourceY, this.TargetY) * 0.1f;
		while (timer < 0.1f)
		{
			timer = Mathf.Min(0.1f, timer + Time.deltaTime);
			float t = timer / 0.1f;
			tempPos.y = Mathf.SmoothStep(this.SourceY, this.TargetY, t);
			tempColor.a = Mathf.Lerp(1f, 0f, t);
			this.text.transform.localPosition = tempPos;
			this.text.Color = tempColor;
			yield return null;
		}
		yield break;
	}

	private static bool CheckHitsForPlayer(Collider2D[] buffer, int hitCount)
	{
		if (!PlayerControl.LocalPlayer)
		{
			return false;
		}
		for (int i = 0; i < hitCount; i++)
		{
			if (buffer[i].gameObject == PlayerControl.LocalPlayer.gameObject)
			{
				return true;
			}
		}
		return false;
	}
}