using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Valve.VR;

public class ViveControllerBehavior : AVRControllerBehavior
{
	public override AVRControllerBehavior.BEH_TYPE BehaviourType
	{
		get
		{
			return AVRControllerBehavior.BEH_TYPE.LEGACY;
		}
	}

	public override bool HandCameraMode
	{
		get
		{
			return this.m_eMode == ViveControllerBehavior.OvrControllerMode.CAMERA;
		}
		set
		{
			if (value)
			{
				this.ChangeMode(ViveControllerBehavior.OvrControllerMode.CAMERA);
			}
		}
	}

	public override bool IsHandCameraMode
	{
		get
		{
			return this.m_eMode == ViveControllerBehavior.OvrControllerMode.CAMERA;
		}
	}

	public override bool IsHandPenMode
	{
		get
		{
			return false;
		}
	}

	public override bool HandDanceMode
	{
		get
		{
			return this.m_bHandDanceMode && this.m_eMode == ViveControllerBehavior.OvrControllerMode.DANCE;
		}
		set
		{
			this.m_bHandDanceMode = value;
			if (value)
			{
				this.ChangeMode(ViveControllerBehavior.OvrControllerMode.DANCE);
			}
			else if (!value && this.m_eMode == ViveControllerBehavior.OvrControllerMode.DANCE)
			{
				this.ChangeMode(ViveControllerBehavior.OvrControllerMode.MOUSE_POINTER);
			}
		}
	}

	public override bool HandVRIKMode
	{
		get
		{
			return false;
		}
		set
		{
		}
	}

	public override bool HandYotogiMode
	{
		get
		{
			return false;
		}
		set
		{
		}
	}

	protected override void Awake()
	{
		base.Awake();
		GameMain.Instance.CMSystem.OvrPointerMode = Mathf.Max(Mathf.Min(0, GameMain.Instance.CMSystem.OvrPointerMode), 0);
		GameMain.Instance.CMSystem.OvrMoveMode = Mathf.Max(Mathf.Min(2, GameMain.Instance.CMSystem.OvrMoveMode), 0);
		OvrMgr.OvrObject ovr_obj = GameMain.Instance.OvrMgr.ovr_obj;
		this.m_trackedObject = base.GetComponent<SteamVR_TrackedObject>();
		this.m_txMode = base.transform.Find("UI/Canvas/Text").GetComponent<Text>();
		this.m_goModeCanvas = this.m_txMode.transform.parent.gameObject;
		this.m_goHandCamera = base.transform.Find("HandCamera").gameObject;
		this.m_HandCamera = this.m_goHandCamera.GetComponent<OvrHandCamera>();
		this.m_HandCamera.m_ctrl = this;
		this.m_trTrackedDeviceParent = base.transform.parent.Find("Tracked Devices");
		this.m_TrackObjs = this.m_trTrackedDeviceParent.GetComponentsInChildren<SteamVR_TrackedObject>(true);
		this.m_ArcTeleport = base.transform.Find("ArcTeleport").GetComponentInChildren<ArcTeleport>(true);
		this.m_ArcTeleport.SetCallBackOnMove(new ArcTeleport.dgOnMove(this.OnWarpMove));
		this.m_HandItem = base.transform.Find("HandItem").GetComponent<OvrHandItemMgr>();
		this.m_HandItem.m_ctrl = this;
		this.m_HandItem.gameObject.SetActive(false);
		this.m_goManHand = base.transform.Find("HandPlayer").gameObject;
		NDebug.Assert(this.m_goManHand != null, "HandPlayer 男の手が見つかりません。");
		this.m_goManHand.GetComponent<ViveHandAnimator>().Init(this.m_buttons);
		this.m_goManHand.SetActive(false);
		this.m_fCursorLaserEasing = GameMain.Instance.CMSystem.OvrViveCursorLaserEasing;
		if (this.m_bHandL)
		{
			this.ChangeMode(ViveControllerBehavior.OvrControllerMode.MOVE);
		}
		else
		{
			this.ChangeMode(ViveControllerBehavior.OvrControllerMode.MOUSE_POINTER);
		}
	}

	private void Start()
	{
		GameMain.Instance.OvrMgr.OvrCamera.m_bWheelToZoom = false;
		if (this.m_bHandL)
		{
			this.ChangeMode(ViveControllerBehavior.OvrControllerMode.MOVE);
		}
		else
		{
			this.ChangeMode(ViveControllerBehavior.OvrControllerMode.MOUSE_POINTER);
		}
	}

	private void OnEnable()
	{
		this.ChangeMode(this.m_eMode);
	}

