using System;
using UnityEngine;

[AddComponentMenu("NGUI/Interaction/Key Navigation2")]
public class UIKeyNavigation2 : MonoBehaviour
{
	protected virtual void OnEnable()
	{
		UIKeyNavigation2.listCate.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()
	{
		UIKeyNavigation2.listCate.Remove(this);
	}

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

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

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

	protected GameObject GetDown()
	{
		if (NGUITools.GetActive(this.onDown))
		{
			return this.onDown;
		}
		if (this.constraint == UIKeyNavigation2.Constraint.Horizontal || this.constraint == UIKeyNavigation2.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 = UIKeyNavigation2.GetCenter(base.gameObject);
		float num = float.MaxValue;
		GameObject result = 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 = UIKeyNavigation2.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 GetRight(Vector3 myDir, bool horizontal)
	{
		Transform transform = base.transform;
		myDir = transform.TransformDirection(myDir);
		Vector3 center = UIKeyNavigation2.GetCenter(base.gameObject);
		float num = float.MaxValue;
		GameObject gameObject = null;
		for (int i = 0; i < UIKeyNavigation3.listPartsType.size; i++)
		{
			UIKeyNavigation3 uikeyNavigation = UIKeyNavigation3.listPartsType[i];
			if (!(uikeyNavigation == this))
			{
				UIButton component = uikeyNavigation.GetComponent<UIButton>();
				if (!(component != null) || component.isEnabled)
				{
					Vector3 direction = UIKeyNavigation2.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;
						}
					}
				}
			}
		}
		if (gameObject == null)
		{
			for (int j = 0; j < UIKeyNavigation5.listSliderItem.size; j++)
			{
				UIKeyNavigation5 uikeyNavigation2 = UIKeyNavigation5.listSliderItem[j];
				if (!(uikeyNavigation2 == this))
				{
					UIButton component2 = uikeyNavigation2.GetComponent<UIButton>();
					if (!(component2 != null) || component2.isEnabled)
					{
						Vector3 direction2 = UIKeyNavigation2.GetCenter(uikeyNavigation2.gameObject) - center;
						float num3 = Vector3.Dot(myDir, direction2.normalized);
						if (num3 >= 0.707f)
						{
							direction2 = transform.InverseTransformDirection(direction2);
							if (horizontal)
							{
								direction2.y *= 2f;
							}
							else
							{
								direction2.x *= 2f;
							}
							float sqrMagnitude2 = direction2.sqrMagnitude;
							if (sqrMagnitude2 <= num)
							{
								gameObject = uikeyNavigation2.gameObject;
								num = sqrMagnitude2;
							}
						}
					}
				}
			}
		}
		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;
		switch (key)
		{
		case KeyCode.UpArrow:
			gameObject = this.GetUp();
			break;
		case KeyCode.DownArrow:
			gameObject = this.GetDown();
			break;
		case KeyCode.RightArrow:
			gameObject = this.GetRight();
			break;
		case KeyCode.LeftArrow:
			gameObject = this.GetLeft();
			break;
		default:
			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();
					}
				}
			}
			break;
		}
		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<UIKeyNavigation2> listCate = new BetterList<UIKeyNavigation2>();

	public UIKeyNavigation2.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
	}
}
