using System;
using PowerTools;
using UnityEngine;

public class Vent : MonoBehaviour, IUsable
{
	public float UsableDistance
	{
		get
		{
			return 0.75f;
		}
	}

	public float PercentCool
	{
		get
		{
			return 0f;
		}
	}

	public int Id;

	public Vent Left;

	public Vent Right;

	public ButtonBehavior[] Buttons;

	public AnimationClip EnterVentAnim;

	public AnimationClip ExitVentAnim;

	private static readonly Vector3 CollOffset = new Vector3(0f, -0.3636057f, 0f);

	private SpriteRenderer myRend;

	private void Start()
	{
		this.SetButtons(false);
		this.myRend = base.GetComponent<SpriteRenderer>();
	}

	public void SetButtons(bool enabled)
	{
		Vent[] array = new Vent[]
		{
			this.Right,
			this.Left
		};
		for (int i = 0; i < this.Buttons.Length; i++)
		{
			ButtonBehavior buttonBehavior = this.Buttons[i];
			if (enabled)
			{
				Vent vent = array[i];
				if (vent)
				{
					buttonBehavior.gameObject.SetActive(true);
					Vector3 localPosition = (vent.transform.position - base.transform.position).normalized * 0.7f;
					localPosition.y -= 0.08f;
					localPosition.z = -10f;
					buttonBehavior.transform.localPosition = localPosition;
					buttonBehavior.transform.LookAt2d(vent.transform);
				}
				else
				{
					buttonBehavior.gameObject.SetActive(false);
				}
			}
			else
			{
				buttonBehavior.gameObject.SetActive(false);
			}
		}
	}

	public float CanUse(GameData.PlayerInfo pc, out bool canUse, out bool couldUse)
	{
		float num = float.MaxValue;
		PlayerControl @object = pc.Object;
		couldUse = (pc.IsImpostor && !pc.IsDead && (@object.CanMove || @object.inVent));
		canUse = couldUse;
		if (canUse)
		{
			num = Vector2.Distance(@object.GetTruePosition(), base.transform.position);
			canUse &= (num <= this.UsableDistance);
		}
		return num;
	}

	public void SetOutline(bool on, bool mainTarget)
	{
		this.myRend.material.SetFloat("_Outline", (float)(on ? 1 : 0));
		this.myRend.material.SetColor("_OutlineColor", Color.red);
		this.myRend.material.SetColor("_AddColor", mainTarget ? Color.red : Color.clear);
	}

	public void ClickRight()
	{
		if (this.Right)
		{
			Vent.DoMove(this.Right.transform.position - Vent.CollOffset);
			this.SetButtons(false);
			this.Right.SetButtons(true);
		}
	}

	public void ClickLeft()
	{
		if (this.Left)
		{
			Vent.DoMove(this.Left.transform.position - Vent.CollOffset);
			this.SetButtons(false);
			this.Left.SetButtons(true);
		}
	}

	private static void DoMove(Vector3 pos)
	{
		PlayerControl.LocalPlayer.NetTransform.RpcSnapTo(pos);
		if (Constants.ShouldPlaySfx())
		{
			SoundManager.Instance.PlaySound(PlayerControl.LocalPlayer.VentMoveSounds.Random<AudioClip>(), false, 1f).pitch = FloatRange.Next(0.8f, 1.2f);
		}
	}

	public void Use()
	{
		bool flag;
		bool flag2;
		this.CanUse(PlayerControl.LocalPlayer.Data, out flag, out flag2);
		if (!flag)
		{
			return;
		}
		PlayerControl localPlayer = PlayerControl.LocalPlayer;
		if (Constants.ShouldPlaySfx())
		{
			SoundManager.Instance.StopSound(localPlayer.VentEnterSound);
			SoundManager.Instance.PlaySound(localPlayer.VentEnterSound, false, 1f).pitch = FloatRange.Next(0.8f, 1.2f);
		}
		if (localPlayer.inVent)
		{
			localPlayer.MyPhysics.RpcExitVent(this.Id);
			this.SetButtons(false);
			return;
		}
		localPlayer.MyPhysics.RpcEnterVent(this.Id);
		this.SetButtons(true);
	}

	internal void EnterVent()
	{
		base.GetComponent<SpriteAnim>().Play(this.EnterVentAnim, 1f);
	}

	internal void ExitVent()
	{
		base.GetComponent<SpriteAnim>().Play(this.ExitVentAnim, 1f);
	}
}