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

public class BingoSheetBlock : MonoBehaviour
{
	[SerializeField]
	private UISprite _numSpriteLeft;

	[SerializeField]
	private UISprite _numSpriteRight;

	[SerializeField]
	private UISprite _blockSprite;

	[SerializeField]
	private UISprite _treasureBoxSprite;

	[SerializeField]
	private UITexture _stampTexutre;

	[SerializeField]
	private UISprite _lightSprite;

	[SerializeField]
	private TweenAlpha _lightTextureAlpha;

	private List<Coroutine> _inProcessCoroutines = new List<Coroutine>();

	private const string NUM_SPRITE_NAME_HEAD = "bingo_square_";

	private const float LINES_NUM_SPRITE_SPACE = 16.65f;

	public void SetNumLabel(int num, int maxPerLine)
	{
		if (num >= 0 && num <= 9)
		{
			_numSpriteRight.gameObject.SetActive(value: false);
			_numSpriteLeft.gameObject.SetActive(value: true);
			_numSpriteLeft.transform.localPosition = Vector3.zero;
			_numSpriteLeft.spriteName = "bingo_square_" + num;
		}
		else if (num >= 10)
		{
			_numSpriteRight.gameObject.SetActive(value: true);
			_numSpriteLeft.gameObject.SetActive(value: true);
			_numSpriteLeft.spriteName = "bingo_square_" + num / 10;
			_numSpriteRight.spriteName = "bingo_square_" + num % 10;
			Vector3 zero = Vector3.zero;
			zero.x = -16.65f;
			_numSpriteLeft.transform.localPosition = zero;
			zero.x = 16.65f;
			_numSpriteRight.transform.localPosition = zero;
		}
	}

	public void SetBlockScale(float blockScale)
	{
		base.transform.localScale *= blockScale;
	}

	public void SetPartsVisible(bool blockVisible, int treasureBoxGrade, bool isOpen)
	{
		_blockSprite.gameObject.SetActive(blockVisible);
		SetTreasureBoxSprite(treasureBoxGrade, isOpen);
		SetStampVisible(isOpen);
	}

	public void SetStampTexture(Texture texture)
	{
		_stampTexutre.mainTexture = texture;
	}

	public void SetTreasureBoxSprite(int treasureBoxGrade, bool isOpen)
	{
		if (!isOpen)
		{
			_treasureBoxSprite.spriteName = string.Format("box_2pick_0{0}{1}", (treasureBoxGrade - 1).ToString(), "_close");
			_treasureBoxSprite.gameObject.SetActive(treasureBoxGrade > 0);
		}
		else
		{
			_treasureBoxSprite.gameObject.SetActive(value: false);
		}
	}

	public void SetStampVisible(bool visible)
	{
		_stampTexutre.gameObject.SetActive(visible);
	}

	public void StampDown(float delay)
	{
		iTween.ScaleTo(_stampTexutre.gameObject, iTween.Hash("scale", Vector3.one * 1f, "delay", delay, "time", 0.15f, "islocal", true, "easetype", iTween.EaseType.easeOutCubic));
		_inProcessCoroutines.Add(UIManager.GetInstance().StartCoroutine(ChangeAlpha(_stampTexutre.gameObject, delay)));
	}

	public void LightOn(float delay)
	{
		_lightTextureAlpha.SetOnFinished(delegate
		{
			_lightSprite.alpha = 0f;
			_lightTextureAlpha.ResetToBeginning();
			_lightTextureAlpha.from = 0f;
		});
		_inProcessCoroutines.Add(UIManager.GetInstance().StartCoroutine(LightOn(_lightTextureAlpha, delay)));
	}

	public void PlaySe(int lineNum, float delay)
	{
		_inProcessCoroutines.Add(UIManager.GetInstance().StartCoroutine(PlaySeCoroutine(lineNum, delay)));
	}

	private IEnumerator PlaySeCoroutine(int lineNum, float delay)
	{
		yield return new WaitForSeconds(delay);
		string text = string.Format("se_sys_bng_line_{0}", Mathf.Clamp(lineNum + 1, 1, 12).ToString("00"));
		GameMgr.GetIns().GetSoundMgr().PlaySeByStr(text, text, 0f, 0L);
	}

	private IEnumerator ChangeAlpha(GameObject obj, float delay)
	{
		yield return new WaitForSeconds(delay);
		TweenAlpha.Begin(obj, 0.15f, 1f);
	}

	private IEnumerator LightOn(TweenAlpha tweenAlpha, float delay)
	{
		_lightTextureAlpha.ResetToBeginning();
		yield return new WaitForSeconds(delay);
		tweenAlpha.PlayForward();
	}

	public void CancelAnimations()
	{
		_lightSprite.alpha = 0f;
		if (_inProcessCoroutines.Count <= 0)
		{
			return;
		}
		foreach (Coroutine inProcessCoroutine in _inProcessCoroutines)
		{
			UIManager.GetInstance().StopCoroutine(inProcessCoroutine);
		}
	}

	public void ResetStampTexture()
	{
		_stampTexutre.gameObject.transform.localScale = Vector3.one * 1.5f;
		_stampTexutre.alpha = 0f;
	}
}
