context: using System;
  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  using Frontiers;
  using Frontiers.World;
  
  namespace Frontiers
  {
  	public class AudioManager : Manager
  	{
  		public static AudioManager Get;
  		public AudioListener CurrentAudioListener;
  		public MusicType CurrentMusic;
  		public MusicType LastMusic;
  		public MusicVolume CurrentVolume;
  		public AmbientAudioManager AmbientAudio;
  		public MusicPlaylist PlayList = new MusicPlaylist ();
  		public MasterAudio Audio;
  		public AmbientAudioManager.ChunkAudioSettings CurrentAudioSettings = new AmbientAudioManager.ChunkAudioSettings ();
  		public List <AmbientAudioClip> AmbientAudioClips = new List <AmbientAudioClip> ();
  
  		public float FadeInAudioTargetVolume {
  			get {
  				return RawFadeInAudioTargetVolume * Profile.Get.CurrentPreferences.Sound.Music;
  			}
  			set {
  				RawFadeInAudioTargetVolume = value;
  			}
  		}
  
  		public float RawFadeInAudioTargetVolume;
  
  		public AudioSource ActiveMusic {
  			get {
  				if (Music1.clip != null && Music1.isPlaying && Music1.volume > 0f) {
  					return Music1;
  				} else if (Music2.clip != null && Music2.isPlaying) {
  					return Music2;
  				}
  				return null;
  			}
  		}
  
  		public AudioSource Music1;
  		public AudioSource Music2;
  		public AudioSource FadingInAudio;
  		public AudioSource FadingOutAudio;
  		//public WWW loader;
  		public override void OnModsLoadFinish ()
  		{
  			PlayList.Combat = GameWorld.Get.Settings.DefaultMusicCombat;
  			PlayList.Cutscene = GameWorld.Get.Settings.DefaultMusicCutscene;
  			PlayList.MainMenu = GameWorld.Get.Settings.DefaultMusicMainMenu;
  			PlayList.Night = GameWorld.Get.Settings.DefaultMusicNight;
  			PlayList.Regional = GameWorld.Get.Settings.DefaultMusicRegional;
  			PlayList.SafeLocation = GameWorld.Get.Settings.DefaultMusicSafeLocation;
  			PlayList.Underground = GameWorld.Get.Settings.DefaultMusicUnderground;
  			mModsLoaded = true;
  		}
  
  		public static void MakeWorldSound (IItemOfInterest source, MasterAudio.SoundType soundType, string soundName)
  		{
  			IAudible audibleSource = (IAudible)source.gameObject.GetComponent (typeof(IAudible));
  			if (audibleSource != null) {
  				MakeWorldSound (audibleSource, soundType, soundName);
  			}
  		}
  
  		public static void MakeWorldSound (IAudible source, MasterAudio.SoundType soundType, string soundName)
  		{
  			//use master audio to create a sound
  			//attach an audible bubble to the source
  			AudibleBubble audibleBubble = null;
  			AudioSource sourceAudio = null;
  			if (MasterAudio.PlaySound (soundType, source.transform, source.AudibleVolume, ref sourceAudio, soundName)) {
  				source.LastSoundType = soundType;
  				source.LastSoundName = soundName;
  				audibleBubble = sourceAudio.gameObject.GetOrAdd <AudibleBubble> ();
  				audibleBubble.StartUsing (source, null, 0.25f);
  			}
  		}
  
  		public static void MakeWorldSound (IAudible source, List <Collider> ignoreColliders, MasterAudio.SoundType soundType, string soundName)
  		{
  			//use master audio to create a sound
  			//attach an audible bubble to the source
  			AudibleBubble audibleBubble = null;
  			AudioSource sourceAudio = null;
  			if (MasterAudio.PlaySound (soundType, source.transform, source.AudibleVolume, ref sourceAudio, soundName)) {
  				source.LastSoundType = soundType;
  				source.LastSoundName = soundName;
  				audibleBubble = sourceAudio.gameObject.GetOrAdd <AudibleBubble> ();
  				audibleBubble.StartUsing (source, ignoreColliders, 0.25f);
  			}
  		}
  
  		public static void MakeWorldSound (IAudible Source, List <Collider> ignoreColliders, MasterAudio.SoundType SoundType)
  		{
  			//use master audio to create a sound
  			//attach an audible bubble to the source
  			AudibleBubble audibleBubble = null;
  			AudioSource sourceAudio = null;
  			if (MasterAudio.PlaySound (SoundType, Source.transform, Source.AudibleVolume, ref sourceAudio)) {
  				Source.LastSoundType = SoundType;
  				audibleBubble = sourceAudio.gameObject.GetOrAdd <AudibleBubble> ();
  				audibleBubble.StartUsing (Source, ignoreColliders, 0.25f);
  			}
  		}
  
  		public string [] AudioClipNames {
  			get {
  				if (audioClipNames == null) {
  					List <string> audioClipNameList = new List <string> ();
  					for (int i = 0; i < AmbientAudioClips.Count; i++) {
  						audioClipNameList.Add (AmbientAudioClips [i].Key);
  					}
  					for (int i = 0; i < Audio.musicPlaylist.Count; i++) {
  						if (Audio.musicPlaylist [i].clip != null) {
  							audioClipNameList.Add (Audio.musicPlaylist [i].clip.name);
  						}
  					}
  					audioClipNames = audioClipNameList.ToArray ();
  				}
  				return audioClipNames;
  			}
  		}
  
  		public float MasterAmbientVolume {
  			get { return Hydrogen.Core.AudioStackItem.MasterVolume; }
  			set {
  				Hydrogen.Core.AudioStackItem.MasterVolume = value;
  			}
  		}
  
  		public float MasterMusicVolume {
  			get { return mMasterMusicVolume; }
  			set {
  				mMasterMusicVolume = value;
  				if (Music1.volume != 0f) {
  					Music1.volume = mMasterMusicVolume;
  				}
  				if (Music2.volume != 0f) {
  					Music2.volume = mMasterMusicVolume;
  				}
  			}
  		}
  
  		public override void WakeUp ()
  		{
  			base.WakeUp ();
  
  			FadingInAudio = null;
  			FadingOutAudio = null;
  			Get = this;
  			audioClipNames = null;
  			if (Application.isPlaying) {
  				foreach (AmbientAudioClip clip in AmbientAudioClips) {	//build the lookup table for audio clips
  					mAudioClipLookup.Add (clip.Key, clip.Clip);
  				}
  			}
  		}
  
  		public override void OnInitialized ()
  		{
  			WorldClock.Get.TimeActions.Subscribe (TimeActionType.DaytimeStart, new ActionListener (DaytimeChange));
  			WorldClock.Get.TimeActions.Subscribe (TimeActionType.NightTimeStart, new ActionListener (DaytimeChange));
  			Player.Get.AvatarActions.Subscribe (AvatarAction.LocationStructureEnter, new ActionListener (LocationStructureEnterOrExit));
  			Player.Get.AvatarActions.Subscribe (AvatarAction.LocationStructureExit, new ActionListener (LocationStructureEnterOrExit));
  			Player.Get.AvatarActions.Subscribe (AvatarAction.LocationUndergroundEnter, new ActionListener (LocationUndergroundEnterOrExit));
  			Player.Get.AvatarActions.Subscribe (AvatarAction.LocationUndergroundExit, new ActionListener (LocationUndergroundEnterOrExit));
  			Player.Get.AvatarActions.Subscribe (AvatarAction.SurvivalDangerExit, new ActionListener (DangerExit));
  			Player.Get.AvatarActions.Subscribe (AvatarAction.SurvivalDangerEnter, new ActionListener (DangerEnter));
  
  			CurrentMusic = MusicType.MainMenu;
  			CurrentVolume = MusicVolume.Default;
  			//StartCoroutine (PlayMusic (CurrentMusic, CurrentVolume));
  		}
  
  		public void Start ()
  		{
  			if (Application.isPlaying) {
  				StartCoroutine (PlayMusic (MusicType.MainMenu, MusicVolume.Default));
  			}
  		}
  
  		public override void OnGameStart ()
  		{
  			GetAmbientAudioSettings ();
  			AmbientAudio.ChunkSettings = CurrentAudioSettings;
  			AmbientAudio.UpdateStackVolumes (Colors.Alpha (Color.black, 0f));
  			AmbientAudio.ClearAudio ();
  			MasterAmbientVolume = 0f;
  		}
  
  		public override void OnLocalPlayerDespawn ()
  		{
  			GetAmbientAudioSettings ();
  			AmbientAudio.ChunkSettings = CurrentAudioSettings;
  			AmbientAudio.UpdateStackVolumes (Colors.Alpha (Color.black, 0f));
  			AmbientAudio.ClearAudio ();
  			MasterAmbientVolume = 0f;
  		}
  
  		public override void OnLocalPlayerSpawn ()
  		{
  			GetAmbientAudioSettings ();
  			AmbientAudio.ChunkSettings = CurrentAudioSettings;
  			MasterAmbientVolume = Profile.Get.CurrentPreferences.Sound.Ambient;
  			UpdateMusicState ();
  			AmbientAudio.ChunkSettings = CurrentAudioSettings;
  			if (!mUpdatingAmbientState) {
  				mUpdatingAmbientState = true;
  				StartCoroutine (UpdateAmbientStateOverTime ());
  			}
  			if (!mUpdatingMusicState) {
  				mUpdatingMusicState = true;
  				StartCoroutine (UpdateMusicStateOverTime ());
  			}
  			AmbientAudio.IsDaytime = WorldClock.IsDay;
  			AmbientAudio.IsInsideStructure = false;
  			AmbientAudio.IsUnderground = Player.Local.Surroundings.IsUnderground;
  			AmbientAudio.UpdateStackVolumes (Player.Local.Surroundings.TerrainType);
  		}
  
  		public override void OnCutsceneStart ()
  		{
  			if (Cutscene.CurrentCutscene.UseCutsceneMusic) {
  				PlayList.Cutscene = Cutscene.CurrentCutscene.CutsceneMusic;
  				PlayMusic (MusicType.Cutscene, MusicVolume.Default);
  			}
  		}
  
  		public override void OnCutsceneFinished ()
  		{
  			UpdateMusicState ();
  		}
  
  		protected IEnumerator UpdateAmbientStateOverTime ()
  		{
  			while (!GameManager.Is (FGameState.Quitting)) {
  				double waitUntil = WorldClock.RealTime + mUpdateAmbientInterval;
  				while (WorldClock.RealTime < waitUntil) {
  					yield return null;
  				}
  				UpdateAmbientState ();
  			}
  			yield break;
  		}
  
  		protected IEnumerator UpdateMusicStateOverTime ()
  		{
  			while (!GameManager.Is (FGameState.Quitting)) {
  				double waitUntil = WorldClock.RealTime + mUpdateMusicInterval;
  
gt: 				while (WorldClock.RealTime < waitUntil) {