using System;
using System.Collections.Generic;
using UnityEngine;
using wf;

[RequireComponent(typeof(VelocityTracker))]
public class OvrGripCollider : MonoBehaviour
{
	public virtual void Init(AVRControllerButtons f_ctrl)
	{
		this.ctrl_btn_ = f_ctrl;
		this.collder_ = base.gameObject.GetComponent<SphereCollider>();
		if (this.collder_ == null)
		{
			this.collder_ = base.gameObject.AddComponent<SphereCollider>();
		}
		float radius = 0.1f;
		this.collder_.radius = radius;
		this.collder_.isTrigger = true;
		this.rigidbody_ = base.gameObject.GetComponent<Rigidbody>();
		if (this.rigidbody_ == null)
		{
			this.rigidbody_ = base.gameObject.AddComponent<Rigidbody>();
		}
		this.rigidbody_.useGravity = false;
		this.rigidbody_.isKinematic = true;
		this.velocity_tracker_ = base.gameObject.GetComponent<VelocityTracker>();
		if (this.velocity_tracker_ == null)
		{
			this.velocity_tracker_ = base.gameObject.AddComponent<VelocityTracker>();
		}
		this.velocity_tracker_.m_showGizmos = false;
		this.trans_dummy_ = base.gameObject.transform.Find("trans_dummy");
		if (this.trans_dummy_ == null)
		{
			this.trans_dummy_ = new GameObject
			{
				name = "trans_dummy"
			}.transform;
			this.trans_dummy_.SetParent(base.transform, false);
		}
		base.gameObject.layer = 18;
	}

