using System;
using UnityEngine;
using UnityEngine.Events;

public class CircleCommandUI : MonoBehaviour
{
	public bool isClockwise
	{
		get
		{
			return this.m_IsClockwise;
		}
		set
		{
			this.m_IsClockwise = value;
			this.UpdateItemPosition();
		}
	}

	public uGUIListViewer listViewer
	{
		get
		{
			return this.m_ListViewer;
		}
	}

	public float circleRadius
	{
		get
		{
			return this.m_CircleRadius;
		}
		set
		{
			this.m_CircleRadius = value;
			this.UpdateItemPosition();
		}
	}

	public float offsetAngle
	{
		get
		{
			return this.m_OffsetAngle;
		}
		set
		{
			this.m_OffsetAngle = value;
			this.UpdateItemPosition();
		}
	}

	public CircleCommandUI.OnSelect onSelect
	{
		get
		{
			return this.m_OnSelect;
		}
	}

	public CircleCommandUI.OnDeselect onDeselect
	{
		get
		{
			return this.m_OnDeselect;
		}
	}

	public CircleCommandUI.OnDecide onDecide
	{
		get
		{
			return this.m_OnDecide;
		}
	}

	public Func<bool> funcDecide { get; set; }

	public GameObject lastSelectItem
	{
		get
		{
			return this.m_LastSelectItem;
		}
		private set
		{
			this.m_LastSelectItem = value;
		}
	}

	public GameObject nowSelectItem
	{
		get
		{
			return this.m_NowSelectItem;
		}
		private set
		{
			this.m_NowSelectItem = value;
		}
	}

	private void OnValidate()
	{
		if (!Application.isPlaying)
		{
			return;
		}
		if (this.isApplicationQuit)
		{
			return;
		}
		this.UpdateItemPosition();
	}

	private void OnApplicationQuit()
	{
		this.isApplicationQuit = true;
	}

	public void Show<T>(int itemCount, Action<int, T> callbackSetUp = null) where T : Component
	{
		this.m_ListViewer.Show<T>(itemCount, callbackSetUp);
		this.UpdateItemPosition();
	}

	private void UpdateItemPosition()
	{
		GameObject[] itemArray = this.m_ListViewer.ItemArray;
		if (itemArray == null)
		{
			return;
		}
		Transform parentItemArea = this.m_ListViewer.parentItemArea;
		float num = 360f / (float)itemArray.Length;
		float num2 = this.offsetAngle;
		if (this.isClockwise)
		{
			num *= -1f;
		}
		foreach (GameObject gameObject in itemArray)
		{
			Transform transform = gameObject.transform;
			Vector3 vector = Vector3.zero;
			vector.x = Mathf.Cos(num2 * 0.0174532924f);
			vector.y = Mathf.Sin(num2 * 0.0174532924f) * 0.75f;
			vector.Normalize();
			vector *= this.circleRadius;
			transform.localPosition = vector;
			num2 += num;
		}
	}

	private void Update()
	{
		if (this.funcDecide == null)
		{
			return;
		}
		if (this.funcDecide())
		{
			this.onDecide.Invoke(this.nowSelectItem);
			this.nowSelectItem = null;
			this.lastSelectItem = null;
		}
	}

	public void UpdateSelect(Vector2 axis)
	{
		GameObject[] itemArray = this.m_ListViewer.ItemArray;
		if (itemArray == null)
		{
			return;
		}
		Transform parentItemArea = this.m_ListViewer.parentItemArea;
		GameObject gameObject = null;
		Vector2 vector = Vector2.one * float.PositiveInfinity;
		foreach (GameObject gameObject2 in itemArray)
		{
			Vector2 a = gameObject2.transform.localPosition;
			Vector2 vector2 = a - axis;
			if (vector2.sqrMagnitude < vector.sqrMagnitude)
			{
				vector = vector2;
				gameObject = gameObject2;
			}
		}
		if (this.nowSelectItem != gameObject)
		{
			this.lastSelectItem = this.nowSelectItem;
			this.nowSelectItem = gameObject;
			this.onDeselect.Invoke(this.lastSelectItem);
			this.onSelect.Invoke(this.nowSelectItem);
		}
	}

	[SerializeField]
	private bool m_IsClockwise;

	[SerializeField]
	private uGUIListViewer m_ListViewer;

	[SerializeField]
	private float m_CircleRadius;

	[SerializeField]
	private float m_OffsetAngle;

	[SerializeField]
	private CircleCommandUI.OnSelect m_OnSelect = new CircleCommandUI.OnSelect();

	[SerializeField]
	private CircleCommandUI.OnDeselect m_OnDeselect = new CircleCommandUI.OnDeselect();

	[SerializeField]
	private CircleCommandUI.OnDecide m_OnDecide = new CircleCommandUI.OnDecide();

	private GameObject m_LastSelectItem;

	private GameObject m_NowSelectItem;

	private bool isApplicationQuit;

	[Serializable]
	public class OnSelect : UnityEvent<GameObject>
	{
	}

	[Serializable]
	public class OnDeselect : UnityEvent<GameObject>
	{
	}

	[Serializable]
	public class OnDecide : UnityEvent<GameObject>
	{
	}
}
