using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using UnityEngine;
using wf;

public class SoundMgr : MonoBehaviour
{
	public AudioSourceMgr m_AudioDummyVoice { get; private set; }

	public AudioMixerMgr mix_mgr { get; private set; }

	public static float ConvertToAudioSourcePitch(int pitch)
	{
		return 1f + (0.3f * ((float)pitch * 0.01f) - 0.15f);
	}

	public int GetVolumeAll()
	{
		return this.m_nAllVol;
	}

	public void SetVolumeAll(int f_nVol)
	{
		this.m_nAllVol = System.Math.Min(System.Math.Max(0, f_nVol), 100);
	}

	public int GetVolume(AudioSourceMgr.Type f_eType)
	{
		return this.m_nVol[(int)f_eType];
	}

	public int GetVolumeDance()
	{
		return this.m_nDanceVol;
	}

	public void SetVolume(AudioSourceMgr.Type f_eType, int f_nVol)
	{
		this.m_nVol[(int)f_eType] = f_nVol;
	}

	public void SetVolumeDance(int f_nVol)
	{
		this.m_nDanceVol = f_nVol;
	}

	public bool GetThreeD(AudioSourceMgr.Type f_eType)
	{
		return this.m_bThreeD[(int)f_eType];
	}

	public void SetThreeD(AudioSourceMgr.Type f_eType, bool f_bEnable)
	{
		this.m_bThreeD[(int)f_eType] = f_bEnable;
	}

	public void Init(GameObject f_goParent)
	{
		this.m_goAudioMgr = UTY.GetChildObject(GameMain.Instance.MainCamera.gameObject, "AudioMgr", false);
		this.mix_mgr = Utility.CreatePrefab(this.m_goAudioMgr.gameObject, "System/AudioMixerMgr", true).GetComponent<AudioMixerMgr>();
		this.m_AudioFadeBufBgm = new SoundMgr.AudioFadeBuffer(AudioSourceMgr.Type.Bgm, "AudioBgm", 2, true, this, this.m_goAudioMgr);
		this.m_AudioFadeBufEnv = new SoundMgr.AudioFadeBuffer(AudioSourceMgr.Type.Env, "AudioEnv", 2, true, this, this.m_goAudioMgr);
		this.m_AudioSeBufSe = new SoundMgr.AudioMultiBuffer(AudioSourceMgr.Type.Se, "AudioSe", 12, true, this, this.m_goAudioMgr);
		this.m_trAudioSystem = new GameObject("AudioSet-System-AudioSystem").transform;
		this.m_trAudioSystem.SetParent(this.m_goAudioMgr.transform, false);
		this.m_AudioDummyVoice = this.AllocVoice(this.m_goAudioMgr.transform, true);
		this.SetVolumeAll(80);
		this.SetVolume(AudioSourceMgr.Type.Bgm, 35);
		this.SetVolume(AudioSourceMgr.Type.Se, 60);
		this.SetVolume(AudioSourceMgr.Type.System, 50);
		this.SetVolume(AudioSourceMgr.Type.Env, 50);
		this.SetVolume(AudioSourceMgr.Type.Voice, 80);
		this.SetVolumeDance(70);
		this.SetThreeD(AudioSourceMgr.Type.Bgm, false);
		this.SetThreeD(AudioSourceMgr.Type.Se, true);
		this.SetThreeD(AudioSourceMgr.Type.Env, true);
		this.SetThreeD(AudioSourceMgr.Type.Voice, true);
	}

	public void PlayBGM(string f_strFileName, float f_fTime, bool f_fLoop = true)
	{
		this.m_AudioFadeBufBgm.Play(f_strFileName, f_fTime, true, f_fLoop, null);
	}

	public void PlayBGMLegacy(string f_strFileName, float f_fTime, bool f_fLoop = true)
	{
		bool flag = this.compatibilityMode;
		this.compatibilityMode = true;
		this.m_AudioFadeBufBgm.Play(f_strFileName, f_fTime, true, f_fLoop, null);
		this.compatibilityMode = flag;
	}

