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

public class ChipStack : MonoBehaviour
{
	public bool IsSplitBetStack
	{
		get
		{
			return this.m_IsSplitBetStack;
		}
	}

	public bool ChipUpdateEnd { get; private set; }

	public void UpdateChips()
	{
		this.ChipUpdateEnd = false;
		this.m_IsStack = false;
		long stackBet = this.m_StackBet;
		if (!this.CheckStackBet() && BetSetUI.Instance.IsDoBet)
		{
			this.ChipUpdateEnd = true;
			return;
		}
		if (!BetSetUI.Instance.IsDoBet)
		{
			this.ResetChips();
			this.m_StackBet = 0L;
			return;
		}
		this.m_NotNeedAnimation = (stackBet > this.m_StackBet);
		if (this.m_NotNeedAnimation)
		{
			this.ResetChips();
		}
		this.m_StackDifference = this.m_StackBet - stackBet;
		this.RefreshCoinAmounts();
	}

	private bool CheckStackBet()
	{
		long num = (!this.IsSplitBetStack) ? BjPlayer.Instance.CurrentBet : BjPlayer.Instance.SplitBet;
		if (this.IsWinningStack && !this.IsEven && !this.IsSurrender)
		{
			if (this.IsBlackJack)
			{
				num = (long)Mathf.FloorToInt((float)num * 2.5f);
			}
			else
			{
				num *= 2L;
			}
			this.m_IsStack = true;
			this.m_Stackline = 5;
		}
		else if (this.IsSurrender)
		{
			num = (long)Mathf.FloorToInt((float)(num / 2L));
		}
		if (num == this.m_StackBet)
		{
			return false;
		}
		this.m_StackBet = num;
		return true;
	}

	private void RefreshCoinAmounts()
	{
		this.IsEven = false;
		this.IsBlackJack = false;
		this.IsSurrender = false;
		long num = 0L;
		int num2 = 0;
		int num3 = 0;
		bool flag = this.m_IsStack && this.m_ChipFallingHigh.Count > 0;
		int num4 = (!this.m_IsStack) ? 0 : this.m_ChipStackList.Count;
		PlayerHand playerHand = (!this.m_IsSplitBetStack) ? BjPlayer.Instance.standardHand : BjPlayer.Instance.splitHand;
		int num5 = (!this.m_IsStack || !playerHand.IsDoubleDown) ? 1 : 2;
		for (int i = 0; i < num5; i++)
		{
			num += this.m_StackDifference / (long)num5;
			foreach (ChipManager.Chip chip in ChipManager.Instance.ChipList)
			{
				int num6 = Mathf.FloorToInt((float)(num / (long)chip.value));
				for (int j = 0; j < num6; j++)
				{
					GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(chip.chip_obj);
					gameObject.transform.SetParent(base.transform, false);
					int num7 = this.m_ChipStackList.Count - num4;
					int num8 = num7 / ChipManager.Instance.StackCount;
					if ((num8 + 1 - num3) / 2 == this.m_Stackline)
					{
						this.m_Stackline -= 2;
						num3 = num8;
						num2 = num8 - (5 - this.m_Stackline);
					}
					int num9 = (!this.m_IsSplitBetStack) ? -1 : 1;
					Vector3 zero = Vector3.zero;
					zero.x = (float)(num8 - num2) / 2f * (float)num9 * ChipManager.Instance.ChipStackSpace.x;
					zero.z = (float)((num8 - num2) % 2 + 5 - this.m_Stackline) * ChipManager.Instance.ChipStackSpace.z;
					if (this.m_NotNeedAnimation)
					{
						zero.y = ChipManager.Instance.ChipStackSpace.y * (float)(num7 % ChipManager.Instance.StackCount);
					}
					else
					{
						zero.y = ChipManager.Instance.FallingLength + ChipManager.Instance.ChipSetSpace * (float)(num7 % ChipManager.Instance.StackCount);
						if (flag && this.m_ChipFallingHigh.ContainsKey(num8))
						{
							zero.y += this.m_ChipFallingHigh[num8];
						}
						this.m_ChipMoveList.Add(gameObject);
					}
					gameObject.transform.localPosition = zero;
					this.m_ChipStackList.Add(gameObject);
				}
				num %= (long)chip.value;
			}
		}
		if (!this.m_NotNeedAnimation)
		{
			base.StartCoroutine(this.CoinFallDown());
		}
	}

