using System;
using UnityEngine;

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

	public long CurrentBet { get; private set; }

	public long SplitBet { get; private set; }

	public long TotalWinnings { get; private set; }

	public PlayerHand standardHand
	{
		get
		{
			return this.m_StandardHand;
		}
	}

	public PlayerHand splitHand
	{
		get
		{
			return this.m_SplitHand;
		}
	}

	public PlayerHand CurrentHand { get; private set; }

	public long OrijinCredit { get; private set; }

	public bool IsDecrease { get; private set; }

	public long MoneyDifference
	{
		get
		{
			return this.OrijinCredit - GameMain.Instance.CharacterMgr.status.casinoCoin;
		}
	}

	public long FactMoneyDifference { get; private set; }

	public bool NotEnoughCredit
	{
		get
		{
			return GameMain.Instance.CharacterMgr.status.casinoCoin < BetSetUI.Instance.MoneySetting.MinValue;
		}
	}

	public bool IsNowSplitHand
	{
		get
		{
			return this.CurrentHand == this.splitHand;
		}
	}

	private void Awake()
	{
		BjPlayer.Instance = this;
		this.m_Playerhands = new PlayerHand[]
		{
			this.standardHand,
			this.splitHand
		};
	}

	private void Start()
	{
		this.m_StartingBet = BetSetUI.Instance.MoneySetting.MinValue;
	}

	public void SubtractBet()
	{
		this.OrijinCredit = GameMain.Instance.CharacterMgr.status.casinoCoin;
		GameMain.Instance.CharacterMgr.status.casinoCoin -= this.CurrentBet;
		UIStates.Instance.OnStateChange();
	}

	public void AwardWinnings(int dealerScore)
	{
		this.TotalWinnings = 0L;
		this.ShowOutcomes(dealerScore, Dealer.Instance.IsNatural21());
		foreach (PlayerHand playerHand in this.m_Playerhands)
		{
			long num = 0L;
			if (playerHand.IsEnded() && !playerHand.IsBust())
			{
				if (playerHand.IsSurrender)
				{
					num = this.GetPushWinnings(playerHand) / 2L;
				}
				else if (playerHand.IsEven)
				{
					num = this.GetPushWinnings(playerHand);
				}
				else if (playerHand.IsWin)
				{
					num = this.GetWinnings(playerHand);
				}
			}
			this.TotalWinnings += num;
			foreach (ChipStack chipStack in ChipManager.Instance.ChipStacks)
			{
				if ((chipStack.IsSplitBetStack && playerHand == this.splitHand) || (!chipStack.IsSplitBetStack && playerHand != this.splitHand))
				{
					chipStack.IsSurrender = playerHand.IsSurrender;
					chipStack.IsWinningStack = (num > 0L);
					chipStack.IsEven = playerHand.IsEven;
					chipStack.IsBlackJack = playerHand.IsNatural21();
				}
			}
		}
		bool flag = GameMain.Instance.CharacterMgr.status.casinoCoin + this.TotalWinnings > 999999L;
		GameMain.Instance.CharacterMgr.status.casinoCoin += this.TotalWinnings;
		if (flag)
		{
			this.FactMoneyDifference = this.TotalWinnings - (this.SplitBet + this.CurrentBet);
		}
		else
		{
			this.FactMoneyDifference = -this.MoneyDifference;
		}
		this.IsDecrease = (GameMain.Instance.CharacterMgr.status.casinoCoin < this.OrijinCredit);
		UIStates.Instance.OnStateChange();
		UIStates.Instance.ShowOutcomeText();
		ChipManager.Instance.UpdateStacks();
	}

	private long GetWinnings(PlayerHand hand)
	{
		long num = this.CurrentBet;
		if (hand == this.splitHand)
		{
			num = this.SplitBet;
		}
		long result = num * 2L;
		if (hand.IsNatural21())
		{
			result = (long)Mathf.FloorToInt((float)num * 2.5f);
		}
		return result;
	}

	private long GetPushWinnings(PlayerHand hand)
	{
		return (!(hand == this.splitHand)) ? this.CurrentBet : this.SplitBet;
	}

	public void SetBet(long bet)
	{
		if (this.CurrentHand == this.splitHand)
		{
			this.SplitBet = bet;
		}
		else
		{
			this.CurrentBet = bet;
		}
		UIStates.Instance.OnStateChange();
	}

	public void ClearBet()
	{
		this.CurrentBet = BetSetUI.Instance.MoneySetting.MinValue;
		UIStates.Instance.OnStateChange();
	}

	public bool IsDoubleDown()
	{
		return this.CurrentHand.IsDoubleDown;
	}

	public bool IsStandardDoubleDown()
	{
		return this.standardHand.IsDoubleDown;
	}

	public bool IsSplitDoubleDown()
	{
		return this.splitHand.IsDoubleDown;
	}

	public void Surrender()
	{
		this.CurrentHand.IsSurrender = true;
	}

	public void DoubleDown()
	{
		this.SubtractDoubleDownBet();
		this.CurrentHand.IsDoubleDown = true;
		this.CurrentHand.Stand();
	}

	public GameObject DealCard(CardData card)
	{
		if (this.IsPlacingBet())
		{
			this.m_StartingBet = this.CurrentBet;
		}
		return this.CurrentHand.DealCard(card);
	}

	public void UpdateScore()
	{
		this.standardHand.UpdateScoreView();
		this.splitHand.UpdateScoreView();
	}

	public void ResetScore()
	{
		this.standardHand.ResetScore();
		this.splitHand.ResetScore();
	}

	public void ResetTable()
	{
		this.CurrentBet = this.m_StartingBet;
		this.OrijinCredit = GameMain.Instance.CharacterMgr.status.casinoCoin;
		this.TotalWinnings = 0L;
		this.m_DoubleDownBet = 0L;
		this.SplitBet = 0L;
		this.standardHand.ResetHand();
		this.splitHand.ResetHand();
		this.CurrentHand = this.standardHand;
		this.m_SplitGame = false;
		this.EndingGame = false;
		this.IsDecrease = false;
		this.IsturnEnd = false;
		UIStates.Instance.StateReset();
	}

	public bool IsSplitGame()
	{
		return this.m_SplitGame;
	}

	public bool IsIdle()
	{
		return this.CurrentHand.CurrentScore == 0 && this.splitHand.CurrentScore == 0 && !BetSetUI.Instance.IsDoBet;
	}

	public bool IsPlacingBet()
	{
		return this.CurrentHand.CurrentScore == 0 && this.splitHand.CurrentScore == 0 && BetSetUI.Instance.IsDoBet;
	}

	public bool CanHit()
	{
		return this.CurrentHand.CanHit();
	}

	public bool CanDoubleDown()
	{
		return this.CurrentHand == this.splitHand && this.splitHand.DoubleDownAvailable();
	}

	public bool HandIsEnded()
	{
		return this.CurrentHand.IsEnded();
	}

	public bool IsNatural21()
	{
		return this.standardHand.IsNatural21();
	}

	public bool IsEnded()
	{
		if (this.m_SplitGame)
		{
			return this.standardHand.IsEnded() && this.splitHand.IsEnded();
		}
		return this.CurrentHand.IsEnded();
	}

	public bool DoubleDownEnoughMoney()
	{
		if (this.CurrentHand == this.splitHand)
		{
			return GameMain.Instance.CharacterMgr.status.casinoCoin - this.SplitBet >= 0L;
		}
		return GameMain.Instance.CharacterMgr.status.casinoCoin - this.CurrentBet >= 0L;
	}

	public bool DoubleDownAvailable()
	{
		return !this.EndingGame && this.CurrentHand.DoubleDownAvailable() && this.DoubleDownEnoughMoney();
	}

	public bool SplitEnoughMoney()
	{
		return GameMain.Instance.CharacterMgr.status.casinoCoin - this.CurrentBet >= 0L;
	}

	public bool SplitAvailable()
	{
		return !this.m_SplitGame && this.CurrentHand.SplitAvailable() && this.SplitEnoughMoney();
	}

	public void StandOnCurrentHand()
	{
		this.CurrentHand.Stand();
	}

	private void SubtractDoubleDownBet()
	{
		this.m_DoubleDownBet = this.CurrentBet;
		GameMain.Instance.CharacterMgr.status.casinoCoin -= this.m_DoubleDownBet;
		if (this.CurrentHand == this.splitHand)
		{
			this.SplitBet += this.m_DoubleDownBet;
		}
		else
		{
			this.CurrentBet += this.m_DoubleDownBet;
		}
	}

	private void SubtractSplitBet()
	{
		this.SplitBet = this.CurrentBet;
		GameMain.Instance.CharacterMgr.status.casinoCoin -= this.SplitBet;
	}

	public void Split()
	{
		this.m_SplitGame = true;
		this.SubtractSplitBet();
		this.CurrentHand = this.splitHand;
	}

	public void NextHand()
	{
		if (this.m_SplitGame)
		{
			this.CurrentHand = this.standardHand;
			this.CurrentHand.ResetPositionForSplit();
		}
	}

	public bool CanSurrender()
	{
		return !this.IsSplitGame() && this.CurrentHand.CanSurrender() && !this.EndingGame && Dealer.Instance.HasFacedownCard();
	}

	private void ShowOutcomes(int dealerScore, bool dealerBlackjack)
	{
		foreach (PlayerHand playerHand in this.m_Playerhands)
		{
			playerHand.ShowOutcome(dealerScore, dealerBlackjack);
		}
	}

	public long GetCurrentBet()
	{
		if (this.CurrentHand == this.splitHand)
		{
			return this.SplitBet;
		}
		return this.CurrentBet;
	}

	public bool IsEven()
	{
		if (this.m_SplitGame)
		{
			return this.standardHand.IsEven && this.splitHand.IsEven;
		}
		return this.standardHand.IsEven;
	}

	public bool ExistDoubleDownwWin()
	{
		return this.standardHand.DoubleDownWin() || this.splitHand.DoubleDownWin();
	}

	public bool ExistWinHand()
	{
		return this.standardHand.IsWin || this.splitHand.IsWin;
	}

	public bool OnesideWin()
	{
		return (!this.standardHand.IsWin || !this.splitHand.IsWin) && (this.standardHand.IsWin || this.splitHand.IsWin);
	}

	public bool ExistSurrenderHand()
	{
		return this.standardHand.IsSurrender;
	}

	public bool ExistBlackJack()
	{
		return this.standardHand.IsNatural21() || this.splitHand.IsNatural21();
	}

	private long m_DoubleDownBet;

	[HideInInspector]
	public bool EndingGame;

	private long m_StartingBet;

	[SerializeField]
	private PlayerHand m_StandardHand;

	[SerializeField]
	private PlayerHand m_SplitHand;

	private PlayerHand[] m_Playerhands;

	private bool m_SplitGame;

	[HideInInspector]
	public bool IsturnEnd;
}