	public void PlayDanceBGM(string f_strFileName, float f_fTime, bool f_fLoop = true)
	{
		this.m_AudioFadeBufBgm.PlayEx(f_strFileName, f_fTime, true, f_fLoop, null, true);
	}

	public AudioSource GetAudioSourceBgm()
	{
		return this.m_AudioFadeBufBgm.GetActiveAudioSource();
	}

	public void StopBGM(float f_fTime)
	{
		this.m_AudioFadeBufBgm.Stop(f_fTime);
	}

	public void PlayEnv(string f_strFileName, float f_fTime)
	{
		this.m_AudioFadeBufEnv.Play(f_strFileName, f_fTime, true, true, null);
	}

	public void StopEnv(float f_fTime)
	{
		this.m_AudioFadeBufEnv.Stop(f_fTime);
	}

	public void PlaySe(string f_strFileName, bool f_bLoop)
	{
		this.m_AudioSeBufSe.Play(f_strFileName, 0f, false, f_bLoop, null);
	}

	public void StopSe()
	{
		this.m_AudioSeBufSe.Stop(0f);
	}

	public void StopSe(string f_strFileName)
	{
		this.m_AudioSeBufSe.Stop(0f, f_strFileName);
	}

	public void PlaySystem(string f_strFileName)
	{
		string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(f_strFileName.ToLower());
		AudioSourceMgr audioSourceMgr;
		if (!this.m_dicAudioSystem.TryGetValue(fileNameWithoutExtension, out audioSourceMgr))
		{
			GameObject original = Resources.Load("System/Prefab/AudioSystem") as GameObject;
			GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(original);
			audioSourceMgr = gameObject.GetComponentsInChildren<AudioSourceMgr>(true)[0];
			audioSourceMgr.Init(AudioSourceMgr.Type.System, false, this, this.m_trAudioSystem.transform);
			audioSourceMgr.LoadFromWf(GameUty.FileSystem, f_strFileName, false);
			this.m_dicAudioSystem.Add(fileNameWithoutExtension, audioSourceMgr);
		}
		audioSourceMgr.PlayOneShot();
	}

	public void PlayDummyVoice(string f_strFileName, float f_fFadeTime = 0f, bool f_bStreaming = false, bool f_bLoop = false, int voice_pitch = 50)
	{
		this.m_AudioDummyVoice.Pitch = SoundMgr.ConvertToAudioSourcePitch(voice_pitch);
		this.m_AudioDummyVoice.LoadPlay(f_strFileName, f_fFadeTime, f_bStreaming, f_bLoop);
	}

	public void StopDummyVoide()
	{
		this.m_AudioDummyVoice.Stop();
	}

	public bool IsPlayDummyVoice()
	{
		return this.m_AudioDummyVoice.isPlay();
	}

	public void PlaySystem(SoundMgr.SeType f_eSe)
	{
		if (f_eSe != SoundMgr.SeType.Non && f_eSe != SoundMgr.SeType.Self)
		{
			this.PlaySystem(this.m_arySeFileName[(int)f_eSe]);
		}
	}

	public void StopSystem()
	{
		foreach (KeyValuePair<string, AudioSourceMgr> keyValuePair in this.m_dicAudioSystem)
		{
			keyValuePair.Value.Stop();
		}
	}

	public AudioSourceMgr AllocVoice(Transform f_trParent, bool f_bThreeD = true)
	{
		GameObject original = Resources.Load("System/Prefab/AudioVoice") as GameObject;
		GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(original);
		AudioSourceMgr audioSourceMgr = gameObject.GetComponentsInChildren<AudioSourceMgr>(true)[0];
		audioSourceMgr.Init(AudioSourceMgr.Type.Voice, f_bThreeD, this, f_trParent);
		this.m_listAudioManChara.Add(audioSourceMgr);
		return audioSourceMgr;
	}

