using System;
using System.Collections;
using UnityEngine;

public class Card : MonoBehaviour
{
	public bool IsMoveend
	{
		get
		{
			return this.m_IsMoveend;
		}
	}

	private void Reset()
	{
		this.m_MyRenderer = base.GetComponent<MeshRenderer>();
	}

	private void PosYFixed()
	{
		Vector3 position = base.transform.position;
		position.y = this.EndPosition.y;
		base.transform.position = position;
	}

	private IEnumerator CardMove()
	{
		this.m_MyRenderer.enabled = false;
		float timer = this.InitTime;
		while (timer <= this.StartTime)
		{
			timer += Time.deltaTime;
			yield return null;
		}
		this.m_MyRenderer.enabled = true;
		BlackjackGame.Instance.CardSe.Play();
		bool is_player = base.transform.parent == BjPlayer.Instance.CurrentHand.transform;
		Vector3 orijin_pos = base.transform.position;
		bool hand_switch = false;
		for (;;)
		{
			timer += Time.deltaTime;
			if (!hand_switch)
			{
				hand_switch = (timer > this.SwitchTime);
				base.transform.position = BjMotionControl.Instance.LeftHandPos();
				float num = this.StartTime + (this.SwitchTime - this.StartTime) / 2f;
				float t = KasaiUtility.SinRate01(timer - num, this.SwitchTime - num, false, false);
				this.m_OpenTime = this.SwitchTime - num;
				if (this.CurrentState == Card.CardState.FlipUp)
				{
					base.transform.eulerAngles = Vector3.Lerp(CardDeck.Instance.FlipdownAngle, CardDeck.Instance.FlipupAngle, t);
				}
			}
			else
			{
				Vector3 position = BjMotionControl.Instance.RightHandPos();
				float num2 = KasaiUtility.SinRate01(timer - this.SwitchTime, this.CardSetTime - this.SwitchTime, false, false);
				this.m_IsMoveend = (timer > this.CardSetTime);
				if (is_player)
				{
					position.x -= CardDeck.Instance.RightOffset * num2;
				}
				else if (BjPlayer.Instance.IsturnEnd)
				{
					this.m_IsMoveend = (Dealer.Instance.LastCardPos.z < position.z);
					position.z = Mathf.Min(Dealer.Instance.LastCardPos.z, position.z);
				}
				base.transform.position = position;
				if (this.m_IsMoveend)
				{
					break;
				}
			}
			this.PosYFixed();
			yield return null;
		}
		this.PosYFixed();
		BlackjackGame.Instance.DealQueue.CardTimer = timer;
		this.MovingCallback(base.gameObject);
		yield break;
		yield break;
	}

	private IEnumerator CardOpen()
	{
		float timer = this.InitTime;
		if (this.CurrentState != Card.CardState.Open)
		{
			yield break;
		}
		bool near_hand = false;
		for (;;)
		{
			if (!near_hand)
			{
				near_hand = (timer > this.StartTime);
				if (near_hand)
				{
					BlackjackGame.Instance.CardSe.Play();
				}
			}
			else
			{
				base.transform.position = BjMotionControl.Instance.LeftHandPos();
				float t = KasaiUtility.SinRate01(timer - this.StartTime, this.m_OpenTime, false, false);
				base.transform.eulerAngles = Vector3.Lerp(CardDeck.Instance.FlipdownAngle, CardDeck.Instance.FlipupAngle, t);
				if (timer > this.CardSetTime)
				{
					break;
				}
			}
			this.PosYFixed();
			yield return null;
			timer += Time.deltaTime;
		}
		this.PosYFixed();
		Dealer.Instance.SetCardOffset();
		if (this.OpenCallback != null)
		{
			this.OpenCallback(base.gameObject);
		}
		yield break;
		yield break;
	}

	public void MoveToPosition(Card.FinishedMoving callback)
	{
		if (this.m_IsMoveend)
		{
			return;
		}
		base.transform.eulerAngles = CardDeck.Instance.FlipdownAngle;
		this.MovingCallback = callback;
		base.StartCoroutine(this.CardMove());
	}

	public void Open(Card.FinishedOpen callback)
	{
		this.OpenCallback = callback;
		this.CurrentState = Card.CardState.Open;
		base.StartCoroutine(this.CardOpen());
	}

	public const float m_CardWidth = 0.035f;

	public const float m_CardHeight = 0.05f;

	private Card.FinishedOpen OpenCallback;

	private Card.FinishedMoving MovingCallback;

	private bool m_IsMoveend;

	[HideInInspector]
	public Card.CardState CurrentState;

	[HideInInspector]
	public Vector3 EndPosition;

	[HideInInspector]
	public Transform CardSet;

	[HideInInspector]
	public CardData CardData;

	[SerializeField]
	[HideInInspector]
	private MeshRenderer m_MyRenderer;

	[HideInInspector]
	public float InitTime;

	[HideInInspector]
	public float StartTime;

	[HideInInspector]
	public float CardSetTime;

	[HideInInspector]
	public float SwitchTime;

	private float m_OpenTime;

	public enum CardState
	{
		FlipUp,
		FlipDown,
		Open
	}

	public delegate void FinishedMoving(GameObject card);

	public delegate void FinishedOpen(GameObject card);
}
