using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BjVoiceMgr : MonoBehaviour
{
	public static BjVoiceMgr Instance { get; private set; }

	private Maid m_TargetMaid
	{
		get
		{
			return GameMain.Instance.CharacterMgr.GetMaid(0);
		}
	}

	public bool isPlay
	{
		get
		{
			return this.m_TargetMaid.AudioMan.isPlay();
		}
	}

	private void Awake()
	{
		BjVoiceMgr.Instance = this;
		KasaiUtility.CsvReadY("bj_voice_data.nei", new Action<CsvParser, int>(this.ReadVoiceData), 1, null);
	}

	private void Start()
	{
		if (CasinoDataMgr.Instance.DealerMaid)
		{
			string label_name = string.Empty;
			if (GameMain.Instance.ScriptMgr.adv_kag.tag_backup.ContainsKey("voice_label"))
			{
				label_name = GameMain.Instance.ScriptMgr.adv_kag.tag_backup["voice_label"];
			}
			GameMain.Instance.ScriptMgr.adv_kag.JumpLabel(label_name);
		}
		else
		{
			string file_name = ScriptManager.ReplacePersonal(this.m_TargetMaid, this.m_VoiceScript);
			GameMain.Instance.ScriptMgr.LoadAdvScenarioScript(file_name, string.Empty);
		}
		GameMain.Instance.ScriptMgr.adv_kag.Exec();
	}

	private void ReadVoiceData(CsvParser csv, int cy)
	{
		this.m_VoiceData.Add(csv.GetCellAsString(0, cy), csv.GetCellAsString(1, cy));
	}

	private IEnumerator VoiceWait(Action end_call)
	{
		yield return null;
		while (this.m_TargetMaid.AudioMan.isPlay())
		{
			yield return null;
		}
		if (end_call != null)
		{
			end_call();
		}
		yield break;
	}

	private void Recet()
	{
		this.m_TargetMaid.FaceBlend("無し");
		this.m_TargetMaid.FaceAnime("通常", 1f, 0);
		GameMain.Instance.ScriptMgr.adv_kag.ClearExecWait();
	}

	public void PlayScoreVoice(Action end_call = null, float fade_time = 0f)
	{
		if (Dealer.Instance.IsNatural21())
		{
			this.PlayVoice("ディーラーBJ", end_call, fade_time);
		}
		else if (Dealer.Instance.IsBust())
		{
			this.PlayVoice("ディーラーバースト", end_call, fade_time);
		}
		else
		{
			this.PlayVoice(Dealer.Instance.CurrentScore.ToString(), end_call, fade_time);
		}
	}

	public void PlayVoice(string voice_category, Action end_call = null, float fade_time = 0f)
	{
		if (!this.m_VoiceData.ContainsKey(voice_category))
		{
			return;
		}
		GameMain.Instance.ScriptMgr.adv_kag.ClearExecWait();
		GameMain.Instance.ScriptMgr.adv_kag.JumpLabel(this.m_VoiceData[voice_category]);
		GameMain.Instance.ScriptMgr.adv_kag.Exec();
		if (end_call != null)
		{
			base.StartCoroutine(this.VoiceWait(end_call));
		}
	}

	public void VoiceToVoice(string first_voice, string second_voice, Action end_call, float first_fade = 0f, float second_fade = 0f)
	{
		this.PlayVoice(first_voice, delegate
		{
			this.PlayVoice(second_voice, end_call, second_fade);
		}, first_fade);
	}

	public void Stop()
	{
		this.Recet();
		base.StopAllCoroutines();
		if (this.m_TargetMaid && this.m_TargetMaid.AudioMan)
		{
			this.m_TargetMaid.AudioMan.Stop();
		}
	}

	public void VoiceEnd()
	{
		this.Recet();
		GameMain.Instance.ScriptMgr.adv_kag.JumpLabel(this.m_EndLabel);
		GameMain.Instance.ScriptMgr.adv_kag.Exec();
	}

	private Dictionary<string, string> m_VoiceData = new Dictionary<string, string>();

	[SerializeField]
	[Header("ボイス再生用のスクリプトファイル")]
	private string m_VoiceScript = "?_casino_0001.ks";

	[SerializeField]
	[Header(" ボイス用スクリプトから抜けるときのラベル")]
	private string m_EndLabel = "*カジノうひょー";
}
