using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using wf;

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

	private void Awake()
	{
		UIStates.Instance = this;
		this.m_ButtonsEnabled = true;
		this.AngleDisabled();
		this.m_NextButton = this.m_ResultUI.GetComponentInChildren<Button>();
		this.m_ResultImage = this.m_ResultUI.transform.Find("Result");
		this.m_GetMoneyText = this.m_ResultUI.transform.Find("GetMoneyText").GetComponent<Text>();
		this.m_CurrentMouneyText = this.m_ResultUI.transform.Find("BG/CurrentMoney").GetComponent<Text>();
		this.m_ResultCanvas = this.m_ResultUI.GetComponent<CanvasGroup>();
	}

	private void Start()
	{
		this.m_ResultUI.SetActive(false);
		ExChangeUI instance = ExChangeUI.Instance;
		instance.FadeInStartAction = (Action)Delegate.Combine(instance.FadeInStartAction, new Action(delegate()
		{
			this.EnableButton(this.m_StartButton, false);
			this.EnableButton(this.m_EndButton, false);
		}));
		BetSetUI instance2 = BetSetUI.Instance;
		instance2.CancelCall = (Action)Delegate.Combine(instance2.CancelCall, new Action(delegate()
		{
			this.EnableButton(this.m_StartButton, true);
			this.EnableButton(this.m_EndButton, true);
		}));
		ExChangeUI instance3 = ExChangeUI.Instance;
		instance3.FadeOutStartAction = (Action)Delegate.Combine(instance3.FadeOutStartAction, new Action(this.OnStateChange));
	}

	public void StateReset()
	{
		this.OnStateChange();
		this.HideResultText();
	}

	public void SetEnabled(bool enabled)
	{
		this.m_ButtonsEnabled = enabled;
		if (!enabled)
		{
			this.EnableButton(this.m_DoubleDownButton, false);
			this.EnableButton(this.m_StandButton, false);
			this.EnableButton(this.m_SplitButton, false);
			this.EnableButton(this.m_SurrenderButton, false);
			this.EnableButton(this.m_HitButton, false);
			this.EnableButton(this.m_ExchangeButton, false);
			this.EnableButton(this.m_EndButton, false);
			this.EnableButton(this.m_StartButton, false);
		}
	}

	public void OnStateChange()
	{
		this.CheckBetText();
		this.CheckHit();
		this.CheckStand();
		this.CheckDoubleDown();
		this.CheckSplit();
		this.CheckCredits();
		this.CheckSurrender();
		this.CheckBetSettingUI();
	}

	public void ShowOutcomeText()
	{
		this.CheckResultText();
	}

	private void CheckHit()
	{
		if (BjPlayer.Instance.CanHit() && this.m_ButtonsEnabled)
		{
			this.EnableButton(this.m_HitButton, true);
		}
		else
		{
			this.EnableButton(this.m_HitButton, false);
		}
	}

	private void CheckStand()
	{
		if (BjPlayer.Instance.CanHit() && this.m_ButtonsEnabled)
		{
			this.EnableButton(this.m_StandButton, true);
		}
		else
		{
			this.EnableButton(this.m_StandButton, false);
		}
	}

	private void CheckDoubleDown()
	{
		if (this.m_ButtonsEnabled && !BjPlayer.Instance.IsEnded() && BjPlayer.Instance.DoubleDownAvailable())
		{
			this.EnableButton(this.m_DoubleDownButton, true);
		}
		else
		{
			this.EnableButton(this.m_DoubleDownButton, false);
		}
	}

	private void CheckSplit()
	{
		if (BjPlayer.Instance.SplitAvailable() && this.m_ButtonsEnabled)
		{
			this.EnableButton(this.m_SplitButton, true);
		}
		else
		{
			this.EnableButton(this.m_SplitButton, false);
		}
	}

	private void CheckCredits()
	{
		ExChangeUI.Instance.TextUIUpdate();
	}

	private void CheckSurrender()
	{
		if (this.m_ButtonsEnabled && BjPlayer.Instance.CanSurrender())
		{
			this.EnableButton(this.m_SurrenderButton, true);
		}
		else
		{
			this.EnableButton(this.m_SurrenderButton, false);
		}
	}

	private void CheckBetText()
	{
		if (BjPlayer.Instance.CurrentBet == 0L || !BetSetUI.Instance.IsDoBet)
		{
			this.m_BetText.text = string.Empty;
		}
		else
		{
			this.m_BetText.text = BjPlayer.Instance.CurrentBet.ToString();
		}
		if (BjPlayer.Instance.SplitBet == 0L || !BetSetUI.Instance.IsDoBet)
		{
			this.m_SplitBetText.text = string.Empty;
		}
		else
		{
			this.m_SplitBetText.text = BjPlayer.Instance.SplitBet.ToString();
		}
	}

	private void CheckBetSettingUI()
	{
		BetSetUI.Instance.UpdateUIState();
		ExChangeUI.Instance.UIStateUpdate();
		bool flag = BjPlayer.Instance.IsIdle() && !BetSetUI.Instance.Active && this.m_ButtonsEnabled;
		this.m_StartButton.interactable = flag;
		this.m_EndButton.interactable = flag;
		ExChangeUI.Instance.SetButtonState(flag);
		BetSetUI.Instance.SetBetMax();
	}

	private void SetResultImage(string image_name)
	{
		IEnumerator enumerator = this.m_ResultImage.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				Transform transform = (Transform)obj;
				transform.gameObject.SetActive(transform.name == image_name);
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
	}

	private void CheckResultText()
	{
		if (this.m_ResultUI.activeSelf)
		{
			this.HideResultText();
			return;
		}
		this.m_ResultUI.SetActive(true);
		if (BjPlayer.Instance.TotalWinnings > 0L)
		{
			if (BjPlayer.Instance.ExistSurrenderHand())
			{
				BjVoiceMgr.Instance.PlayVoice("サレンダー終了", null, 0f);
				this.m_GetMoneyText.text = "コインを" + BjPlayer.Instance.TotalWinnings.ToString() + "枚支払いました";
				this.SetResultImage(Translations.Instance.SURRENDER);
			}
			else if (BjPlayer.Instance.ExistWinHand())
			{
				this.m_GetMoneyText.text = "コインを" + BjPlayer.Instance.TotalWinnings.ToString() + "枚手に入れました!!";
				if (BjPlayer.Instance.ExistBlackJack())
				{
					this.SetResultImage(Translations.Instance.BLACKJACK);
					BjVoiceMgr.Instance.PlayVoice("ブラックジャック勝ち", null, 0f);
				}
				else
				{
					BjVoiceMgr.Instance.PlayVoice("勝利", null, 0f);
					if (BjPlayer.Instance.ExistDoubleDownwWin())
					{
						this.SetResultImage(Translations.Instance.BIG_WINNINGS);
					}
					else
					{
						this.SetResultImage(Translations.Instance.WINNINGS);
					}
				}
				BjMotionControl.Instance.PlayWinMotion(BjMotionControl.Instance.TargetMaid);
				CasinoDataMgr.Instance.AddTotalCoin((int)BjPlayer.Instance.FactMoneyDifference);
			}
			else if (BjPlayer.Instance.IsEven())
			{
				if (BjPlayer.Instance.ExistBlackJack() && Dealer.Instance.IsNatural21())
				{
					BjVoiceMgr.Instance.PlayVoice("ブラックジャック引き分け", null, 0f);
				}
				else
				{
					BjVoiceMgr.Instance.PlayVoice("引き分け", null, 0f);
				}
				this.SetResultImage(Translations.Instance.PUSH);
				this.m_GetMoneyText.text = "コイン" + BjPlayer.Instance.TotalWinnings.ToString() + "枚が手元に戻ってきます";
			}
			else
			{
				BjVoiceMgr.Instance.PlayVoice("負け", null, 0f);
				this.SetResultImage(Translations.Instance.YOU_LOSE);
				this.m_GetMoneyText.text = "コインを" + BjPlayer.Instance.MoneyDifference.ToString() + "枚失いました...";
			}
		}
		else
		{
			BjVoiceMgr.Instance.PlayVoice("負け", null, 0f);
			this.SetResultImage(Translations.Instance.YOU_LOSE);
			this.m_GetMoneyText.text = "コインを" + (BjPlayer.Instance.CurrentBet + BjPlayer.Instance.SplitBet).ToString() + "枚失いました...";
		}
		long orijin_coin = BjPlayer.Instance.OrijinCredit;
		if (BjPlayer.Instance.ExistWinHand())
		{
			orijin_coin = wf.Math.RoundMinMax(BjPlayer.Instance.OrijinCredit - (BjPlayer.Instance.CurrentBet + BjPlayer.Instance.SplitBet), 0L, 999999L);
		}
		this.m_CurrentMouneyText.text = Utility.ConvertMoneyText(orijin_coin);
		base.StartCoroutine(KasaiUtility.FadeCoroutine(this.m_ResultCanvas, false, 0.5f, null, true, true));
		this.m_NextButton.interactable = false;
		if (BjPlayer.Instance.IsEven())
		{
			this.m_NextButton.interactable = true;
		}
		else if (BjPlayer.Instance.ExistSurrenderHand())
		{
			base.StartCoroutine(this.TextFluctuation(orijin_coin));
		}
		else
		{
			ChipManager instance = ChipManager.Instance;
			instance.StackEndCallBack = (Action)Delegate.Combine(instance.StackEndCallBack, new Action(delegate()
			{
				this.StartCoroutine(this.TextFluctuation(orijin_coin));
			}));
		}
	}

	private IEnumerator TextFluctuation(long orijin_coin)
	{
		long coin = GameMain.Instance.CharacterMgr.status.casinoCoin;
		float timer = 0f;
		float rate = 0f;
		if (orijin_coin > coin)
		{
			yield return new WaitForSeconds(this.m_FirstPauseTime);
		}
		else if (orijin_coin == coin)
		{
			this.m_NextButton.interactable = true;
			yield break;
		}
		for (;;)
		{
			timer += Time.deltaTime;
			rate = Mathf.Clamp01(timer / this.m_FluctuationTime);
			long money = (long)Mathf.Lerp((float)orijin_coin, (float)coin, rate);
			this.m_CurrentMouneyText.text = Utility.ConvertMoneyText(money);
			if (rate >= 1f)
			{
				break;
			}
			yield return null;
		}
		this.m_NextButton.interactable = true;
		yield break;
		yield break;
	}

	public void HideResultText()
	{
		base.StartCoroutine(KasaiUtility.FadeCoroutine(this.m_ResultCanvas, true, 0.5f, null, true, true));
	}

	public void EnableButton(Button button, bool enabled)
	{
		button.interactable = enabled;
	}

	public void AngleAbled()
	{
		this.m_AngleButton.interactable = !GameMain.Instance.VRMode;
	}

	public void AngleDisabled()
	{
		this.m_AngleButton.interactable = false;
	}

	public void SetUIActive(bool active)
	{
		this.m_BjGameUI.interactable = active;
	}

	[SerializeField]
	private float m_FluctuationTime = 0.5f;

	[SerializeField]
	private float m_NotFluctuationWait = 0.5f;

	[SerializeField]
	private float m_FirstPauseTime = 1f;

	[SerializeField]
	private float m_EndPauseTime = 0.5f;

	[SerializeField]
	private CanvasGroup m_BjGameUI;

	[SerializeField]
	[Header("ヒット用のUI")]
	private Button m_HitButton;

	[SerializeField]
	[Header("ダブルダウン用のUI")]
	private Button m_DoubleDownButton;

	[SerializeField]
	[Header("スプリット用のUI")]
	private Button m_SplitButton;

	[SerializeField]
	[Header("スタンド用のUI")]
	private Button m_StandButton;

	[SerializeField]
	[Header("サレンダー用のUI")]
	private Button m_SurrenderButton;

	[SerializeField]
	[Header("ベット額表示用のUI")]
	private Text m_BetText;

	[SerializeField]
	[Header("スプリットのベット額表示用のUI")]
	private Text m_SplitBetText;

	[SerializeField]
	[Header("換金用UI表示ボタン")]
	private Button m_ExchangeButton;

	[SerializeField]
	[Header("勝負結果表示用のUI")]
	private GameObject m_ResultUI;

	[SerializeField]
	[Header("アングル切り替えボタン")]
	private Button m_AngleButton;

	[SerializeField]
	[Header("ブラックジャック終了ボタン")]
	private Button m_EndButton;

	[SerializeField]
	[Header("ブラックジャック開始ボタン")]
	private Button m_StartButton;

	private Transform m_ResultImage;

	private Text m_GetMoneyText;

	private Text m_CurrentMouneyText;

	private Button m_NextButton;

	private CanvasGroup m_ResultCanvas;

	private bool m_ButtonsEnabled;
}
