using System;
using System.Collections;
using System.IO;
using UnityEngine;

public class OvrHandCamera : MonoBehaviour
{
	private void Awake()
	{
		Transform transform = base.transform.Find("Camera");
		NDebug.Assert(transform != null, "HandCameraの下にCameraがありませんでした。");
		this.m_Camera = transform.GetComponent<Camera>();
		Transform transform2 = base.transform.Find("mopnitor");
		NDebug.Assert(transform2 != null, "HandCameraの下にmopnitorがありませんでした。");
		MeshRenderer component = transform2.GetComponent<MeshRenderer>();
		this.m_matMonitor = component.material;
		this.m_rtPreview = new RenderTexture(180, 320, 24, RenderTextureFormat.ARGB32);
		this.m_Camera.targetTexture = this.m_rtPreview;
		this.m_matMonitor.mainTexture = this.m_rtPreview;
		this.m_vBackPos = base.transform.localPosition;
		this.m_vBackCamPos = transform.localPosition;
	}

	private void OnEnable()
	{
		if (OvrIK.IsModeVRIK)
		{
			if (this.m_ctrl != null)
			{
				if (this.m_ctrl.m_bHandL)
				{
					base.transform.localPosition = new Vector3(0.085f, -0.021f, -0.082f);
				}
				else
				{
					base.transform.localPosition = new Vector3(-0.085f, -0.021f, -0.082f);
				}
				this.m_Camera.transform.localPosition = new Vector3(this.m_Camera.transform.localPosition.x, 0.0189f, this.m_Camera.transform.localPosition.z);
			}
		}
		else
		{
			base.transform.localPosition = this.m_vBackPos;
			this.m_Camera.transform.localPosition = this.m_vBackCamPos;
		}
	}

	public void Snap()
	{
		if (GameMain.Instance.OvrMgr.OvrCamera.IsFadeStateNon())
		{
			GameMain.Instance.SoundMgr.PlaySe("SE022.ogg", false);
			base.StartCoroutine(this.SaveScreenShotNormal());
		}
	}

	private IEnumerator SaveScreenShotNormal()
	{
		int nSuperSize = this.ss_super_size_[(int)GameMain.Instance.CMSystem.ScreenShotSuperSize];
		RenderTexture rtSnap = new RenderTexture(GameMain.Instance.CMSystem.OvrHandCameraWidth * nSuperSize, GameMain.Instance.CMSystem.OvrHandCameraHeight * nSuperSize, 24, RenderTextureFormat.ARGB32);
		RenderTexture rtSnapThum = null;
		if (SceneVRCommunication.Instance != null)
		{
			rtSnapThum = new RenderTexture(576, 1024, 24, RenderTextureFormat.ARGB32);
		}
		this.m_Camera.targetTexture = rtSnap;
		yield return new WaitForEndOfFrame();
		if (SceneVRCommunication.Instance != null)
		{
			this.m_Camera.targetTexture = rtSnapThum;
			yield return new WaitForEndOfFrame();
		}
		UTY.SaveImage(rtSnap, this.GetTimeFileName(false), TextureFormat.RGB24);
		if (SceneVRCommunication.Instance != null)
		{
			this.SaveImageThum(rtSnapThum, this.GetTimeFileName(true), TextureFormat.RGB24);
		}
		yield return new WaitForEndOfFrame();
		this.m_Camera.targetTexture = this.m_rtPreview;
		UnityEngine.Object.Destroy(rtSnap);
		if (rtSnapThum != null)
		{
			UnityEngine.Object.Destroy(rtSnapThum);
		}
		yield break;
	}

	public void SaveImageThum(RenderTexture f_rtSrc, string f_strFileName, TextureFormat f_TexFormat = TextureFormat.ARGB32)
	{
		Texture2D texture2D = new Texture2D(f_rtSrc.width, f_rtSrc.height, f_TexFormat, false);
		RenderTexture active = RenderTexture.active;
		RenderTexture.active = f_rtSrc;
		texture2D.ReadPixels(new Rect(0f, 0f, (float)f_rtSrc.width, (float)f_rtSrc.height), 0, 0);
		RenderTexture.active = active;
		byte[] bytes = texture2D.EncodeToJPG();
		File.WriteAllBytes(f_strFileName, bytes);
	}

	private string GetTimeFileName(bool f_bThum = false)
	{
		string fullPath = Path.GetFullPath(".\\");
		string text = fullPath + "ScreenShot";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		if (!f_bThum)
		{
			return string.Concat(new string[]
			{
				text,
				"\\img",
				DateTime.Now.ToString("yyyyMMddHHmmss"),
				this.m_strAddSideChar[(int)this.m_eSide],
				".png"
			});
		}
		text += "\\VRPhotoThumb";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		return string.Concat(new string[]
		{
			text,
			"\\img",
			DateTime.Now.ToString("yyyyMMddHHmmss"),
			this.m_strAddSideChar[(int)this.m_eSide],
			".jpg"
		});
	}

	private void Update()
	{
		Vector3 vector = base.transform.TransformPoint(-0.06f, 0f, 0.08f);
		Vector3 vector2 = base.transform.TransformPoint(0.06f, 0f, 0.08f);
		Vector3 vector3 = base.transform.TransformPoint(0f, 0f, 0.15f);
		float y = base.transform.position.y;
		OvrHandCamera.SIDE eSide = OvrHandCamera.SIDE.PORTRAIT_DOWN;
		if (vector.y < y)
		{
			y = vector.y;
			eSide = OvrHandCamera.SIDE.LANDSCAPE_L;
		}
		if (vector2.y < y)
		{
			y = vector2.y;
			eSide = OvrHandCamera.SIDE.LANDSCAPE_R;
		}
		if (vector3.y < y)
		{
			y = vector3.y;
			eSide = OvrHandCamera.SIDE.PORTRAIT_TOP;
		}
		this.m_eSide = eSide;
	}

	private Camera m_Camera;

	private Material m_matMonitor;

	private RenderTexture m_rtPreview;

	public AVRControllerBehavior m_ctrl;

	private int[] ss_super_size_ = new int[]
	{
		1,
		2,
		4
	};

	public OvrHandCamera.SIDE m_eSide = OvrHandCamera.SIDE.PORTRAIT_DOWN;

	private string[] m_strAddSideChar = new string[]
	{
		"l",
		"r",
		"d",
		"t"
	};

	private Vector3 m_vBackPos;

	private Vector3 m_vBackCamPos;

	public enum SIDE
	{
		LANDSCAPE_L,
		LANDSCAPE_R,
		PORTRAIT_DOWN,
		PORTRAIT_TOP,
		MAX
	}
}