	public void FreeVoice(AudioSourceMgr f_gcAudioMan, bool f_bDestoryMe)
	{
		this.m_listAudioManChara.Remove(f_gcAudioMan);
		f_gcAudioMan.Clear();
		if (f_bDestoryMe)
		{
			UnityEngine.Object.Destroy(f_gcAudioMan);
		}
		f_gcAudioMan = null;
	}

	public void StopAll()
	{
		this.VoiceStopAll();
		this.StopBGM(0f);
		this.StopEnv(0f);
		this.StopSe();
		this.StopSystem();
	}

	public void OnDestroy()
	{
		this.ClearAll();
		this.m_AudioFadeBufBgm = null;
		this.m_AudioFadeBufEnv = null;
		this.m_AudioSeBufSe = null;
		this.m_dicAudioSystem.Clear();
		this.m_listAudioManChara.Clear();
		this.m_AudioDummyVoice = null;
	}

	public void ClearAll()
	{
		if (this.m_AudioFadeBufBgm != null)
		{
			this.m_AudioFadeBufBgm.Clear();
		}
		if (this.m_AudioFadeBufEnv != null)
		{
			this.m_AudioFadeBufEnv.Clear();
		}
		if (this.m_AudioSeBufSe != null)
		{
			this.m_AudioSeBufSe.Clear();
		}
		foreach (KeyValuePair<string, AudioSourceMgr> keyValuePair in this.m_dicAudioSystem)
		{
			keyValuePair.Value.Clear();
		}
		for (int i = 0; i < this.m_listAudioManChara.Count; i++)
		{
			this.m_listAudioManChara[i].Clear();
		}
		if (this.m_AudioDummyVoice != null)
		{
			this.m_AudioDummyVoice.Clear();
		}
	}

	public bool isVoicePlaying()
	{
		bool flag = false;
		for (int i = 0; i < this.m_listAudioManChara.Count; i++)
		{
			flag |= this.m_listAudioManChara[i].isPlay();
		}
		return flag | this.m_AudioDummyVoice.isPlay();
	}

	public void VoiceStopAll()
	{
		for (int i = 0; i < this.m_listAudioManChara.Count; i++)
		{
			this.m_listAudioManChara[i].Stop();
		}
		this.m_AudioDummyVoice.Stop();
	}

	public void Apply()
	{
		this.mix_mgr.SetVolume(AudioMixerMgr.Group.Master, (float)this.m_nAllVol * 0.01f);
		this.mix_mgr.SetVolume(AudioMixerMgr.Group.BGM, (float)this.GetVolume(AudioSourceMgr.Type.Bgm) * 0.01f);
		this.mix_mgr.SetVolume(AudioMixerMgr.Group.Voice, (float)this.GetVolume(AudioSourceMgr.Type.Voice) * 0.01f);
		this.mix_mgr.SetVolume(AudioMixerMgr.Group.Dance, (float)this.GetVolumeDance() * 0.01f);
		this.mix_mgr.SetVolume(AudioMixerMgr.Group.System, (float)this.GetVolume(AudioSourceMgr.Type.System) * 0.01f);
		this.mix_mgr.SetVolume(AudioMixerMgr.Group.SE, (float)this.GetVolume(AudioSourceMgr.Type.Se) * 0.01f);
		for (int i = 0; i < this.m_listAudioManChara.Count; i++)
		{
			this.m_listAudioManChara[i].ApplyThreeD();
		}
		this.m_AudioDummyVoice.ApplyThreeD();
		this.m_AudioFadeBufBgm.Apply();
		this.m_AudioFadeBufEnv.Apply();
		this.m_AudioSeBufSe.Apply();
		foreach (KeyValuePair<string, AudioSourceMgr> keyValuePair in this.m_dicAudioSystem)
		{
			keyValuePair.Value.ApplyThreeD();
		}
	}

