using System;
using System.Collections;
using UnityEngine;

// Token: 0x02000015 RID: 21
public class AudioListenForMain : MonoBehaviour
{
	// Token: 0x0600020F RID: 527 RVA: 0x0002C690 File Offset: 0x0002A890
	private void Start()
	{
		this.coolAudio = base.gameObject.GetComponent<CoolAudioSource>();
		this.coolAudio.audioSource.volume = 0f;
		this.distance = this.coolAudio.audioSource.maxDistance;
		this.offDistance = this.distance + 10f;
		this.position = base.gameObject.transform.position;
		this.maxAudioVolume = 1f;
		if (this.coolAudio.is2D)
		{
			this.maxAudioVolume = 0.9f;
			if (this.coolAudio.audioSource.clip.name.Contains("Bar"))
			{
				this.maxAudioVolume = 0.5f;
			}
			if (this.coolAudio.audioSource.clip.name.Contains("Rozafir"))
			{
				this.keepPlayingThroughInterior = true;
			}
		}
		if (this.coolAudio.audioSource.clip.name.Contains("RozafirDrums"))
		{
			this.listenForCharacter = true;
			this.overrideMainSoundtrack = true;
			this.characterListenName = "RozafirDancerFlutist";
		}
	}

	// Token: 0x06000210 RID: 528 RVA: 0x0002C7B4 File Offset: 0x0002A9B4
	private void Update()
	{
		if (Links.x.hasMain)
		{
			if (!Records.x.inMenus && !Links.x.gaia.loadingCharacterCreation)
			{
				float sqrMagnitude = (Links.x.main.currentPosition - this.position).sqrMagnitude;
				if (sqrMagnitude < this.distance * this.distance)
				{
					bool flag = false;
					if (this.listenForCharacter)
					{
						if (!this.character)
						{
							this.character = Links.x.diorama.FindCharacterAnyState(this.characterListenName);
						}
						if (this.character && !this.character.dead && !this.character.stunned)
						{
							flag = true;
						}
					}
					if (!this.inRange && (this.coolAudio.playEveryTimePlayerIsNear || this.endAudio == null) && (!this.listenForCharacter || flag))
					{
						this.coolAudio.audioSource.Play();
						if (this.endAudio == null)
						{
							this.coolAudio.audioSource.volume = 0f;
						}
						this.inRange = true;
						this.StartAudioBegin();
					}
				}
				else if (this.inRange && sqrMagnitude > this.offDistance * this.offDistance && this.endAudio == null)
				{
					if (this.keepPlayingThroughInterior)
					{
						if (!(Links.x.rtsCamera.currentBoundsName == "interior_frog_bridgeA") && !Links.x.cameraEffects.transitioning)
						{
							this.StartAudioEnd();
						}
					}
					else
					{
						this.StartAudioEnd();
					}
				}
			}
			if (this.playing && Links.x.gaia.loadingCharacterCreation)
			{
				this.StartAudioEnd();
			}
		}
	}

	// Token: 0x06000211 RID: 529 RVA: 0x0002C974 File Offset: 0x0002AB74
	private void StartAudioBegin()
	{
		if (this.startAudio == null)
		{
			this.startAudio = this.AudioFadeIn();
			base.StartCoroutine(this.startAudio);
		}
	}

	// Token: 0x06000212 RID: 530 RVA: 0x0002C998 File Offset: 0x0002AB98
	private void OnDisable()
	{
		if (this.endAudio != null)
		{
			base.StopCoroutine(this.endAudio);
			this.endAudio = null;
		}
		if (this.inRange)
		{
			if (Links.x.hasMain && ((this.coolAudio.loopIt && this.overrideMainSoundtrack) || !this.coolAudio.loopIt))
			{
				Links.x.gaia.playingSoundtrackFromInWorld = false;
				Links.x.gaia.SetEnvironmentPlaylist(Links.x.gameplay.sailing, Links.x.main.inTown, true);
			}
			this.inRange = false;
			this.coolAudio.audioSource.Stop();
		}
		if (this.startAudio != null)
		{
			base.StopCoroutine(this.startAudio);
			this.startAudio = null;
			if (Links.x.hasMain && ((this.coolAudio.loopIt && this.overrideMainSoundtrack) || !this.coolAudio.loopIt))
			{
				Links.x.gaia.playingSoundtrackFromInWorld = false;
				Links.x.gaia.SetEnvironmentPlaylist(Links.x.gameplay.sailing, Links.x.main.inTown, true);
			}
			this.inRange = false;
		}
	}

