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

public class Hand : MonoBehaviour
{
	public int CurrentScore { get; private set; }

	public int CardNum
	{
		get
		{
			return this.m_TableCards.Count;
		}
	}

	private void Awake()
	{
		this.scoreOutput.gameObject.SetActive(false);
		this.ResetHand();
	}

	public GameObject DealCard(CardData card)
	{
		return this.InitialiseInShoe(card);
	}

	public void ResetScore()
	{
		this.CurrentScore = 0;
		this.UpdateScoreView();
		this.scoreOutput.gameObject.SetActive(false);
	}

	protected void ResetNextPosition()
	{
		this.nextTransformPosition = Vector3.zero;
	}

	public virtual void ResetPositionForSplit()
	{
		this.nextTransformPosition = this.m_CardOffset;
	}

	public virtual void ResetHand()
	{
		this.nextTransformPosition = Vector3.zero;
		if (this.m_TableCards != null && this.m_TableCards.Count > 0)
		{
			for (int i = this.m_TableCards.Count - 1; i >= 0; i--)
			{
				UnityEngine.Object.Destroy(this.m_TableCards[i]);
				this.m_TableCards.Remove(this.m_TableCards[i]);
			}
		}
		this.m_TableCards.Clear();
		this.m_HandCardData.Clear();
		this.ResetScore();
	}

	public bool IsBust()
	{
		return this.CurrentScore > 21;
	}

	protected GameObject InitialiseInShoe(CardData card)
	{
		GameObject cardObj = CardDeck.Instance.GetCardObj(card.CardSuit, card.CardRank);
		GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(cardObj);
		gameObject.transform.SetParent(base.transform, false);
		gameObject.transform.localPosition = Vector3.zero;
		Vector3 position = BlackjackGame.Instance.DeckPos();
		position.y = gameObject.transform.position.y;
		gameObject.transform.position = position;
		this.LinkCardWithData(gameObject, card);
		this.AddCardToHand(gameObject);
		gameObject.SetActive(false);
		return gameObject;
	}

	protected void AddCardToHand(GameObject card)
	{
		this.SetupPosition(card);
		this.m_TableCards.Add(card);
		this.CalculateScore();
	}

	public void UpdateScoreView()
	{
		this.scoreOutput.gameObject.SetActive(this.CurrentScore > 0);
		this.scoreOutput.text = this.CurrentScore.ToString();
		if (this.CurrentScore > 21)
		{
			this.scoreOutput.color = Color.red;
		}
		else
		{
			this.scoreOutput.color = Color.white;
		}
	}

	protected void LinkCardWithData(GameObject cardGameObject, CardData cardData)
	{
		Card component = cardGameObject.GetComponent<Card>();
		component.CardData = cardData;
		if (!this.m_HandCardData.ContainsKey(cardGameObject))
		{
			this.m_HandCardData.Add(cardGameObject, component);
		}
	}

	protected void SetupPosition(GameObject cardGameObject)
	{
		this.m_HandCardData[cardGameObject].CardSet = base.transform;
		this.m_HandCardData[cardGameObject].EndPosition = base.transform.TransformPoint(this.nextTransformPosition);
		Card.CardState cardState = this.GetCardState();
		this.m_HandCardData[cardGameObject].CurrentState = cardState;
		this.nextTransformPosition += this.m_CardOffset;
	}

	public void CalculateScore()
	{
		this.CurrentScore = 0;
		int num = 0;
		foreach (GameObject gameObject in this.m_TableCards)
		{
			Card component = gameObject.GetComponent<Card>();
			if (component.CurrentState != Card.CardState.FlipDown)
			{
				if (component.CardData.CardRank != CardData.Rank.Ace)
				{
					this.CurrentScore += component.CardData.GetValue();
				}
				else
				{
					num++;
				}
			}
		}
		for (int i = 0; i < num; i++)
		{
			if (this.CurrentScore + 11 < 22)
			{
				this.CurrentScore += 11;
			}
			else
			{
				this.CurrentScore++;
			}
		}
	}

	protected virtual Card.CardState GetCardState()
	{
		return Card.CardState.FlipUp;
	}

	public bool IsNatural()
	{
		return this.m_TableCards.Count == 2;
	}

	public Text scoreOutput;

	[SerializeField]
	[Header("カード位置のオフセット")]
	protected Vector3 m_CardOffset = Vector3.zero;

	protected List<GameObject> m_TableCards = new List<GameObject>();

	protected Dictionary<GameObject, Card> m_HandCardData = new Dictionary<GameObject, Card>();

	private Vector3 nextTransformPosition;
}