	public virtual void Update()
	{
		if (this.ctrl_btn_ != null)
		{
			if (this.ctrl_btn_.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_GRUB))
			{
				this.press_down_trigger_ = true;
			}
			if (!this.ctrl_btn_.GetPress(AVRControllerButtons.BTN.VIRTUAL_GRUB))
			{
				if (this.mode_scale_control_)
				{
					this.pair_obj_.press_down_trigger_ = false;
					if (this.pair_obj_.ResetGrip())
					{
						this.pair_obj_.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
					}
				}
				this.press_down_trigger_ = false;
				if (this.ResetGrip())
				{
					this.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
				}
				return;
			}
		}
		if (!this.grip)
		{
			return;
		}
		if (this.mode_scale_control_)
		{
			float num = Vector3.Distance(this.pair_object.transform.position, base.transform.position) / this.scale_distance_;
			float num2 = wf.Math.RoundMinMax(this.init_scale_ * num, this.scale_min_max_.x, this.scale_min_max_.y);
			this.lock_object_trans_.localScale = new Vector3(num2, num2, num2);
			WorldTransformAxis component = this.lock_object_trans_.GetComponent<WorldTransformAxis>();
			if (component != null)
			{
				component.Apply();
			}
		}
		else if (this.m_bEnableGrab)
		{
			this.lock_object_trans_.position = this.trans_dummy_.position;
			this.lock_object_trans_.rotation = this.trans_dummy_.rotation;
			WorldTransformAxis component2 = this.lock_object_trans_.GetComponent<WorldTransformAxis>();
			if (component2 != null)
			{
				component2.Apply();
			}
		}
	}

	public virtual bool ResetGrip()
	{
		if (this.trans_dummy_ == null)
		{
			return false;
		}
		this.mode_scale_control_ = false;
		this.m_bEnableGrab = true;
		bool grip = this.grip;
		GameObject gripObject = null;
		if (grip)
		{
			gripObject = this.lock_object_trans_.gameObject;
		}
		if (this.lock_object_trans_ != null)
		{
			WorldTransformAxis component = this.lock_object_trans_.GetComponent<WorldTransformAxis>();
			if (component != null)
			{
				component.is_grip = false;
			}
		}
		this.lock_object_trans_ = null;
		if (this.trans_dummy_.transform.parent != base.gameObject.transform)
		{
			this.trans_dummy_.SetParent(base.gameObject.transform, false);
		}
		if (grip && this.on_release_object_ != null)
		{
			this.on_release_object_(gripObject, this.velocity_tracker_.TrackedLinearVelocity, this.velocity_tracker_.TrackedAngularVelocity);
		}
		return grip;
	}

	public virtual void OnTriggerEnter(Collider other)
	{
		this.m_listCollider.RemoveAll((GameObject a) => a == null);
		if (this.m_listCollider.Find((GameObject a) => a.GetInstanceID() == other.gameObject.GetInstanceID()) == null)
		{
			this.m_listCollider.Add(other.gameObject);
		}
	}

	protected virtual void OnTriggerStay(Collider col)
	{
		if (this.grip || this.mode_scale_control_)
		{
			return;
		}
		if (this.press_down_trigger_)
		{
			this.press_down_trigger_ = false;
			Transform transform = this.GetTargetTransform(col.gameObject);
			if (transform == null)
			{
				transform = this.GetTargetTransformComm(col.gameObject);
				if (transform == null)
				{
					return;
				}
			}
			if (this.pair_obj_ != null && this.pair_obj_.lock_object_trans_ != null && this.pair_obj_.lock_object_trans_ == transform)
			{
				GameObject worldTransformAxisParent = PhotoWindowManager.GetWorldTransformAxisParent();
				if (worldTransformAxisParent != null)
				{
					bool flag = transform.GetComponent<WorldTransformAxis>() != null;
					if (!flag)
					{
						WorldTransformAxis[] componentsInChildren = worldTransformAxisParent.GetComponentsInChildren<WorldTransformAxis>(true);
						foreach (WorldTransformAxis worldTransformAxis in componentsInChildren)
						{
							if (!(worldTransformAxis.TargetObject == null))
							{
								if (worldTransformAxis.TargetObject.transform == this.pair_obj_.lock_object_trans_ && worldTransformAxis.ScaleMinMax.x != worldTransformAxis.ScaleMinMax.y)
								{
									this.scale_min_max_ = worldTransformAxis.ScaleMinMax;
									flag = true;
									break;
								}
							}
						}
					}
					else
					{
						this.scale_min_max_ = transform.GetComponent<WorldTransformAxis>().ScaleMinMax;
						if (this.scale_min_max_.x == this.scale_min_max_.y)
						{
							flag = false;
						}
					}
					if (flag)
					{
						this.init_scale_ = this.pair_obj_.lock_object_trans_.localScale.x;
						this.scale_distance_ = Vector3.Distance(this.pair_obj_.transform.position, base.transform.position);
						this.ResetGrip();
						this.pair_obj_.ResetGrip();
						this.lock_object_trans_ = transform;
						this.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
						this.pair_obj_.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
						this.pair_obj_.mode_scale_control_ = (this.mode_scale_control_ = true);
					}
				}
				return;
			}
			this.ResetGrip();
			this.lock_object_trans_ = transform;
			this.ctrl_btn_.Haptic(this.m_byHaptic, this.m_fTime);
			WorldTransformAxis component = this.lock_object_trans_.GetComponent<WorldTransformAxis>();
			if (component != null)
			{
				component.is_grip = true;
			}
			this.m_bEnableGrab = this.IsEnableGrab(col.gameObject);
			if (this.m_bEnableGrab)
			{
				this.trans_dummy_.SetParent(base.gameObject.transform, false);
				this.trans_dummy_.SetParent(this.lock_object_trans_.parent, true);
				this.trans_dummy_.localPosition = this.lock_object_trans_.localPosition;
				this.trans_dummy_.localRotation = this.lock_object_trans_.localRotation;
				this.trans_dummy_.SetParent(base.gameObject.transform, true);
			}
			if (this.on_grab_object_ != null)
			{
				this.on_grab_object_(col.gameObject);
			}
		}
	}

	protected virtual void OnTriggerExit(Collider other)
	{
		this.m_listCollider.RemoveAll((GameObject a) => a == null);
		GameObject gameObject = this.m_listCollider.Find((GameObject g) => g.GetInstanceID() == other.gameObject.GetInstanceID());
		if (gameObject != null)
		{
			this.m_listCollider.Remove(gameObject);
		}
	}

	protected virtual Transform GetTargetTransform(GameObject obj)
	{
		if (obj == null || obj.transform == null)
		{
			return null;
		}
		bool flag = false;
		if (obj.layer == 17)
		{
			flag = true;
		}
		string b = "AllOffset";
		Transform transform = null;
		if (flag)
		{
			transform = obj.transform;
			while (!(transform == null) && !(transform.parent == null))
			{
				if (transform.parent.gameObject.name == b)
				{
					return transform;
				}
				transform = transform.parent;
			}
			return null;
		}
		GameObject worldTransformAxisParent = PhotoWindowManager.GetWorldTransformAxisParent();
		if (worldTransformAxisParent != null)
		{
			Transform transform2 = obj.transform;
			while (!(transform2 == null) && !(transform2.GetComponent<GizmoRender>() != null))
			{
				transform2 = transform2.parent;
			}
			bool flag2 = true;
			if (flag2 && transform2 != null)
			{
				WorldTransformAxis[] componentsInChildren = worldTransformAxisParent.GetComponentsInChildren<WorldTransformAxis>(true);
				foreach (WorldTransformAxis worldTransformAxis in componentsInChildren)
				{
					if (worldTransformAxis.TargetObject != null && worldTransformAxis.TargetObject == transform2.gameObject)
					{
						transform = transform2;
						break;
					}
				}
			}
		}
		return transform;
	}

	protected virtual Transform GetTargetTransformComm(GameObject obj)
	{
		if (obj == null || obj.transform == null)
		{
			return null;
		}
		if (obj.layer != 17)
		{
			return null;
		}
		return obj.transform;
	}

	protected virtual bool IsEnableGrab(GameObject obj)
	{
		VREventTriggerGrab component = obj.GetComponent<VREventTriggerGrab>();
		return !(component != null) || !component.m_bNoMove;
	}

	public new virtual bool enabled
	{
		get
		{
			return base.enabled;
		}
		set
		{
			if (!value)
			{
				this.ClearColliderList();
			}
			base.enabled = value;
			this.collder.enabled = value;
		}
	}

	public virtual void OnDisable()
	{
		this.ClearColliderList();
	}

	public virtual void ClearColliderList()
	{
		this.ResetGrip();
		this.m_listCollider.Clear();
	}

	public virtual SphereCollider collder
	{
		get
		{
			return this.collder_;
		}
	}

	public virtual Rigidbody rigidbody
	{
		get
		{
			return this.rigidbody_;
		}
	}

	public virtual OvrGripCollider pair_object
	{
		get
		{
			return this.pair_obj_;
		}
		set
		{
			this.pair_obj_ = value;
		}
	}

	public virtual bool grip
	{
		get
		{
			return this.lock_object_trans_ != null;
		}
	}

	public virtual List<GameObject> collider_list
	{
		get
		{
			return this.m_listCollider;
		}
	}

	public virtual OvrGripCollider.OnGrabObject on_grab_obj_callback
	{
		set
		{
			this.on_grab_object_ = value;
		}
	}

	public virtual OvrGripCollider.OnReleaseObject on_release_obj_callback
	{
		set
		{
			this.on_release_object_ = value;
		}
	}

	public byte m_byHaptic = 200;

	public float m_fTime = 0.05f;

	protected bool m_bEnableGrab = true;

	protected bool mode_scale_control_;

	protected float scale_distance_;

	protected float init_scale_;

	protected Vector2 scale_min_max_;

	protected Transform trans_dummy_;

	protected Transform lock_object_trans_;

	protected OvrGripCollider pair_obj_;

	protected SphereCollider collder_;

	protected Rigidbody rigidbody_;

	protected VelocityTracker velocity_tracker_;

	protected List<GameObject> m_listCollider = new List<GameObject>();

	protected OvrGripCollider.OnGrabObject on_grab_object_;

	protected OvrGripCollider.OnReleaseObject on_release_object_;

	protected AVRControllerButtons ctrl_btn_;

	protected bool press_down_trigger_;

	public delegate void OnGrabObject(GameObject target_obj);

	public delegate void OnReleaseObject(GameObject gripObject, Vector3 linearVelocity, Vector3 angularVelocity);
}
