using System;
using UnityEngine;

[AddComponentMenu("NGUI/Interaction/Key Navigation4")]
public class UIKeyNavigation5 : MonoBehaviour
{
	protected virtual void OnEnable()
	{
		UIKeyNavigation5.listSliderItem.Add(this);
		if (this.startsSelected && (UICamera.selectedObject == null || !NGUITools.GetActive(UICamera.selectedObject)))
		{
			UICamera.currentScheme = UICamera.ControlScheme.Controller;
			UICamera.selectedObject = base.gameObject;
		}
	}

	protected virtual void OnDisable()
	{
		UIKeyNavigation5.listSliderItem.Remove(this);
	}

	protected GameObject GetLeft()
	{
		if (NGUITools.GetActive(this.onLeft))
		{
			return this.onLeft;
		}
		if (this.constraint == UIKeyNavigation5.Constraint.Vertical || this.constraint == UIKeyNavigation5.Constraint.Explicit)
		{
			return null;
		}
		return this.GetLeft(Vector3.left, true);
	}

	private GameObject GetRight()
	{
		if (NGUITools.GetActive(this.onRight))
		{
			return this.onRight;
		}
		if (this.constraint == UIKeyNavigation5.Constraint.Vertical || this.constraint == UIKeyNavigation5.Constraint.Explicit)
		{
			return null;
		}
		return this.Get(Vector3.right, true);
	}

	protected GameObject GetUp()
	{
		if (NGUITools.GetActive(this.onUp))
		{
			return this.onUp;
		}
		if (this.constraint == UIKeyNavigation5.Constraint.Horizontal || this.constraint == UIKeyNavigation5.Constraint.Explicit)
		{
			return null;
		}
		return this.Get(Vector3.up, false);
	}

	protected GameObject GetDown()
	{
		if (NGUITools.GetActive(this.onDown))
		{
			return this.onDown;
		}
		if (this.constraint == UIKeyNavigation5.Constraint.Horizontal || this.constraint == UIKeyNavigation5.Constraint.Explicit)
		{
			return null;
		}
		return this.Get(Vector3.down, false);
	}

	protected GameObject Get(Vector3 myDir, bool horizontal)
	{
		Transform transform = base.transform;
		myDir = transform.TransformDirection(myDir);
		Vector3 center = UIKeyNavigation5.GetCenter(base.gameObject);
		float num = float.MaxValue;
		GameObject result = null;
		for (int i = 0; i < UIKeyNavigation5.listSliderItem.size; i++)
		{
			UIKeyNavigation5 uikeyNavigation = UIKeyNavigation5.listSliderItem[i];
			if (!(uikeyNavigation == this))
			{
				UIButton component = uikeyNavigation.GetComponent<UIButton>();
				if (!(component != null) || component.isEnabled)
				{
					Vector3 direction = UIKeyNavigation5.GetCenter(uikeyNavigation.gameObject) - center;
					float num2 = Vector3.Dot(myDir, direction.normalized);
					if (num2 >= 0.707f)
					{
						direction = transform.InverseTransformDirection(direction);
						if (horizontal)
						{
							direction.y *= 2f;
						}
						else
						{
							direction.x *= 2f;
						}
						float sqrMagnitude = direction.sqrMagnitude;
						if (sqrMagnitude <= num)
						{
							result = uikeyNavigation.gameObject;
							num = sqrMagnitude;
						}
					}
				}
			}
		}
		return result;
	}

	protected GameObject GetLeft(Vector3 myDir, bool horizontal)
	{
		Transform transform = base.transform;
		myDir = transform.TransformDirection(myDir);
		Vector3 center = UIKeyNavigation5.GetCenter(base.gameObject);
		float num = float.MaxValue;
		GameObject gameObject = this.Get(myDir, horizontal);
		if (gameObject == null)
		{
			for (int i = 0; i < UIKeyNavigation2.listCate.size; i++)
			{
				UIKeyNavigation2 uikeyNavigation = UIKeyNavigation2.listCate[i];
				if (!(uikeyNavigation == this))
				{
					UIButton component = uikeyNavigation.GetComponent<UIButton>();
					if (!(component != null) || component.isEnabled)
					{
						Vector3 direction = UIKeyNavigation5.GetCenter(uikeyNavigation.gameObject) - center;
						float num2 = Vector3.Dot(myDir, direction.normalized);
						if (num2 >= 0.707f)
						{
							direction = transform.InverseTransformDirection(direction);
							if (horizontal)
							{
								direction.y *= 2f;
							}
							else
							{
								direction.x *= 2f;
							}
							float sqrMagnitude = direction.sqrMagnitude;
							if (sqrMagnitude <= num)
							{
								gameObject = uikeyNavigation.gameObject;
								num = sqrMagnitude;
							}
						}
					}
				}
			}
		}
		return gameObject;
	}

	protected static Vector3 GetCenter(GameObject go)
	{
		UIWidget component = go.GetComponent<UIWidget>();
		if (component != null)
		{
			Vector3[] worldCorners = component.worldCorners;
			return (worldCorners[0] + worldCorners[2]) * 0.5f;
		}
		return go.transform.position;
	}

	protected virtual void OnKey(KeyCode key)
	{
		if (!NGUITools.GetActive(this))
		{
			return;
		}
		GameObject gameObject = null;
		if (key != KeyCode.UpArrow)
		{
			if (key != KeyCode.DownArrow)
			{
				if (key != KeyCode.Joystick1Button0)
				{
					if (key != KeyCode.Joystick1Button1)
					{
						if (key == KeyCode.Tab)
						{
							if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
							{
								gameObject = this.GetLeft();
								if (gameObject == null)
								{
									gameObject = this.GetUp();
								}
								if (gameObject == null)
								{
									gameObject = this.GetDown();
								}
								if (gameObject == null)
								{
									gameObject = this.GetRight();
								}
							}
							else
							{
								gameObject = this.GetRight();
								if (gameObject == null)
								{
									gameObject = this.GetDown();
								}
								if (gameObject == null)
								{
									gameObject = this.GetUp();
								}
								if (gameObject == null)
								{
									gameObject = this.GetLeft();
								}
							}
						}
					}
					else
					{
						gameObject = this.GetLeft();
					}
				}
				else
				{
					gameObject = this.GetLeft();
				}
			}
			else
			{
				gameObject = this.GetDown();
			}
		}
		else
		{
			gameObject = this.GetUp();
		}
		if (gameObject != null)
		{
			UICamera.selectedObject = gameObject;
		}
	}

	protected virtual void OnClick()
	{
		if (NGUITools.GetActive(this) && NGUITools.GetActive(this.onClick))
		{
			UICamera.selectedObject = this.onClick;
		}
	}

	public static BetterList<UIKeyNavigation5> listSliderItem = new BetterList<UIKeyNavigation5>();

	public UIKeyNavigation5.Constraint constraint;

	public GameObject onUp;

	public GameObject onDown;

	public GameObject onLeft;

	public GameObject onRight;

	public GameObject onClick;

	public bool startsSelected;

	public enum Constraint
	{
		None,
		Vertical,
		Horizontal,
		Explicit
	}
}
