using System;
using System.Collections.Generic;
using CriWare;
using Cute;
using Wizard;

public class Bgm
{
	public enum BGM_TYPE
	{
		NONE,
		TITLE,
		TITLE_SPECIAL_1,
		TITLE_SPECIAL_2,
		HOME,
		BATTLE_STANDBY,
		BATTLE,
		SYS_WIN_LOOP,
		SYS_LOSE_LOOP,
		COLOSSEUM_FINAL,
		GRANDPRIX_SPECIAL,
		GRANDPRIX_SPECIAL_FINAL,
		SEALED,
		QUEST,
		COMPETITION_LOBBY,
		BINGO,
		MAX
	}

	private class BgmInfo
	{
		public string CueName { get; private set; }

		public string CueSheet { get; private set; }

		public BgmInfo(string cueName, string cueSheet)
		{
			CueName = cueName;
			CueSheet = cueSheet;
		}
	}

	private const string CATEGORY_NAME = "BGM";

	private Dictionary<BGM_TYPE, BgmInfo> m_AudioData;

	private string m_PlayBgmCueName;

	private int m_CurrentBgmIdx;

	private int m_MaxBgmCount;

	private float m_volume;

	private bool m_isMuted;

	public Bgm()
	{
		m_AudioData = new Dictionary<BGM_TYPE, BgmInfo>();
		m_PlayBgmCueName = null;
		m_CurrentBgmIdx = 0;
		m_MaxBgmCount = Toolbox.AudioManager.GetBgmMaxCount();
		SetVolume(PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.BGM_VOLUME));
		Mute(PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.SOUND_MUTE));
	}

	public void AddList(BGM_TYPE BgmType, string cueName, string cueSheet = null)
	{
		if (!m_AudioData.ContainsKey(BgmType))
		{
			m_AudioData.Add(BgmType, new BgmInfo(cueName, (cueSheet == null) ? cueName : cueSheet));
		}
	}

	public void RemoveList(BGM_TYPE BgmType)
	{
		if (m_AudioData.ContainsKey(BgmType))
		{
			m_AudioData.Remove(BgmType);
		}
	}

	public virtual void Play(BGM_TYPE BgmType, float FadeTime = 0f, long startTime = 0L)
	{
		if (BgmType != BGM_TYPE.NONE)
		{
			BgmInfo bgmInfo = m_AudioData[BgmType];
			string cueSheet = bgmInfo.CueSheet;
			m_PlayBgmCueName = bgmInfo.CueName;
			Toolbox.AudioManager.PlayBgmFromName(cueSheet, m_PlayBgmCueName, cueSheet, cueSheet, m_CurrentBgmIdx, loop: false, FadeTime, 0f, startTime);
		}
	}

	public virtual void PlayCrossFade(BGM_TYPE BgmType, float fadeOutTime, float fadeInTime, long startTime)
	{
		if (BgmType != BGM_TYPE.NONE)
		{
			BgmInfo bgmInfo = m_AudioData[BgmType];
			string cueSheet = bgmInfo.CueSheet;
			m_PlayBgmCueName = bgmInfo.CueName;
			Toolbox.AudioManager.StopFadeBgm(m_CurrentBgmIdx, fadeOutTime);
			m_CurrentBgmIdx = (m_CurrentBgmIdx + 1) % m_MaxBgmCount;
			Toolbox.AudioManager.PlayBgmFromName(cueSheet, m_PlayBgmCueName, cueSheet, cueSheet, m_CurrentBgmIdx, loop: false, fadeInTime, 0f, startTime);
		}
	}

	public virtual void PlayFadeOut(BGM_TYPE BgmType, float FadeOutTime = 0f, float FadeInTime = 0f)
	{
		Toolbox.AudioManager.StopFadeBgm(m_CurrentBgmIdx, FadeOutTime);
		if (BgmType != BGM_TYPE.NONE)
		{
			m_CurrentBgmIdx = (m_CurrentBgmIdx + 1) % m_MaxBgmCount;
			BgmInfo bgmInfo = m_AudioData[BgmType];
			string cueSheet = bgmInfo.CueSheet;
			m_PlayBgmCueName = bgmInfo.CueName;
			Toolbox.AudioManager.PlayBgmFromName(cueSheet, m_PlayBgmCueName, cueSheet, cueSheet, m_CurrentBgmIdx, loop: false, FadeInTime, FadeOutTime, 0L);
		}
	}

	public virtual void Play(string cuename, float fadeInTime, long startTime)
	{
		if (IsPreInstall(cuename, out var type))
		{
			Play(type, fadeInTime, startTime);
			return;
		}
		m_PlayBgmCueName = cuename;
		Toolbox.AudioManager.PlayBgmFromName(cuename, cuename, cuename, cuename, m_CurrentBgmIdx, loop: false, fadeInTime, 0f, startTime);
	}

	public virtual void PlayCrossFade(string cuename, float fadeOutTime, float fadeInTime, long startTime)
	{
		if (IsPreInstall(cuename, out var type))
		{
			PlayCrossFade(type, fadeOutTime, fadeInTime, startTime);
			return;
		}
		m_PlayBgmCueName = cuename;
		Toolbox.AudioManager.StopFadeBgm(m_CurrentBgmIdx, fadeOutTime);
		m_CurrentBgmIdx = (m_CurrentBgmIdx + 1) % m_MaxBgmCount;
		Toolbox.AudioManager.PlayBgmFromName(cuename, cuename, cuename, cuename, m_CurrentBgmIdx, loop: false, fadeInTime, 0f, startTime);
	}

	public virtual void Pause()
	{
		for (int i = 0; i < m_MaxBgmCount; i++)
		{
			Toolbox.AudioManager.PauseBgm(isPause: true, i);
		}
	}

	public virtual void Stop(float FadeTime, Action onStopped)
	{
		m_PlayBgmCueName = null;
		AudioManager audioManager = Toolbox.AudioManager;
		audioManager.StopFadeBgm(m_CurrentBgmIdx, FadeTime);
		audioManager.StartCoroutine_DelayMethod(FadeTime, onStopped);
	}

	public virtual void StopAll(float FadeTime)
	{
		m_PlayBgmCueName = null;
		for (int i = 0; i < m_MaxBgmCount; i++)
		{
			Toolbox.AudioManager.StopFadeBgm(i, FadeTime);
		}
	}

	public bool IsPlayBGM(BGM_TYPE bgmType)
	{
		if (bgmType != BGM_TYPE.NONE)
		{
			return m_AudioData[bgmType].CueName == m_PlayBgmCueName;
		}
		return m_PlayBgmCueName != null;
	}

	public string GetCueSheet(BGM_TYPE bgmType)
	{
		return m_AudioData[bgmType].CueSheet;
	}

	public bool IsPreInstall(string cuename, out BGM_TYPE type)
	{
		foreach (KeyValuePair<BGM_TYPE, BgmInfo> audioDatum in m_AudioData)
		{
			BgmInfo value = audioDatum.Value;
			if (value.CueName == cuename && value.CueSheet == SoundMgr.PREINSTALL_CUESHEET)
			{
				type = audioDatum.Key;
				return true;
			}
		}
		type = BGM_TYPE.NONE;
		return false;
	}

	public void SetVolume(float Prm)
	{
		CriAtomExCategory.SetVolume("BGM", Prm);
		m_volume = Prm;
	}

	public void Mute(bool isMute)
	{
		CriAtomExCategory.Mute("BGM", isMute);
		m_isMuted = isMute;
	}

	public float GetVolume()
	{
		return m_volume;
	}

	public bool IsMuted()
	{
		return m_isMuted;
	}
}