	// Token: 0x06000213 RID: 531 RVA: 0x0002CADF File Offset: 0x0002ACDF
	private IEnumerator AudioFadeIn()
	{
		if ((this.coolAudio.loopIt && this.overrideMainSoundtrack) || !this.coolAudio.loopIt)
		{
			Links.x.gaia.playingSoundtrackFromInWorld = true;
			Links.x.gaia.FadeOutPlaylist(true, 0.5f);
		}
		float startValue = this.coolAudio.audioSource.volume;
		float num = Links.x.options.soundVolume * this.maxAudioVolume;
		float timeToLerp = 3f;
		float percentage = 0f;
		float startTime = Time.realtimeSinceStartup;
		while (percentage < 1f)
		{
			num = Links.x.options.soundVolume;
			num *= this.maxAudioVolume;
			percentage = (Time.realtimeSinceStartup - startTime) / timeToLerp;
			float num2 = Mathf.Lerp(startValue, num, percentage);
			this.coolAudio.audioSource.volume = num2;
			yield return null;
		}
		this.startAudio = null;
		yield break;
	}

	// Token: 0x06000214 RID: 532 RVA: 0x0002CAEE File Offset: 0x0002ACEE
	private void StartAudioEnd()
	{
		if (this.endAudio == null)
		{
			this.endAudio = this.AudioFadeOut();
			base.StartCoroutine(this.endAudio);
		}
	}

	// Token: 0x06000215 RID: 533 RVA: 0x0002CB11 File Offset: 0x0002AD11
	private IEnumerator AudioFadeOut()
	{
		this.playing = false;
		float startValue = this.coolAudio.audioSource.volume;
		float endValue = 0f;
		float timeToLerp = 3f;
		float percentage = 0f;
		float startTime = Time.realtimeSinceStartup;
		while (percentage < 1f)
		{
			percentage = (Time.realtimeSinceStartup - startTime) / timeToLerp;
			float num = Mathf.Lerp(startValue, endValue, percentage);
			this.coolAudio.audioSource.volume = num;
			yield return null;
		}
		if (Links.x.hasMain && ((this.coolAudio.loopIt && this.overrideMainSoundtrack) || !this.coolAudio.loopIt))
		{
			Links.x.gaia.playingSoundtrackFromInWorld = false;
			Links.x.gaia.SetEnvironmentPlaylist(Links.x.gameplay.sailing, Links.x.main.inTown, true);
		}
		this.inRange = false;
		this.coolAudio.audioSource.Stop();
		if (!this.coolAudio.playEveryTimePlayerIsNear)
		{
			float num2 = Random.Range(3f, 5f) * 60f;
			yield return new WaitForSeconds(num2);
		}
		this.endAudio = null;
		yield break;
	}

	// Token: 0x040003D7 RID: 983
	public CoolAudioSource coolAudio;

	// Token: 0x040003D8 RID: 984
	private float distance;

	// Token: 0x040003D9 RID: 985
	private float offDistance;

	// Token: 0x040003DA RID: 986
	private Vector3 position;

	// Token: 0x040003DB RID: 987
	public bool inRange;

	// Token: 0x040003DC RID: 988
	public bool playing;

	// Token: 0x040003DD RID: 989
	public bool keepPlayingThroughInterior;

	// Token: 0x040003DE RID: 990
	public bool listenForCharacter;

	// Token: 0x040003DF RID: 991
	public string characterListenName;

	// Token: 0x040003E0 RID: 992
	private IEnumerator startAudio;

	// Token: 0x040003E1 RID: 993
	private IEnumerator endAudio;

	// Token: 0x040003E2 RID: 994
	private float maxAudioVolume;

	// Token: 0x040003E3 RID: 995
	private Character character;

	// Token: 0x040003E4 RID: 996
	private bool overrideMainSoundtrack;
}
