using System;
using System.Collections;
using UnityEngine;

public class OvrTablet : MonoBehaviour
{
	public float Size
	{
		get
		{
			return base.transform.localScale.x;
		}
		set
		{
			if (GameMain.Instance.VRDeviceTypeID != GameMain.VRDeviceType.NON)
			{
				float num = Mathf.Clamp(value, 0.5f, 3f);
				base.transform.localScale = new Vector3(num, num, num);
			}
		}
	}

	public bool IsRotDown
	{
		get
		{
			return this.m_bRotDown;
		}
	}

	public bool IsSideBack
	{
		get
		{
			return this.m_bSideBack;
		}
	}

	public bool UIStickyHead
	{
		get
		{
			return this.m_bUIStickyHead;
		}
		set
		{
			this.m_bUIStickyHead = value;
		}
	}

	private void Awake()
	{
		this.m_trScreen = base.transform.Find("Screen");
		NDebug.Assert(this.m_trScreen != null, "OvrTablet Screenが見つかりません。");
		this.m_vScaleBackup = base.transform.localScale;
		this.m_vScaleNow = this.m_vScaleBackup;
		this.m_trButton = base.transform.Find("Button");
		NDebug.Assert(this.m_trButton != null, "OvrTablet Buttonが見つかりません。");
		this.m_trTabletFrame = base.transform.Find("Tablet");
		NDebug.Assert(this.m_trTabletFrame != null, "OvrTablet Tabletが見つかりません。");
	}

	public void Init(GameObject f_goCenterEye)
	{
		this.m_trOvrUiTabletResetParent = UTY.GetChildObject(f_goCenterEye, "TabletUIResetParent", false).transform;
		this.m_trOvrUiTabletResetPos = UTY.GetChildObject(this.m_trOvrUiTabletResetParent.gameObject, "TabletUIResetPos", false).transform;
	}

	private void OnEnable()
	{
		this.SetVisible(true);
	}

	private void OnDisable()
	{
		this.SetVisible(false);
	}

