using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;

namespace Wizard;

[ExecuteAlways]
public class TweenAnimationGroup : MonoBehaviour
{
	private List<TweenAnimation> _tweenAnimationList = new List<TweenAnimation>();

	[SerializeField]
	private ParticleSystem _effect;

	[SerializeField]
	private float _effectDelayTime;

	[SerializeField]
	private GameObject[] _gameObjects;

	public void Play(Action onComplete)
	{
		CollectTweenAnimation();
		foreach (TweenAnimation tweenAnimation in _tweenAnimationList)
		{
			tweenAnimation.Play();
		}
		if (UIManager.GetInstance() != null)
		{
			UIManager.GetInstance().StartCoroutine(OnPlayComplete(onComplete));
			UIManager.GetInstance().StartCoroutine(PlayEffect(_effectDelayTime));
		}
		else
		{
			StartCoroutine(OnPlayComplete(onComplete));
			StartCoroutine(PlayEffect(_effectDelayTime));
		}
	}

	private IEnumerator OnPlayComplete(Action onComplete)
	{
		if (onComplete != null)
		{
			while (!AnimationAllEnd())
			{
				yield return null;
			}
			onComplete.Call();
		}
	}

	private IEnumerator PlayEffect(float delay)
	{
		if (!(_effect == null))
		{
			_effect.gameObject.SetActive(value: false);
			_effect.gameObject.transform.position = base.gameObject.transform.position;
			yield return new WaitForSeconds(delay);
			_effect.gameObject.SetActive(value: true);
		}
	}

	private bool AnimationAllEnd()
	{
		bool flag = true;
		foreach (TweenAnimation tweenAnimation in _tweenAnimationList)
		{
			flag &= tweenAnimation.IsPlayEnd();
		}
		return flag;
	}

	private void CollectTweenAnimation()
	{
		_tweenAnimationList = base.gameObject.GetComponentsInChildren<TweenAnimation>().ToList();
	}

	public GameObject GetChildObject(int index)
	{
		if (index < _gameObjects.Length)
		{
			return _gameObjects[index];
		}
		return null;
	}
}