	public bool SaveIni(XElement f_xe)
	{
		XElement[] array = new XElement[5];
		XElement[] array2 = new XElement[5];
		for (int i = 0; i < 5; i++)
		{
			XElement[] array3 = array;
			int num = i;
			AudioSourceMgr.Type type = (AudioSourceMgr.Type)i;
			array3[num] = new XElement(type.ToString() + "Volume", this.GetVolume((AudioSourceMgr.Type)i));
			XElement[] array4 = array2;
			int num2 = i;
			AudioSourceMgr.Type type2 = (AudioSourceMgr.Type)i;
			array4[num2] = new XElement(type2.ToString() + "ThreeD", this.GetThreeD((AudioSourceMgr.Type)i));
		}
		XElement content = new XElement("Sound", new object[]
		{
			new XElement("VolumeAll", this.GetVolumeAll()),
			new XElement("VolumeDance", this.GetVolumeDance()),
			array,
			array2
		});
		f_xe.Add(content);
		return true;
	}

	public bool LoadIni(XElement f_xe)
	{
		XElement xelement = f_xe.Element("Sound");
		this.SetVolumeAll(int.Parse(xelement.Element("VolumeAll").Value));
		XElement xelement2 = xelement.Element("VolumeDance");
		if (xelement2 != null)
		{
			this.SetVolumeDance(int.Parse(xelement2.Value));
		}
		for (int i = 0; i < 5; i++)
		{
			AudioSourceMgr.Type f_eType = (AudioSourceMgr.Type)i;
			XContainer xcontainer = xelement;
			AudioSourceMgr.Type type = (AudioSourceMgr.Type)i;
			this.SetVolume(f_eType, int.Parse(xcontainer.Element(type.ToString() + "Volume").Value));
		}
		for (int j = 0; j < 5; j++)
		{
			AudioSourceMgr.Type f_eType2 = (AudioSourceMgr.Type)j;
			XContainer xcontainer2 = xelement;
			AudioSourceMgr.Type type2 = (AudioSourceMgr.Type)j;
			this.SetThreeD(f_eType2, bool.Parse(xcontainer2.Element(type2.ToString() + "ThreeD").Value));
		}
		return true;
	}

	public static Dictionary<string, AudioClip> dicAudioClip = new Dictionary<string, AudioClip>();

	private List<AudioSourceMgr> m_listAudioManChara = new List<AudioSourceMgr>();

	private SoundMgr.AudioFadeBuffer m_AudioFadeBufBgm;

	private SoundMgr.AudioFadeBuffer m_AudioFadeBufEnv;

	private SoundMgr.AudioMultiBuffer m_AudioSeBufSe;

	private Dictionary<string, AudioSourceMgr> m_dicAudioSystem = new Dictionary<string, AudioSourceMgr>();

	private Transform m_trAudioSystem;

	private GameObject m_goAudioMgr;

	private int m_nAllVol = 50;

	private int m_nDanceVol;

	private int[] m_nVol = new int[5];

	private bool[] m_bThreeD = new bool[5];

	public bool compatibilityMode;

	private string[] m_arySeFileName = new string[]
	{
		"SE001.ogg",
		"SE002.ogg",
		"SE003.ogg",
		"SE004.ogg",
		string.Empty,
		string.Empty
	};

	private abstract class AAudioMultiBuffer
	{
		public AAudioMultiBuffer(AudioSourceMgr.Type f_eType, string f_strPrefab, int f_nBufNum, bool f_bThreeD, SoundMgr f_gcSounMgr, GameObject f_goParent)
		{
			this.m_SoundMgr = f_gcSounMgr;
			this.m_aryAudioMan = new AudioSourceMgr[f_nBufNum];
			this.m_trParent = new GameObject("AudioSet-" + f_eType.ToString() + "-" + f_strPrefab).transform;
			this.m_trParent.SetParent(f_goParent.transform, false);
			for (int i = 0; i < f_nBufNum; i++)
			{
				GameObject original = Resources.Load("System/Prefab/" + f_strPrefab) as GameObject;
				GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(original);
				this.m_aryAudioMan[i] = gameObject.GetComponentsInChildren<AudioSourceMgr>(true)[0];
				this.m_aryAudioMan[i].Init(f_eType, f_bThreeD, f_gcSounMgr, this.m_trParent);
			}
		}