	private void OnDisable()
	{
		this.m_HandItem.gameObject.SetActive(false);
	}

	private void ChangeMode(ViveControllerBehavior.OvrControllerMode f_eNewMode)
	{
		this.m_txMode.gameObject.SetActive(true);
		if (f_eNewMode == ViveControllerBehavior.OvrControllerMode.GRAB)
		{
			this.m_obj_controller.grip_collider.enabled = true;
		}
		else
		{
			this.m_obj_controller.grip_collider.enabled = false;
		}
		bool flag = false;
		if (f_eNewMode == ViveControllerBehavior.OvrControllerMode.CAMERA)
		{
			this.m_goHandCamera.SetActive(true);
			flag = true;
		}
		else
		{
			this.m_goHandCamera.SetActive(false);
		}
		if (f_eNewMode == ViveControllerBehavior.OvrControllerMode.ITEM)
		{
			this.m_HandItem.gameObject.SetActive(true);
			flag = true;
		}
		else
		{
			this.m_HandItem.gameObject.SetActive(false);
		}
		if (flag)
		{
			foreach (SteamVR_TrackedObject steamVR_TrackedObject in this.m_TrackObjs)
			{
				if (steamVR_TrackedObject.index == this.m_trackedObject.index)
				{
					steamVR_TrackedObject.gameObject.transform.localScale = new Vector3(0f, 0f, 0f);
				}
			}
			this.m_txMode.enabled = false;
			this.m_goManHand.SetActive(false);
		}
		else
		{
			if (this.m_eHandSubMode == ViveControllerBehavior.HandSubMode.CONTROLLER)
			{
				foreach (SteamVR_TrackedObject steamVR_TrackedObject2 in this.m_TrackObjs)
				{
					if (steamVR_TrackedObject2.index == this.m_trackedObject.index)
					{
						steamVR_TrackedObject2.gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
					}
				}
				this.m_goManHand.SetActive(false);
			}
			else if (this.m_eHandSubMode == ViveControllerBehavior.HandSubMode.MAN_HAND)
			{
				foreach (SteamVR_TrackedObject steamVR_TrackedObject3 in this.m_TrackObjs)
				{
					if (steamVR_TrackedObject3.index == this.m_trackedObject.index)
					{
						steamVR_TrackedObject3.gameObject.transform.localScale = new Vector3(0f, 0f, 0f);
					}
				}
				this.m_goManHand.SetActive(true);
			}
			this.m_txMode.enabled = true;
		}
		GameMain.Instance.OvrMgr.SystemUICamera.SetActiveVirtualCursorObjByNocurrent((!this.m_bHandL) ? UICamera.VCURSOR.RIGHT : UICamera.VCURSOR.LEFT, f_eNewMode == ViveControllerBehavior.OvrControllerMode.MOUSE_POINTER);
		this.m_txMode.text = this.m_strMode[(int)f_eNewMode];
		if (f_eNewMode == ViveControllerBehavior.OvrControllerMode.MOUSE_POINTER)
		{
			Text txMode = this.m_txMode;
			txMode.text += this.m_strPointerMode[GameMain.Instance.CMSystem.OvrPointerMode];
		}
		else if (f_eNewMode == ViveControllerBehavior.OvrControllerMode.MOVE)
		{
			Text txMode2 = this.m_txMode;
			txMode2.text += this.m_strMoveMode[GameMain.Instance.CMSystem.OvrMoveMode];
		}
		this.m_eMode = f_eNewMode;
		Debug.Log("コントローラモード変更:" + this.m_eMode.ToString());
	}

	private void ChangePointerMode(ViveControllerBehavior.PointerSubMode f_ePMode)
	{
		GameMain.Instance.CMSystem.OvrPointerMode = (int)f_ePMode;
		this.ChangeMode(ViveControllerBehavior.OvrControllerMode.MOUSE_POINTER);
	}

	private void ChangeMoveMode(ViveControllerBehavior.MoveSubMode f_eSubMode)
	{
		GameMain.Instance.CMSystem.OvrMoveMode = (int)f_eSubMode;
		this.ChangeMode(ViveControllerBehavior.OvrControllerMode.MOVE);
	}

	private void ChangeHandMode(ViveControllerBehavior.HandSubMode f_eSubMode)
	{
		this.m_eHandSubMode = f_eSubMode;
		this.ChangeMode(ViveControllerBehavior.OvrControllerMode.GRAB);
	}