	private IEnumerator CoinFallDown()
	{
		float timer = 0f;
		bool exist_chip = this.m_IsStack && this.m_ChipFallingHigh.Count > 0;
		List<Vector3> start_pos = new List<Vector3>();
		foreach (GameObject gameObject in this.m_ChipMoveList)
		{
			start_pos.Add(gameObject.transform.localPosition);
		}
		int chip_index = (!this.m_IsStack) ? (this.m_ChipStackList.Count - this.m_ChipMoveList.Count) : 0;
		int part_count = this.m_ChipMoveList.Count / ChipManager.Instance.StackCount;
		int index_remaind = (this.m_IsStack || chip_index <= 0) ? 0 : (chip_index % ChipManager.Instance.StackCount);
		bool sound_play = false;
		for (;;)
		{
			timer += Time.deltaTime;
			bool move_end = true;
			for (int i = 0; i < this.m_ChipMoveList.Count; i++)
			{
				float current_time = Mathf.Max(timer - (float)((i + index_remaind) / ChipManager.Instance.StackCount) * ChipManager.Instance.TimeOffset, 0f);
				float num = KasaiUtility.SinRate01(current_time, ChipManager.Instance.FallingTime, false, false);
				move_end = (num == 1f);
				if (num == 0f)
				{
					break;
				}
				if (move_end && !sound_play)
				{
					GameMain.Instance.SoundMgr.PlaySe("casino_SE004.ogg", false);
					sound_play = true;
				}
				Vector3 localPosition = this.m_ChipMoveList[i].transform.localPosition;
				localPosition.y = ChipManager.Instance.ChipStackSpace.y * (float)((i + chip_index) % ChipManager.Instance.StackCount);
				if (exist_chip && this.m_ChipFallingHigh.ContainsKey(i / ChipManager.Instance.StackCount))
				{
					localPosition.y += this.m_ChipFallingHigh[i / ChipManager.Instance.StackCount];
				}
				this.m_ChipMoveList[i].transform.localPosition = Vector3.Lerp(start_pos[i], localPosition, num);
			}
			if (move_end)
			{
				break;
			}
			yield return null;
		}
		this.ChipUpdateEnd = true;
		int num2 = this.m_ChipStackList.Count / ChipManager.Instance.StackCount;
		if (this.m_ChipStackList.Count % ChipManager.Instance.StackCount != 0)
		{
			num2++;
		}
		for (int j = 0; j < num2; j++)
		{
			int index = Mathf.Min((j + 1) * ChipManager.Instance.StackCount - 1, this.m_ChipStackList.Count - 1);
			float value = this.m_ChipStackList[index].transform.localPosition.y + ChipManager.Instance.ChipStackSpace.y;
			if (this.m_ChipFallingHigh.ContainsKey(j))
			{
				this.m_ChipFallingHigh[j] = value;
			}
			else
			{
				this.m_ChipFallingHigh.Add(j, value);
			}
		}
		this.m_ChipMoveList.Clear();
		ChipManager.Instance.StackEndAction();
		yield break;
		yield break;
	}

	public bool ContainsChip(GameObject chip)
	{
		return this.m_ChipStackList.Contains(chip);
	}

	public void ResetChips()
	{
		if (this.m_ChipStackList != null && this.m_ChipStackList.Count > 0)
		{
			foreach (GameObject obj in this.m_ChipStackList)
			{
				UnityEngine.Object.Destroy(obj);
			}
		}
		this.m_ChipFallingHigh.Clear();
		this.m_ChipStackList.Clear();
		this.m_Stackline = 5;
		this.m_IsStack = false;
	}

	private const int m_BaseStackLine = 5;

	[SerializeField]
	[Header("スプリット用かどうか")]
	private bool m_IsSplitBetStack;

	[HideInInspector]
	public bool IsWinningStack;

	[HideInInspector]
	public bool IsSurrender;

	[HideInInspector]
	public bool IsBlackJack;

	[HideInInspector]
	public bool IsEven;

	private long m_StackBet;

	private long m_StackDifference;

	private List<GameObject> m_ChipStackList = new List<GameObject>();

	private List<GameObject> m_ChipMoveList = new List<GameObject>();

	private bool m_NotNeedAnimation;

	private bool m_IsStack;

	private bool m_ChipUp;

	private int m_Stackline = 5;

	private Dictionary<int, float> m_ChipFallingHigh = new Dictionary<int, float>();
}