		public abstract void Play(string f_strFileName, float f_fTime, bool f_bLoop, bool f_bStreaming, Transform f_trAttachParent);

		public abstract void Stop(float f_fTime);

		public abstract void Apply();

		public virtual AudioSource GetActiveAudioSource()
		{
			return this.m_asActive;
		}

		public abstract string GetActivePlay();

		public virtual void Clear()
		{
			if (this.m_aryAudioMan != null)
			{
				foreach (AudioSourceMgr audioSourceMgr in this.m_aryAudioMan)
				{
					audioSourceMgr.Clear();
				}
			}
		}

		protected AudioSourceMgr[] m_aryAudioMan;

		protected SoundMgr m_SoundMgr;

		protected Transform m_trParent;

		protected AudioSource m_asActive;
	}

	private class AudioFadeBuffer : SoundMgr.AAudioMultiBuffer
	{
		public AudioFadeBuffer(AudioSourceMgr.Type f_eType, string f_strPrefab, int f_nBufNum, bool f_bThreeD, SoundMgr f_gcSounMgr, GameObject f_goParent) : base(f_eType, f_strPrefab, f_nBufNum, f_bThreeD, f_gcSounMgr, f_goParent)
		{
		}

		public override void Play(string f_strFileName, float f_fTime, bool f_bStreaming, bool f_bLoop, Transform f_trAttachParent)
		{
			this.PlayEx(f_strFileName, f_fTime, f_bStreaming, f_bLoop, f_trAttachParent, false);
		}

		public void PlayEx(string f_strFileName, float f_fTime, bool f_bStreaming, bool f_bLoop, Transform f_trAttachParent, bool f_bDance)
		{
			if (this.m_aryAudioMan[this.m_nBgmActiveNo].isPlay() && this.m_aryAudioMan[this.m_nBgmActiveNo].FileName == f_strFileName && this.m_aryAudioMan[this.m_nBgmActiveNo].isLastPlayCompatibilityMode == this.m_SoundMgr.compatibilityMode)
			{
				Debug.Log("同じファイル名が再生中なので再生を拒否:" + f_strFileName);
				return;
			}
			this.m_aryAudioMan[this.m_nBgmActiveNo].Stop(f_fTime);
			int num = -1;
			int num2 = -1;
			float num3 = 1f;
			for (int i = 0; i < this.m_aryAudioMan.Length; i++)
			{
				if (this.m_nBgmActiveNo != i)
				{
					AudioSourceMgr audioSourceMgr = this.m_aryAudioMan[i];
					if (!audioSourceMgr.isPlay())
					{
						num = i;
					}
					if (audioSourceMgr.FadeVolume <= num3)
					{
						num3 = audioSourceMgr.FadeVolume;
						num2 = i;
					}
				}
			}
			if (num == -1)
			{
				if (num2 == -1)
				{
					for (int j = 0; j < this.m_aryAudioMan.Length; j++)
					{
						if (this.m_nBgmActiveNo != j)
						{
							this.m_nBgmActiveNo = j;
							break;
						}
					}
				}
				else
				{
					this.m_nBgmActiveNo = num2;
				}
			}
			else
			{
				this.m_nBgmActiveNo = num;
			}
			AudioSourceMgr audioSourceMgr2 = this.m_aryAudioMan[this.m_nBgmActiveNo];
			audioSourceMgr2.Stop();
			if (f_bDance)
			{
				audioSourceMgr2.LoadPlayDanceBGM(f_strFileName, f_fTime, f_bStreaming, f_bLoop);
			}
			else
			{
				audioSourceMgr2.LoadPlay(f_strFileName, f_fTime, f_bStreaming, f_bLoop);
			}
			audioSourceMgr2.SetParent(f_trAttachParent);
			this.m_asActive = audioSourceMgr2.GetComponent<AudioSource>();
		}