	public void SetVisible(bool f_bVisible)
	{
		Renderer[] componentsInChildren = base.GetComponentsInChildren<Renderer>(true);
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			componentsInChildren[i].enabled = f_bVisible;
		}
		Collider[] componentsInChildren2 = base.GetComponentsInChildren<Collider>(true);
		for (int j = 0; j < componentsInChildren2.Length; j++)
		{
			componentsInChildren2[j].enabled = f_bVisible;
		}
		this.m_bVisible = f_bVisible;
		this.AvoidFloorAndChara();
	}

	public bool IsVisible
	{
		get
		{
			return this.m_bVisible;
		}
	}

	public bool IsHitPointerArea(Vector3 f_vWorldPoint)
	{
		if (!this.IsVisible)
		{
			return false;
		}
		Vector3 vector = base.transform.InverseTransformPoint(f_vWorldPoint);
		float num = this.m_vHitArea.x * 0.5f;
		float num2 = this.m_vHitArea.z * 0.5f;
		float num3 = this.m_vHitArea.y * 0.5f;
		if (-num <= vector.x && vector.x <= num && -num2 <= vector.z && vector.z <= num2 && -num3 <= vector.y && vector.y <= num3)
		{
			Debug.DrawLine(f_vWorldPoint, base.transform.TransformPoint(vector.x, 0f, vector.z), Color.green);
			return true;
		}
		Debug.DrawLine(f_vWorldPoint, base.transform.TransformPoint(vector.x, 0f, vector.z), Color.gray);
		return false;
	}

	public void UIPosReset(float f_fOffsetRotY = 0f)
	{
		if (!base.enabled || !base.gameObject.activeInHierarchy)
		{
			return;
		}
		if (GameMain.Instance.VRDeviceTypeID != GameMain.VRDeviceType.NON)
		{
			base.StartCoroutine(this.CoUIPosReset(f_fOffsetRotY));
		}
	}

	private IEnumerator CoUIPosReset(float f_fOffsetRotY = 0f)
	{
		bool bSticky = true;
		float timeBack = Time.realtimeSinceStartup;
		if (bSticky)
		{
			Transform trHead = GameMain.Instance.OvrMgr.OvrCamera.GetRealHeadTransform();
			if (!(trHead == null))
			{
				Vector3 vHeadEuler = trHead.rotation.eulerAngles;
				this.m_trOvrUiTabletResetParent.rotation = Quaternion.Euler(vHeadEuler.x, vHeadEuler.y + f_fOffsetRotY, 0f);
				base.transform.position = this.m_trOvrUiTabletResetPos.position;
				base.transform.rotation = this.m_trOvrUiTabletResetPos.rotation;
				this.AvoidFloorAndChara();
				yield return null;
			}
		}
		this.m_vWorldPos = base.transform.position;
		yield break;
	}

	public void AvoidFloorAndChara()
	{
		if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.NON)
		{
			return;
		}
		bool flag = false;
		float num = 0.15f;
		float num2 = 0.25f * base.transform.localScale.x;
		Ray ray = new Ray(base.transform.position, Vector3.down);
		float num3 = 0f;
		RaycastHit raycastHit;
		if (Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, 512))
		{
			num3 = Vector3.Distance(raycastHit.point, base.transform.position);
		}
		else
		{
			ray.origin = new Vector3(base.transform.position.x, 100f, base.transform.position.z);
			ray.direction = Vector3.down;
			if (Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, 512))
			{
				num3 = Vector3.Distance(raycastHit.point, base.transform.position);
			}
		}
		if (raycastHit.collider != null && num3 < num2 + 0.1f)
		{
			base.transform.position = new Vector3(base.transform.position.x, raycastHit.point.y + num2 + 0.1f, base.transform.position.z);
			flag = true;
		}
		if (GameMain.Instance.OvrMgr != null && GameMain.Instance.OvrMgr.OvrCamera != null)
		{
			Transform realHeadTransform = GameMain.Instance.OvrMgr.OvrCamera.GetRealHeadTransform();
			int maidCount = GameMain.Instance.CharacterMgr.GetMaidCount();
			for (int i = 0; i < maidCount; i++)
			{
				Maid maid = GameMain.Instance.CharacterMgr.GetMaid(i);
				if (maid != null && maid.Visible && maid.body0 != null && maid.body0.isLoadedBody)
				{
					foreach (Transform transform in new Transform[]
					{
						maid.body0.trsHead,
						maid.body0.Spine,
						maid.body0.Spine0a,
						maid.body0.Spine1a,
						maid.body0.UpperArmL,
						maid.body0.UpperArmR
					})
					{
						if (!(transform == null))
						{
							if (Vector3.Distance(base.transform.position, transform.position) < num + num2)
							{
								Vector3 normalized = (realHeadTransform.position - transform.position).normalized;
								Vector3 normalized2 = Vector3.Reflect(-normalized, Vector3.up).normalized;
								Vector3 normalized3 = Vector3.Cross(normalized, normalized2).normalized;
								Vector3 a = base.transform.position - normalized3 * (num + num2 + 0.1f);
								float angle = Vector3.Angle((transform.position - realHeadTransform.position).normalized, (a - realHeadTransform.position).normalized);
								base.transform.RotateAround(realHeadTransform.position, Vector3.up, angle);
								flag = true;
							}
						}
					}
				}
			}
			if (flag)
			{
				base.transform.LookAt(realHeadTransform.position);
				base.transform.Rotate(-90f, 180f, 0f, Space.Self);
			}
		}
		this.m_vWorldPos = base.transform.position;
	}

	private void Update()
	{
		if (!this.IsVisible)
		{
			return;
		}
		if (this.m_bUIStickyHead)
		{
			Transform realHeadTransform = GameMain.Instance.OvrMgr.OvrCamera.GetRealHeadTransform();
			float num = (this.m_vWorldPos - realHeadTransform.position).magnitude;
			Vector3 normalized = (this.m_vWorldPos - realHeadTransform.position).normalized;
			bool flag = false;
			if (num < 0.1f || 0.5f < num)
			{
				if (0.5f < num)
				{
					flag = true;
				}
				num = Mathf.Clamp(num, 0.1f, 0.5f);
				this.m_vWorldPos = realHeadTransform.position + normalized * num;
			}
			Vector3 normalized2 = GameMain.Instance.OvrMgr.OvrCamera.EyeRay.GetRay().direction.normalized;
			float num2 = Vector3.Angle(normalized2, normalized);
			if (num2 > 60f)
			{
				Vector3 normalized3 = Vector3.RotateTowards(normalized, normalized2, 1f - 60f / num2, 0f).normalized;
				this.m_vWorldPos = realHeadTransform.position + normalized3 * num;
				flag = true;
			}
			base.transform.position = this.m_vWorldPos;
			if (flag)
			{
				base.transform.LookAt(GameMain.Instance.OvrMgr.OvrCamera.GetRealHeadTransform().position);
				base.transform.Rotate(new Vector3(-90f, 0f, 0f), Space.Self);
			}
		}
		Vector3 forward = base.transform.forward;
		if (GameMain.Instance.OvrMgr != null && GameMain.Instance.OvrMgr.OvrCamera != null)
		{
			Transform realHeadTransform2 = GameMain.Instance.OvrMgr.OvrCamera.GetRealHeadTransform();
			if (Vector3.Dot((realHeadTransform2.position - base.transform.position).normalized, base.transform.up) < 0f)
			{
				this.m_bSideBack = true;
			}
			else
			{
				this.m_bSideBack = false;
			}
		}
		if (GameMain.Instance.VRDeviceTypeID != GameMain.VRDeviceType.NON)
		{
			if (Vector3.Dot(forward, Vector3.down) < 0f)
			{
				this.m_trScreen.localRotation = Quaternion.Euler(90f, 0f, 0f);
				this.m_bRotDown = false;
			}
			else
			{
				this.m_trScreen.localRotation = Quaternion.Euler(-90f, 0f, 0f);
				this.m_bRotDown = true;
			}
		}
		else
		{
			this.m_trScreen.localRotation = Quaternion.Euler(90f, 0f, 0f);
			this.m_bRotDown = false;
		}
		float vrcameraFov = GameMain.Instance.CMSystem.VRCameraFov;
		if (this.m_fBackFOV != vrcameraFov)
		{
			float num3 = (vrcameraFov - 30f) / 30f;
			float num4 = this.m_fTabletScaleMin + (this.m_fTabletScaleMax - this.m_fTabletScaleMin) * num3;
			this.m_vScaleNow = new Vector3(num4, num4, num4);
			this.m_fBackFOV = vrcameraFov;
		}
		if (GameMain.Instance.VRDeviceTypeID == GameMain.VRDeviceType.NON)
		{
			float num5 = (float)Screen.width / (float)Screen.height;
			if (num5 < 1.7777f)
			{
				float num6 = this.m_vScaleNow.x * (num5 / 1.7777f);
				base.transform.localScale = new Vector3(num6, num6, num6);
			}
			else
			{
				base.transform.localScale = this.m_vScaleNow;
			}
		}
		bool isVisibleMessageViewer = GameMain.Instance.MsgWnd.IsVisibleMessageViewer;
		if (this.m_bBackVisibleMessageViewer != isVisibleMessageViewer)
		{
			if (GameMain.Instance.CMSystem.SConfig.OvrTabletFrameHideWhenMsgWndVisible)
			{
				this.m_trTabletFrame.gameObject.SetActive(!isVisibleMessageViewer);
				this.m_trButton.gameObject.SetActive(!isVisibleMessageViewer);
			}
			else
			{
				this.m_trTabletFrame.gameObject.SetActive(true);
				this.m_trButton.gameObject.SetActive(true);
			}
			this.m_bBackVisibleMessageViewer = isVisibleMessageViewer;
		}
	}

	public Vector3 m_vHitArea = new Vector3(1.3f, 1f, 1f);

	public Transform m_trOvrUiTabletResetParent;

	public Transform m_trOvrUiTabletResetPos;

	private Transform m_trScreen;

	private bool m_bRotDown;

	private bool m_bSideBack;

	private bool m_bVisible = true;

	private bool m_bUIStickyHead;

	private Vector3 m_vWorldPos;

	private Vector3 m_vScaleBackup;

	private Vector3 m_vScaleNow;

	private float m_fDistance = 1f;

	private float m_fBackFOV;

	private float m_fTabletScaleMin = 0.1543127f;

	private float m_fTabletScaleMax = 0.3287759f;

	private bool m_bBackVisibleMessageViewer;

	private Transform m_trButton;

	private Transform m_trTabletFrame;
}
