using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[RequireComponent(typeof(Animation))]
public class uGUISelectableAnimation : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IEventSystemHandler
{
	private void Start()
	{
		this.OnReset();
	}

	public virtual void OnPointerEnter(PointerEventData eventData)
	{
		if (!this.m_AnimationButtonPuyo)
		{
			return;
		}
		if (this.m_PtrSelectable && (!this.m_PtrSelectable.interactable || !this.m_PtrSelectable.enabled))
		{
			return;
		}
		CanvasGroup canvasGroup = this.GetCanvasGroup();
		if (canvasGroup != null && !canvasGroup.interactable)
		{
			return;
		}
		IEnumerator enumerator = this.m_AnimationButtonPuyo.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				AnimationState animationState = (AnimationState)obj;
				animationState.speed = 1f;
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		this.m_AnimationButtonPuyo.Play();
	}

	public virtual void OnPointerExit(PointerEventData eventData)
	{
		if (!this.m_AnimationButtonPuyo)
		{
			return;
		}
		if (this.m_PtrSelectable && (!this.m_PtrSelectable.interactable || !this.m_PtrSelectable.enabled))
		{
			return;
		}
		CanvasGroup canvasGroup = this.GetCanvasGroup();
		if (canvasGroup != null && !canvasGroup.interactable)
		{
			return;
		}
		IEnumerator enumerator = this.m_AnimationButtonPuyo.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				AnimationState animationState = (AnimationState)obj;
				if (animationState.speed < 0f)
				{
					return;
				}
				animationState.speed = -1f;
				if (animationState.normalizedTime <= 0f)
				{
					animationState.normalizedTime = 1f;
				}
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		this.m_AnimationButtonPuyo.Play();
	}

	public virtual void OnPointerClick(PointerEventData eventData)
	{
		if (eventData.button != PointerEventData.InputButton.Left)
		{
			return;
		}
		if (!this.m_AnimationButtonPuyo)
		{
			return;
		}
		if (this.m_PtrSelectable && (!this.m_PtrSelectable.interactable || !this.m_PtrSelectable.enabled))
		{
			return;
		}
		CanvasGroup canvasGroup = this.GetCanvasGroup();
		if (canvasGroup != null && !canvasGroup.interactable)
		{
			return;
		}
		IEnumerator enumerator = this.m_AnimationButtonPuyo.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				object obj = enumerator.Current;
				AnimationState animationState = (AnimationState)obj;
				if (animationState.speed < 0f)
				{
					return;
				}
				animationState.speed = -1f;
				if (animationState.normalizedTime <= 0f)
				{
					animationState.normalizedTime = 1f;
				}
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		this.m_AnimationButtonPuyo.Play();
	}

	private CanvasGroup GetCanvasGroup()
	{
		return base.GetComponentInParent<CanvasGroup>();
	}

	private void OnReset()
	{
		if (!this.m_AnimationButtonPuyo)
		{
			this.m_AnimationButtonPuyo = base.GetComponent<Animation>();
			this.m_AnimationButtonPuyo.enabled = true;
			this.m_AnimationButtonPuyo.playAutomatically = false;
			this.m_AnimationButtonPuyo.clip = Resources.Load<AnimationClip>("SceneEdit/MainMenu/Animation/ButtonPuyo");
		}
		this.m_PtrSelectable = base.GetComponentInChildren<Selectable>();
		if (this.m_PtrSelectable && !this.m_PtrSelectable.gameObject.GetComponent<uGUISelectableSound>())
		{
			this.m_PtrSelectable.gameObject.AddComponent<uGUISelectableSound>();
		}
	}

	[SerializeField]
	[HideInInspector]
	private Animation m_AnimationButtonPuyo;

	[SerializeField]
	[HideInInspector]
	private Selectable m_PtrSelectable;
}