	private void TouchPadToClick()
	{
		if (this.m_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_L_CLICK))
		{
			this.m_bCursorMoveEnable = false;
			GameMain.Instance.OvrMgr.SystemUICamera.SetCurrentCursorSide((!this.m_bHandL) ? UICamera.VCURSOR.RIGHT : UICamera.VCURSOR.LEFT);
			NUty.EmurateMouseButton(2);
			if (this.m_coCursorMoveWait != null)
			{
				base.StopCoroutine(this.m_coCursorMoveWait);
			}
			this.m_coCursorMoveWait = base.StartCoroutine(this.CoCursorMoveWait());
		}
		else if (this.m_buttons.GetPressUp(AVRControllerButtons.BTN.VIRTUAL_L_CLICK))
		{
			NUty.EmurateMouseButton(4);
		}
		if (this.m_buttons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_R_CLICK))
		{
			this.m_bCursorMoveEnable = false;
			GameMain.Instance.OvrMgr.SystemUICamera.SetCurrentCursorSide((!this.m_bHandL) ? UICamera.VCURSOR.RIGHT : UICamera.VCURSOR.LEFT);
			NUty.EmurateMouseButton(8);
			if (this.m_coCursorMoveWait != null)
			{
				base.StopCoroutine(this.m_coCursorMoveWait);
			}
			this.m_coCursorMoveWait = base.StartCoroutine(this.CoCursorMoveWait());
		}
		else if (this.m_buttons.GetPressUp(AVRControllerButtons.BTN.VIRTUAL_R_CLICK))
		{
			NUty.EmurateMouseButton(16);
		}
	}

	private IEnumerator CoCursorMoveWait()
	{
		yield return new WaitForSeconds(0.3f);
		this.m_bCursorMoveEnable = true;
		yield break;
	}

	private void TouchPadWheel()
	{
		if (this.m_device.GetTouchDown(4294967296UL))
		{
			this.m_fTrackPadBefY = this.m_device.GetAxis(EVRButtonId.k_EButton_Axis0).y;
		}
		else if (this.m_device.GetTouch(4294967296UL))
		{
			Vector2 axis = this.m_device.GetAxis(EVRButtonId.k_EButton_Axis0);
			float num = axis.y - this.m_fTrackPadBefY;
			if (0.1f < Mathf.Abs(num))
			{
				NUty.EmurateMouseWheel((int)(num * 10f));
				this.m_fTrackPadBefY = axis.y;
			}
		}
	}

	private void OnWarpMove(Vector3 f_vPosWorld, Vector3 f_vRotWorld)
	{
		GameMain.Instance.MainCamera.SetFootPos(f_vPosWorld, CameraMain.STAND_SIT.STAND);
		GameMain.Instance.MainCamera.SetRealHeadRot(f_vRotWorld, true);
	}

	protected virtual void Update()
	{
		this.m_device = SteamVR_Controller.Input((int)this.m_trackedObject.index);
		this.m_vMoveCam.x = (this.m_vMoveCam.y = (this.m_vMoveCam.z = (this.m_fRotCam = 0f)));
		this.m_vMoveVelocity.x = (this.m_vMoveVelocity.y = (this.m_vMoveVelocity.z = 0f));
		if (this.m_device.GetPressDown(2UL))
		{
			this.m_bPressMenuBtnLong = false;
			this.m_fMenuPressBeforeTime = Time.realtimeSinceStartup;
		}
		else if (this.m_device.GetPress(2UL))
		{
			if (!this.m_bPressMenuBtnLong && Time.realtimeSinceStartup - this.m_fMenuPressBeforeTime > 1.5f)
			{
				if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.MOUSE_POINTER)
				{
					int num = GameMain.Instance.CMSystem.OvrPointerMode + 1;
					if (1 <= num)
					{
						num = 0;
					}
					this.ChangePointerMode((ViveControllerBehavior.PointerSubMode)num);
				}
				else if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.MOVE)
				{
					int num2 = GameMain.Instance.CMSystem.OvrMoveMode + 1;
					if (3 <= num2)
					{
						num2 = 0;
					}
					this.ChangeMoveMode((ViveControllerBehavior.MoveSubMode)num2);
				}
				else if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.ITEM)
				{
					this.m_HandItem.NextModel();
				}
				else if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.GRAB)
				{
					ViveControllerBehavior.HandSubMode handSubMode = this.m_eHandSubMode + 1;
					if (handSubMode >= ViveControllerBehavior.HandSubMode.MAX)
					{
						handSubMode = ViveControllerBehavior.HandSubMode.CONTROLLER;
					}
					this.ChangeHandMode(handSubMode);
				}
				this.m_bPressMenuBtnLong = true;
			}
		}
		else if (this.m_device.GetPressUp(2UL) && !this.m_bPressMenuBtnLong)
		{
			if (this.m_device.GetPress(4UL))
			{
				GameMain.Instance.OvrMgr.OvrCamera.ReCallcOffset();
				GameMain.Instance.OvrMgr.OvrCamera.ShowUI(true);
				GameMain.Instance.OvrMgr.OvrCamera.UIPosReset(0f);
			}
			else
			{
				int num3 = (int)(this.m_eMode + 1);
				if (num3 == 5 && !this.m_bHandDanceMode)
				{
					num3++;
				}
				if (6 <= num3)
				{
					num3 = 0;
				}
				this.ChangeMode((ViveControllerBehavior.OvrControllerMode)num3);
			}
		}
		if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.MOUSE_POINTER)
		{
			if (GameMain.Instance.CMSystem.OvrPointerMode == 0)
			{
				RaycastHit raycastHit;
				if (this.m_bCursorMoveEnable && Physics.Raycast(base.transform.position, base.transform.rotation * Vector3.forward, out raycastHit, float.PositiveInfinity, 33554432))
				{
					Vector2 textureCoord = raycastHit.textureCoord;
					Vector2 a = new Vector2(1280f * ((textureCoord.x - this.m_fUiCursorMargin) / (1f - this.m_fUiCursorMargin * 2f)), 720f * ((textureCoord.y - this.m_fUiCursorMargin) / (1f - this.m_fUiCursorMargin * 2f)));
					Vector2 vector = GameMain.Instance.OvrMgr.SystemUICamera.GetOvrVirtualMousePos(this.m_bHandL);
					Vector2 a2 = a - vector;
					Vector2 b = a2 * this.m_fCursorLaserEasing;
					Vector2 v = vector + b;
					GameMain.Instance.OvrMgr.SystemUICamera.SetOvrVirtualMousePos(this.m_bHandL, v);
				}
				Debug.DrawLine(base.transform.position, base.transform.position + base.transform.rotation * (Vector3.forward * 10f), new Color(1f, 0f, 0f));
				this.TouchPadToClick();
				this.TouchPadWheel();
				if (this.m_device.GetPressUp(4UL))
				{
					GameMain.Instance.OvrMgr.OvrCamera.ToggleUI();
				}
			}
		}
		else if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.MOVE)
		{
			if (GameMain.Instance.CMSystem.OvrMoveMode == 0)
			{
				Vector2 vector2 = this.m_device.GetAxis(EVRButtonId.k_EButton_Axis0) * this.m_fMoveSpeed;
				if (this.m_device.GetPress(4294967296UL))
				{
					if (this.m_device.GetTouch(8589934592UL))
					{
						this.m_vMoveCam.y = vector2.y;
					}
					else if (this.m_device.GetPress(4UL))
					{
						this.m_fRotCam = vector2.x;
					}
					else
					{
						this.m_vMoveCam.x = -vector2.x;
						this.m_vMoveCam.z = vector2.y;
					}
				}
			}
			if (GameMain.Instance.CMSystem.OvrMoveMode == 2)
			{
			}
		}
		else if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.CAMERA)
		{
			if (this.m_device.GetTouchDown(8589934592UL))
			{
				this.m_HandCamera.Snap();
			}
			if (this.m_device.GetPressUp(4UL))
			{
				GameMain.Instance.OvrMgr.OvrCamera.ToggleUI();
			}
		}
	}

	public override Vector3 GetVelocityHand()
	{
		Vector3 result = default(Vector3);
		if (this.m_device == null)
		{
			return result;
		}
		if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.MOVE && GameMain.Instance.CMSystem.OvrMoveMode == 1)
		{
			if (this.m_device.GetTouchDown(8589934592UL))
			{
				this.m_vMoveTugPosBack = base.transform.position;
			}
			else if (this.m_device.GetTouch(8589934592UL))
			{
				if (this.m_goTestObj != null)
				{
					this.m_goTestObj.transform.position = base.transform.position;
				}
				Vector3 a = base.transform.position - this.m_vMoveTugPosBack;
				result = a * -1f;
			}
		}
		return result;
	}

	public override float GetRotHandY()
	{
		float num = 0f;
		if (this.m_device == null)
		{
			return num;
		}
		if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.MOVE && GameMain.Instance.CMSystem.OvrMoveMode == 1)
		{
			if (this.m_device.GetTouchDown(4UL))
			{
				Transform baseHeadTransform = GameMain.Instance.OvrMgr.OvrCamera.GetBaseHeadTransform();
				Vector3 position = baseHeadTransform.position;
				position.y = 0f;
				Vector3 vector = baseHeadTransform.rotation * Vector3.forward;
				vector.y = 0f;
				Vector3 position2 = base.transform.position;
				position2.y = 0f;
				Vector3 vector2 = position2 - position;
				this.m_fRotBack = Quaternion.FromToRotation(vector.normalized, vector2.normalized).eulerAngles.y;
			}
			else if (this.m_device.GetTouch(4UL))
			{
				Transform baseHeadTransform2 = GameMain.Instance.OvrMgr.OvrCamera.GetBaseHeadTransform();
				Vector3 position3 = baseHeadTransform2.position;
				position3.y = 0f;
				Vector3 vector3 = baseHeadTransform2.rotation * Vector3.forward;
				vector3.y = 0f;
				Vector3 position4 = base.transform.position;
				position4.y = 0f;
				Vector3 vector4 = position4 - position3;
				num = Quaternion.FromToRotation(vector3.normalized, vector4.normalized).eulerAngles.y - this.m_fRotBack;
				num *= -1f;
			}
		}
		return num;
	}

	protected virtual void LateUpdate()
	{
		if (this.m_device == null)
		{
			return;
		}
		if (this.m_eMode == ViveControllerBehavior.OvrControllerMode.MOVE && GameMain.Instance.CMSystem.OvrMoveMode == 1 && this.m_device.GetTouch(4UL))
		{
			Transform baseHeadTransform = GameMain.Instance.OvrMgr.OvrCamera.GetBaseHeadTransform();
			Vector3 position = baseHeadTransform.position;
			position.y = 0f;
			Vector3 vector = baseHeadTransform.rotation * Vector3.forward;
			vector.y = 0f;
			Vector3 position2 = base.transform.position;
			position2.y = 0f;
			Vector3 vector2 = position2 - position;
			this.m_fRotBack = Quaternion.FromToRotation(vector.normalized, vector2.normalized).eulerAngles.y;
		}
	}

	public float m_fPointerSpeed = 10f;

	public float m_fMoveSpeed = 10f;

	public Vector3 m_vMoveVelocity;

	public float m_fMenuPressBeforeTime;

	public float m_fCursorLaserEasing = 0.3f;

	public float m_fUiCursorMargin = 0.2f;

	public GameObject m_goTestObj;

	private readonly string[] m_strMode = new string[]
	{
		"GRAB",
		"POINTER",
		"MOVE",
		"CAMERA",
		"ITEM",
		"DANCE"
	};

	private readonly string[] m_strPointerMode = new string[]
	{
		"-LASER"
	};

	private readonly string[] m_strMoveMode = new string[]
	{
		"-DIR",
		"-DRAW",
		"-ARC"
	};

	[NonSerialized]
	private ViveControllerBehavior.OvrControllerMode m_eMode;

	protected SteamVR_TrackedObject m_trackedObject;

	protected Transform m_trTrackedDeviceParent;

	protected SteamVR_TrackedObject[] m_TrackObjs;

	protected SteamVR_Controller.Device m_device;

	protected bool m_bPressLeft;

	protected float m_fTrackPadBefY;

	protected Text m_txMode;

	protected GameObject m_goModeCanvas;

	protected Vector2 m_vPadStartPos;

	protected bool m_bPressMenuBtnLong;

	protected GameObject m_goHandCamera;

	protected OvrHandCamera m_HandCamera;

	protected Vector3 m_vMoveTugPosBack;

	protected Vector3 m_vMoveTugRotHnadPosBack;

	protected Vector3 m_vMoveTugRotHeadPosBack;

	protected Vector3 m_vMoveTugVector;

	protected ArcTeleport m_ArcTeleport;

	protected OvrHandItemMgr m_HandItem;

	protected bool m_bCursorMoveEnable = true;

	protected float m_fRotBack;

	protected Coroutine m_coCursorMoveWait;

	protected GameObject m_goManHand;

	[NonSerialized]
	private ViveControllerBehavior.HandSubMode m_eHandSubMode = ViveControllerBehavior.HandSubMode.MAN_HAND;

	private enum OvrControllerMode
	{
		GRAB,
		MOUSE_POINTER,
		MOVE,
		CAMERA,
		ITEM,
		DANCE,
		MAX
	}

	private enum PointerSubMode
	{
		LASER,
		MAX
	}

	private enum MoveSubMode
	{
		DIRECTIONAL,
		DRAW,
		ARC,
		MAX
	}

	private enum TouchSubMode
	{
		MAN,
		WOMAN
	}

	private enum HandSubMode
	{
		CONTROLLER,
		MAN_HAND,
		MAX
	}
}
