using System;
using UnityEngine;

public class TuneRadioMinigame : Minigame
{
	public RadioWaveBehaviour actualSignal;

	public DialBehaviour dial;

	public SpriteRenderer redLight;

	public SpriteRenderer greenLight;

	public float Tolerance = 0.1f;

	public float targetAngle;

	public bool finished;

	private float steadyTimer;

	public AudioClip StaticSound;

	public AudioClip RadioSound;

	public override void Begin(PlayerTask task)
	{
		base.Begin(task);
		this.targetAngle = this.dial.DialRange.Next();
		if (Constants.ShouldPlaySfx())
		{
			SoundManager.Instance.PlayDynamicSound("CommsRadio", this.RadioSound, true, new DynamicSound.GetDynamicsFunction(this.GetRadioVolume), true);
			SoundManager.Instance.PlayDynamicSound("RadioStatic", this.StaticSound, true, new DynamicSound.GetDynamicsFunction(this.GetStaticVolume), true);
		}
	}

	private void GetRadioVolume(AudioSource player, float dt)
	{
		player.volume = 1f - this.actualSignal.NoiseLevel;
	}

	private void GetStaticVolume(AudioSource player, float dt)
	{
		player.volume = this.actualSignal.NoiseLevel;
	}

	public void Update()
	{
		if (this.finished)
		{
			return;
		}
		float f = Mathf.Abs((this.targetAngle - this.dial.Value) / this.dial.DialRange.Width) * 2f;
		this.actualSignal.NoiseLevel = Mathf.Clamp(Mathf.Sqrt(f), 0f, 1f);
		if (this.actualSignal.NoiseLevel <= this.Tolerance)
		{
			this.redLight.color = new Color(0.35f, 0f, 0f);
			if (!this.dial.Engaged)
			{
				this.FinishGame();
				return;
			}
			this.steadyTimer += Time.deltaTime;
			if (this.steadyTimer > 1.5f)
			{
				this.FinishGame();
				return;
			}
		}
		else
		{
			this.redLight.color = new Color(1f, 0f, 0f);
			this.steadyTimer = 0f;
		}
	}

	private void FinishGame()
	{
		this.greenLight.color = Color.green;
		this.finished = true;
		this.dial.enabled = false;
		this.dial.SetValue(this.targetAngle);
		this.actualSignal.NoiseLevel = 0f;
		if (PlayerControl.LocalPlayer)
		{
			ShipStatus.Instance.RpcRepairSystem(SystemTypes.Comms, 0);
		}
		base.StartCoroutine(base.CoStartClose(0.75f));
		try
		{
			((SabotageTask)this.MyTask).MarkContributed();
		}
		catch
		{
		}
	}

	public override void Close()
	{
		SoundManager.Instance.StopSound(this.StaticSound);
		SoundManager.Instance.StopSound(this.RadioSound);
		base.Close();
	}
}