using System;
using System.Collections;
using PlayerStatus;
using UnityEngine;
using UnityEngine.UI;

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

	public bool isZoom
	{
		get
		{
			return this.m_IsZoom;
		}
	}

	public AudioSource CardSe
	{
		get
		{
			return this.m_CardSe;
		}
	}

	public DealQueue DealQueue
	{
		get
		{
			return this.m_DealQueue;
		}
	}

	private void Awake()
	{
		BlackjackGame.Instance = this;
		uGUIUtility.GetEventSystem();
		if (GameMain.Instance.ScriptMgr.adv_kag.tag_backup.ContainsKey("cancel_label"))
		{
			this.m_CancelLabel = GameMain.Instance.ScriptMgr.adv_kag.tag_backup["cancel_label"];
		}
	}

	private void Start()
	{
		GameMain.Instance.SysShortcut.strSceneHelpName = "SceneBlackJack";
		this.m_DealQueue = new DealQueue();
		this.m_DealQueue.Init();
		this.ResetTable();
		base.StartCoroutine(this.CharaLoadWait());
	}

	private IEnumerator CharaLoadWait()
	{
		GameMain.Instance.BgMgr.DeleteBg();
		GameMain.Instance.SoundMgr.PlayBGM(this.m_BGMName, 0.5f, true);
		this.m_MainCamType = GameMain.Instance.MainCamera.GetCameraType();
		GameMain.Instance.MainCamera.SetTargetPos(this.m_NormalCamSetting.TatgetPos, true);
		GameMain.Instance.MainCamera.SetDistance(this.m_NormalCamSetting.Distance, true);
		GameMain.Instance.MainCamera.SetAroundAngle(this.m_NormalCamSetting.TargetAngle, true);
		GameMain.Instance.MainLight.Reset();
		GameMain.Instance.MainCamera.FadeOut(0f, false, null, true, default(Color));
		this.m_ControlEnable = GameMain.Instance.MainCamera.GetControl();
		GameMain.Instance.MainCamera.SetControl(false);
		this.m_UIRaycaster.enabled = false;
		if (CasinoDataMgr.Instance.DealerMaid)
		{
			GameMain.Instance.CharacterMgr.SetActiveMaid(CasinoDataMgr.Instance.DealerMaid, 0);
			CasinoDataMgr.Instance.DealerMaid.AllProcPropSeqStart();
			CasinoDataMgr.Instance.DealerMaid.Visible = true;
		}
		else
		{
			GameMain.Instance.CharacterMgr.status.casinoCoin = 499999L;
			ExChangeUI.Instance.TextUIUpdate();
		}
		bool need_costumeload = CasinoDataMgr.Instance.DealerMaid && CasinoDataMgr.Instance.CurrentCasino.typeCostume != Facility.CostumeType.Heroine;
		Action load_end = delegate()
		{
			BjMotionControl.Instance.Init();
			this.m_ChaOffsetRot = GameMain.Instance.CharacterMgr.GetCharaAllOfsetRot();
			GameMain.Instance.CharacterMgr.CharaAllOfsetRot(this.m_DealerMaidAngle);
			this.m_UIRaycaster.enabled = true;
			GameMain.Instance.MainCamera.FadeIn(0.5f, false, null, false, true, default(Color));
		};
		while (GameMain.Instance.CharacterMgr.IsBusy())
		{
			yield return null;
		}
		if (!need_costumeload)
		{
			yield return null;
			load_end();
			yield break;
		}
		Maid dealerMaid = CasinoDataMgr.Instance.DealerMaid;
		Facility.CostumeType typeCostume = CasinoDataMgr.Instance.CurrentCasino.typeCostume;
		if (typeCostume != Facility.CostumeType.Default)
		{
			if (typeCostume == Facility.CostumeType.Edit)
			{
				CasinoDataMgr.Instance.CurrentCasino.UpdateMaidCostumeToEditCostume(dealerMaid, false);
			}
		}
		else
		{
			CasinoDataMgr.Instance.CurrentCasino.UpdateMaidCostumeToDefaultCostume(dealerMaid);
		}
		dealerMaid.AllProcPropSeqStart();
		while (GameMain.Instance.CharacterMgr.IsBusy())
		{
			yield return null;
		}
		yield return null;
		load_end();
		yield break;
	}

	public void SetDealerPos()
	{
		this.m_ChaOffsetPos = GameMain.Instance.CharacterMgr.GetCharaAllOfsetPos();
		GameMain.Instance.CharacterMgr.CharaAllOfsetPos(this.m_DealerMaidPos);
	}

	public void AngleChange()
	{
		UIStates.Instance.AngleDisabled();
		this.m_IsZoom = !this.m_IsZoom;
		base.StartCoroutine(KasaiUtility.TimeCroutine(this.m_AngleChange, new Action<float>(this.SetCamAngle), new Action(UIStates.Instance.AngleAbled)));
	}

	private void OnceAngleChange()
	{
		UIStates.Instance.AngleDisabled();
		BjMotionControl.Instance.DoLeaveMotion = false;
		this.m_IsZoom = !this.m_IsZoom;
		base.StartCoroutine(KasaiUtility.TimeCroutine(this.m_AngleChange, new Action<float>(this.SetCamAngle), null));
	}

	private void SetCamAngle(float time)
	{
		float t = (!this.m_IsZoom) ? (1f - Mathf.Clamp01(time / this.m_AngleChange)) : Mathf.Clamp01(time / this.m_AngleChange);
		GameMain.Instance.MainCamera.SetTargetPos(Vector3.Lerp(this.m_NormalCamSetting.TatgetPos, this.m_ZoomCamSetting.TatgetPos, t), true);
		GameMain.Instance.MainCamera.SetAroundAngle(Vector2.Lerp(this.m_NormalCamSetting.TargetAngle, this.m_ZoomCamSetting.TargetAngle, t), true);
		GameMain.Instance.MainCamera.SetDistance(Mathf.Lerp(this.m_NormalCamSetting.Distance, this.m_ZoomCamSetting.Distance, t), true);
	}

	public void Deal()
	{
		UIStates.Instance.SetEnabled(false);
		UIStates.Instance.AngleAbled();
		if (BjPlayer.Instance.IsPlacingBet())
		{
			BjMotionControl.Instance.PlayMotion("*初期配布", new Action(this.FinishedDealing), false, false);
			if (GameMain.Instance.CharacterMgr.status.casinoCoin >= BjPlayer.Instance.CurrentBet)
			{
				this.InitialDeal();
			}
		}
	}

	public void Hit()
	{
		BjPlayer.Instance.CurrentHand.IsHit = true;
		ActionDirect.Instance.ActionStart(Translations.Instance.HIT);
		UIStates.Instance.SetEnabled(false);
		BjMotionControl.Instance.DoLeaveMotion = false;
		Action end_action = delegate()
		{
			BjMotionControl.Instance.PlayMotion(BjMotionControl.Instance.GetHaihuMotion(), new Action(this.FinishedDealing), false, false);
			if (!BjPlayer.Instance.IsPlacingBet())
			{
				this.DealPlayer();
			}
		};
		BjVoiceMgr.Instance.PlayVoice("ヒット", null, 0f);
		BjMotionControl.Instance.PlayMotion("*頷く", end_action, true, false);
	}

	public void DoubleDown()
	{
		ActionDirect.Instance.ActionStart(Translations.Instance.DOUBLE_DOWN);
		UIStates.Instance.SetEnabled(false);
		BjMotionControl.Instance.DoLeaveMotion = false;
		Action end_action = delegate()
		{
			ChipManager instance = ChipManager.Instance;
			instance.StackEndCallBack = (Action)Delegate.Combine(instance.StackEndCallBack, new Action(delegate()
			{
				BjVoiceMgr.Instance.PlayVoice("ダブルダウンオープン", new Action(this.DoDoubleDown), 0f);
			}));
			ChipManager instance2 = ChipManager.Instance;
			instance2.StackEndCallBack = (Action)Delegate.Combine(instance2.StackEndCallBack, new Action(UIStates.Instance.OnStateChange));
			BjPlayer.Instance.DoubleDown();
			ChipManager.Instance.UpdateStacks();
			BjMotionControl.Instance.SetWaitMotion(BjMotionControl.WaitType.Normal);
		};
		BjVoiceMgr.Instance.PlayVoice("ダブルダウン", null, 0f);
		BjMotionControl.Instance.PlayMotion("*頷く", end_action, true, false);
	}

	private void DoDoubleDown()
	{
		BjMotionControl.Instance.PlayMotion(BjMotionControl.Instance.GetHaihuMotion(), new Action(this.FinishedDealing), false, false);
		CardData card = CardDeck.Instance.GetCard();
		this.m_DealQueue.DealCard(BjPlayer.Instance.DealCard(card));
	}

	public void Stand()
	{
		ActionDirect.Instance.ActionStart(Translations.Instance.STAND);
		UIStates.Instance.SetEnabled(false);
		BjMotionControl.Instance.DoLeaveMotion = false;
		Action end_action = delegate()
		{
			if (!BjPlayer.Instance.IsSplitGame())
			{
				BjMotionControl.Instance.SetWaitMotion(BjMotionControl.WaitType.Normal);
			}
			else
			{
				BjMotionControl.Instance.SetWaitMotion(BjMotionControl.WaitType.Split2);
			}
			BjPlayer.Instance.StandOnCurrentHand();
			this.CheckIfEnded();
		};
		BjVoiceMgr.Instance.PlayVoice("スタンド", null, 0f);
		BjMotionControl.Instance.PlayMotion("*頷く", end_action, true, false);
	}

	public void Split()
	{
		ActionDirect.Instance.ActionStart(Translations.Instance.SPLIT);
		BjMotionControl.Instance.DoLeaveMotion = false;
		UIStates.Instance.SetEnabled(false);
		Action end_action = delegate()
		{
			ChipManager instance = ChipManager.Instance;
			instance.StackEndCallBack = (Action)Delegate.Combine(instance.StackEndCallBack, new Action(delegate()
			{
				GameMain.Instance.MainCamera.FadeOut(0.5f, false, new CameraMain.dgOnCompleteFade(this.SplitFadeIn), false, default(Color));
			}));
			ChipManager instance2 = ChipManager.Instance;
			instance2.StackEndCallBack = (Action)Delegate.Combine(instance2.StackEndCallBack, new Action(UIStates.Instance.OnStateChange));
			BjPlayer.Instance.Split();
			ChipManager.Instance.UpdateStacks();
		};
		BjVoiceMgr.Instance.PlayVoice("スプリット", null, 0f);
		BjMotionControl.Instance.PlayMotion("*頷く", end_action, true, false);
	}

	private void SplitFadeIn()
	{
		GameObject card = BjPlayer.Instance.standardHand.TakeCard(1);
		BjPlayer.Instance.splitHand.StartSplit(card);
		BjPlayer.Instance.UpdateScore();
		GameMain.Instance.MainCamera.FadeIn(0.5f, false, new CameraMain.dgOnCompleteFade(this.DoSplit), false, true, default(Color));
	}

	private void DoSplit()
	{
		BjMotionControl.Instance.PlayMotion(BjMotionControl.Instance.GetHaihuMotion(), new Action(this.FinishedDealing), false, false);
		this.DealPlayer();
		BjMotionControl.Instance.SetWaitMotion(BjMotionControl.WaitType.Split2);
	}

	public void Surrender()
	{
		ActionDirect.Instance.ActionStart(Translations.Instance.SURRENDER);
		UIStates.Instance.SetEnabled(false);
		BjMotionControl.Instance.DoLeaveMotion = false;
		Action end_action = delegate()
		{
			BjPlayer.Instance.Surrender();
			this.CheckIfEnded();
		};
		BjVoiceMgr.Instance.PlayVoice("サレンダー", null, 0f);
		BjMotionControl.Instance.PlayMotion("*頷く", end_action, true, false);
	}

	private void InitialDeal()
	{
		BjPlayer.Instance.SubtractBet();
		for (int i = 0; i < 2; i++)
		{
			this.DealPlayer();
			this.DealDealer();
		}
	}

	private void DealDealer()
	{
		UIStates.Instance.SetEnabled(false);
		BjMotionControl.Instance.SetWaitMotion(BjMotionControl.WaitType.Normal);
		if (Dealer.Instance.HasFacedownCard())
		{
			Action end_call = delegate()
			{
				BjMotionControl.Instance.PlayMotion("*カード開く", new Action(this.FinishedAnimating), false, false);
				this.m_DealQueue.CardOpen(Dealer.Instance.GetFacedownCard());
			};
			BjVoiceMgr.Instance.PlayVoice("ディーラーオープン", end_call, 0f);
		}
		else if (Dealer.Instance.NeedCardMove)
		{
			BjMotionControl.Instance.PlayMotion("*カード移動", new Action(this.DealDealer), false, false);
			Dealer.Instance.CardMove();
		}
		else
		{
			if (BjPlayer.Instance.IsturnEnd)
			{
				BjMotionControl.Instance.PlayMotion("*ディーラーに配布", new Action(this.FinishedAnimating), false, false);
			}
			CardData card = CardDeck.Instance.GetCard();
			this.m_DealQueue.DealCard(Dealer.Instance.DealCard(card));
		}
	}

	private void DealPlayer()
	{
		UIStates.Instance.SetEnabled(false);
		CardData card = CardDeck.Instance.GetCard();
		this.m_DealQueue.DealCard(BjPlayer.Instance.DealCard(card));
	}

	private void FinishedAnimating()
	{
		Dealer.Instance.CheckSurrender();
		Dealer.Instance.CalculateScore();
		this.FinishedDealing();
	}

	private void FinishedDealing()
	{
		BjMotionControl.Instance.ForceWaitStop = false;
		UIStates.Instance.SetEnabled(true);
		UIStates.Instance.OnStateChange();
		BjPlayer.Instance.UpdateScore();
		Dealer.Instance.UpdateScoreView();
		this.CheckIfEnded();
		if (BetSetUI.Instance.IsDoBet && !BjPlayer.Instance.IsEnded() && !BjPlayer.Instance.IsSplitGame())
		{
			BjMotionControl.Instance.DoLeaveMotion = true;
		}
		BjMotionControl.Instance.PlayWaitMotion();
	}

	private void CheckIfEnded()
	{
		if (BjPlayer.Instance.IsEnded())
		{
			this.PlayerIsFinished();
		}
		else if (BjPlayer.Instance.IsSplitGame() && BjPlayer.Instance.HandIsEnded() && BjPlayer.Instance.CurrentHand == BjPlayer.Instance.splitHand)
		{
			bool is_bust = BjPlayer.Instance.CurrentHand.IsBust();
			Action action = delegate()
			{
				BjPlayer.Instance.NextHand();
				BjMotionControl.Instance.PlayMotion(BjMotionControl.Instance.GetHaihuMotion(), new Action(this.FinishedDealing), is_bust, true);
				this.DealPlayer();
				BjMotionControl.Instance.SetWaitMotion(BjMotionControl.WaitType.Split1);
			};
			if (is_bust)
			{
				if (BjPlayer.Instance.CurrentHand.IsDoubleDown)
				{
					BjVoiceMgr.Instance.PlayVoice("ダブルダウン慰め", action, 0f);
				}
				else
				{
					BjVoiceMgr.Instance.PlayVoice("バースト慰め", action, 0f);
				}
				this.m_BustSe.Play();
				ActionDirect.Instance.ActionStart(Translations.Instance.BUST);
				BjMotionControl.Instance.SetWaitMotion(BjMotionControl.WaitType.Normal);
			}
			else
			{
				BjMotionControl.Instance.ForceWaitStop = true;
				action();
			}
		}
		else if (BjPlayer.Instance.CurrentHand.IsHit)
		{
			BjVoiceMgr.Instance.PlayVoice("ヒット後選択", null, 0f);
		}
		else if (BjPlayer.Instance.IsSplitGame())
		{
			BjVoiceMgr.Instance.PlayVoice("スプリット選択", null, 0f);
		}
		else
		{
			BjVoiceMgr.Instance.PlayVoice("行動選択", null, 0f);
		}
	}

	private void PlayerIsFinished()
	{
		if (!BjPlayer.Instance.IsEnded())
		{
			return;
		}
		if (!Dealer.Instance.IsEnded())
		{
			BjPlayer.Instance.IsturnEnd = true;
			UIStates.Instance.SetEnabled(false);
			if (Dealer.Instance.HasFacedownCard())
			{
				if (BjPlayer.Instance.CurrentHand.IsNatural21())
				{
					BjVoiceMgr.Instance.PlayVoice("ブラックジャック", new Action(this.DealDealer), 0f);
					GameMain.Instance.SoundMgr.PlaySe("casino_SE002.ogg", false);
					ActionDirect.Instance.ActionStart(Translations.Instance.BLACKJACK);
				}
				else if (BjPlayer.Instance.CurrentHand.IsBust())
				{
					if (BjPlayer.Instance.CurrentHand.IsDoubleDown)
					{
						BjVoiceMgr.Instance.PlayVoice("ダブルダウン慰め", new Action(this.DealDealer), 0f);
					}
					else
					{
						BjVoiceMgr.Instance.PlayVoice("バースト慰め", new Action(this.DealDealer), 0f);
					}
					this.m_BustSe.Play();
					ActionDirect.Instance.ActionStart(Translations.Instance.BUST);
					BjMotionControl.Instance.SetWaitMotion(BjMotionControl.WaitType.Normal);
				}
				else
				{
					this.DealDealer();
				}
			}
			else
			{
				BjVoiceMgr.Instance.PlayScoreVoice(new Action(this.DealDealer), 0f);
			}
		}
		else
		{
			BjVoiceMgr.Instance.PlayScoreVoice(new Action(this.EndGame), 0f);
		}
	}

	private void EndGame()
	{
		if (!BjPlayer.Instance.EndingGame)
		{
			UIStates.Instance.AngleDisabled();
			if (this.m_IsZoom)
			{
				this.OnceAngleChange();
			}
			BjPlayer.Instance.EndingGame = true;
			BjMotionControl.Instance.ForceWaitStop = false;
			BjMotionControl.Instance.DoLeaveMotion = false;
			BjPlayer.Instance.AwardWinnings(Dealer.Instance.CurrentScore);
		}
	}

	public void ResetTable()
	{
		BjVoiceMgr.Instance.Stop();
		UIStates.Instance.HideResultText();
		BjPlayer.Instance.ResetScore();
		Dealer.Instance.ResetScore();
		BjMotionControl.Instance.PlayWaitMotion(BjMotionControl.WaitType.Normal);
		this.HardResetTable();
	}

	private void HardResetTable()
	{
		BetSetUI.Instance.StateRecet();
		CardDeck.Instance.Shuffle(this.m_DeckCount);
		BjPlayer.Instance.ResetTable();
		Dealer.Instance.ResetHand();
		ChipManager.Instance.HardReset();
		ChipManager.Instance.UpdateStacks();
	}

	public static string BlackJackResource(string behind_path)
	{
		return "SceneBlackJack/" + behind_path;
	}

	public Vector3 DeckPos()
	{
		return this.m_DeckObj.transform.position;
	}

	public void PushEndButton()
	{
		GameMain.Instance.SysDlg.Show("カジノ画面に戻ります", SystemDialog.TYPE.OK_CANCEL, new SystemDialog.OnClick(this.BJEnd), null);
	}

	public void BJStart()
	{
		Status status = GameMain.Instance.CharacterMgr.status;
		if (status.casinoCoin >= BetSetUI.Instance.MoneySetting.MinValue)
		{
			BetSetUI.Instance.FadeIn();
			UIStates.Instance.OnStateChange();
		}
		else
		{
			string f_strMsg = string.Format("ブラックジャックをするには最低でも\nコインが{0:#,##0}枚必用です", BetSetUI.Instance.MoneySetting.MinValue);
			GameMain.Instance.SysDlg.Show(f_strMsg, SystemDialog.TYPE.OK, null, null);
		}
	}

	public void BJEnd()
	{
		SubtitleMovieManager.DestroyGlobalInstance();
		this.m_UIRaycaster.enabled = false;
		GameMain.Instance.SysDlg.Close();
		BetSetUI.Instance.FadeOut();
		GameMain.Instance.MainCamera.FadeOut(0.5f, false, delegate
		{
			BjVoiceMgr.Instance.VoiceEnd();
			GameMain.Instance.CharacterMgr.ResetCharaPosAll();
			GameMain.Instance.CharacterMgr.CharaAllOfsetRot(Vector3.zero);
			GameMain.Instance.ScriptMgr.is_motion_blend = false;
			GameMain.Instance.ScriptMgr.ClearForcExecWaitEvent();
			GameMain.Instance.ScriptMgr.StopMotionScript();
			GameMain.Instance.ScriptMgr.adv_kag.JumpLabel(this.m_CancelLabel);
			GameMain.Instance.ScriptMgr.adv_kag.Exec();
		}, true, default(Color));
	}

	public const float DeskHeight = 0.755f;

	[SerializeField]
	private string m_BGMName = "BGM009.ogg";

	[SerializeField]
	[Header("ディーラーとなるメイドの位置")]
	private Vector3 m_DealerMaidPos;

	[SerializeField]
	[Header("ディーラーとなるメイドの角度")]
	private Vector3 m_DealerMaidAngle;

	[SerializeField]
	private float m_AngleChange = 0.5f;

	[SerializeField]
	[Header("カメラ設定(通常時)")]
	private BlackjackGame.CamSetting m_NormalCamSetting;

	[SerializeField]
	[Header("カメラ設定(切り替え時)")]
	private BlackjackGame.CamSetting m_ZoomCamSetting;

	private bool m_IsZoom;

	[SerializeField]
	[Header("デッキの個数")]
	private int m_DeckCount = 1;

	[SerializeField]
	[Header("デッキオブジェクト")]
	private GameObject m_DeckObj;

	[SerializeField]
	[Header("UIの当たり判定クラス")]
	private GraphicRaycaster m_UIRaycaster;

	[SerializeField]
	[Header("独自のSe再生用")]
	private AudioSource m_CardSe;

	[SerializeField]
	private AudioSource m_BustSe;

	private DealQueue m_DealQueue;

	private bool m_ControlEnable;

	private CameraMain.CameraType m_MainCamType;

	private Vector3 m_ChaOffsetPos;

	private Vector3 m_ChaOffsetRot;

	private string m_CancelLabel;

	[Serializable]
	private class CamSetting
	{
		[Header("カメラのターゲット位置")]
		public Vector3 TatgetPos;

		[Header("カメラの相対角度")]
		public Vector2 TargetAngle;

		[Header("ターゲットに対するカメラの距離")]
		public float Distance;
	}
}