		public override void Stop(float f_fTime)
		{
			for (int i = 0; i < this.m_aryAudioMan.Length; i++)
			{
				this.m_aryAudioMan[i].Stop(f_fTime);
			}
		}

		public override void Apply()
		{
			for (int i = 0; i < this.m_aryAudioMan.Length; i++)
			{
				this.m_aryAudioMan[i].ApplyThreeD();
			}
		}

		public override string GetActivePlay()
		{
			return this.m_aryAudioMan[this.m_nBgmActiveNo].FileName;
		}

		private int m_nBgmActiveNo;
	}

	private class AudioMultiBuffer : SoundMgr.AAudioMultiBuffer
	{
		public AudioMultiBuffer(AudioSourceMgr.Type f_eType, string f_strPrefab, int f_nBufNum, bool f_bThreeD, SoundMgr f_gcSounMgr, GameObject f_goParent) : base(f_eType, f_strPrefab, f_nBufNum, f_bThreeD, f_gcSounMgr, f_goParent)
		{
			this.m_aryTime = new float[f_nBufNum];
		}

		public override void Play(string f_strFileName, float f_fTime, bool f_bStreaming, bool f_bLoop, Transform f_trAttachParent)
		{
			int num = -1;
			int num2 = -1;
			float num3 = Time.time;
			for (int i = 0; i < this.m_aryAudioMan.Length; i++)
			{
				AudioSourceMgr audioSourceMgr = this.m_aryAudioMan[i];
				if (audioSourceMgr.isPlay() && audioSourceMgr.isLoop())
				{
					if (audioSourceMgr.FileName == f_strFileName)
					{
						return;
					}
				}
				else
				{
					if (!audioSourceMgr.isPlay())
					{
						num = i;
					}
					if (this.m_aryTime[i] < num3)
					{
						num3 = this.m_aryTime[i];
						num2 = i;
					}
				}
			}
			int num4;
			if (num == -1)
			{
				if (num2 == -1)
				{
					num4 = UnityEngine.Random.Range(0, this.m_aryAudioMan.Length);
				}
				else
				{
					num4 = num2;
				}
			}
			else
			{
				num4 = num;
			}
			this.m_aryAudioMan[num4].LoadPlay(f_strFileName, f_fTime, f_bStreaming, f_bLoop);
			this.m_aryAudioMan[num4].SetParent(f_trAttachParent);
			this.m_aryTime[num4] = Time.time;
			this.m_asActive = this.m_aryAudioMan[num4].GetComponent<AudioSource>();
		}

		public override void Stop(float f_fTime)
		{
			for (int i = 0; i < this.m_aryAudioMan.Length; i++)
			{
				this.m_aryAudioMan[i].Stop(f_fTime);
			}
		}

		public void Stop(float f_fTime, string f_strFileName)
		{
			for (int i = 0; i < this.m_aryAudioMan.Length; i++)
			{
				AudioSourceMgr audioSourceMgr = this.m_aryAudioMan[i];
				if (audioSourceMgr.isPlay() && audioSourceMgr.FileName == f_strFileName)
				{
					audioSourceMgr.Stop(f_fTime);
				}
			}
		}

		public override void Apply()
		{
			for (int i = 0; i < this.m_aryAudioMan.Length; i++)
			{
				this.m_aryAudioMan[i].ApplyThreeD();
			}
		}

		public override string GetActivePlay()
		{
			return string.Empty;
		}

		private float[] m_aryTime;
	}

	public enum SeType
	{
		Click,
		IClick,
		Cancel,
		Hover,
		Self,
		Non
	}
}
