using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using wf;

public class OvrSelfShotCamera : MonoBehaviour
{
	public static OvrSelfShotCamera Create()
	{
		GameObject gameObject = Utility.CreatePrefab(null, "OVR/OvrSelfShotCamera", false);
		gameObject.name = "OvrSelfShotCamera";
		OvrSelfShotCamera component = gameObject.GetComponent<OvrSelfShotCamera>();
		component.Init();
		return component;
	}

	public RenderTexture CamTex
	{
		get
		{
			return this.m_rtMoniPlaneTex;
		}
	}

	public Transform EyeTarget
	{
		get
		{
			return this.m_trEyeTarget;
		}
	}

	private void Init()
	{
		this.m_camera = base.gameObject.GetComponentInChildren<Camera>();
		this.m_rtMoniPlaneTex = this.m_camera.targetTexture;
		this.m_trEyeTarget = this.m_camera.transform;
		this.m_trCamMoniPlane = base.gameObject.transform.Find("SelfShotCam/Monitor/Plane");
		Transform realHeadTransform = GameMain.Instance.OvrMgr.OvrCamera.GetRealHeadTransform();
		Vector3 vector = new Vector3(realHeadTransform.forward.x, 0f, realHeadTransform.forward.z);
		Vector3 normalized = vector.normalized;
		base.gameObject.transform.position = realHeadTransform.position + normalized * 1.5f;
		base.gameObject.transform.LookAt(realHeadTransform.position, Vector3.up);
		base.gameObject.transform.Rotate(-20f, 180f, 0f);
		this.m_OvrCamera = GameMain.Instance.OvrMgr.OvrCamera.GetComponent<Camera>();
		this.m_goCountCanvas = base.gameObject.transform.Find("SelfShotCam/Canvas").gameObject;
		this.m_txCountText = this.m_goCountCanvas.transform.Find("Text").GetComponent<Text>();
		this.m_goCountCanvas.SetActive(false);
	}

	private void Start()
	{
		if (this.m_camera == null)
		{
			this.Init();
		}
	}

	private void OnDisable()
	{
		this.m_coSnap = null;
		if (this.m_camera != null && this.m_rtPreview != null)
		{
			this.m_camera.targetTexture = this.m_rtPreview;
		}
		this.m_coCountdown = null;
		if (this.m_goCountCanvas != null)
		{
			this.m_goCountCanvas.SetActive(false);
		}
	}

	public void StartCountSnap()
	{
		if (GameMain.Instance.OvrMgr.OvrCamera.IsFadeStateNon() && this.m_coSnap == null && this.m_goCountCanvas != null)
		{
			this.m_coCountdown = base.StartCoroutine(this.CoCountdown());
		}
	}

	private IEnumerator CoCountdown()
	{
		this.m_goCountCanvas.SetActive(true);
		int nC = 3;
		while (0 < nC)
		{
			this.m_txCountText.text = nC.ToString();
			yield return new WaitForSeconds(1f);
			nC--;
		}
		this.Snap();
		this.m_goCountCanvas.SetActive(false);
		this.m_coCountdown = null;
		yield break;
	}

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

	private IEnumerator SaveScreenShotNormal()
	{
		this.m_rtPreview = this.m_camera.targetTexture;
		int nSuperSize = this.ss_super_size_[(int)GameMain.Instance.CMSystem.ScreenShotSuperSize];
		RenderTexture rtSnap = new RenderTexture(this.m_rtPreview.width * nSuperSize, this.m_rtPreview.height * nSuperSize, 24, RenderTextureFormat.ARGB32);
		this.m_camera.targetTexture = rtSnap;
		yield return new WaitForEndOfFrame();
		UTY.SaveImage(rtSnap, this.GetTimeFileName(false), TextureFormat.RGB24);
		yield return new WaitForEndOfFrame();
		this.m_camera.targetTexture = this.m_rtPreview;
		this.m_rtPreview = null;
		UnityEngine.Object.Destroy(rtSnap);
		this.m_coSnap = null;
		yield break;
	}

	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 text + "\\img" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
		}
		text += "\\VRPhotoThumb";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		return text + "\\img" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
	}

	private void Update()
	{
		if (this.m_trCamMoniPlane != null)
		{
			this.m_trCamMoniPlane.localScale = new Vector3((float)this.m_rtMoniPlaneTex.width / (float)this.m_rtMoniPlaneTex.height * 0.1f, 1f, -0.1f);
		}
		this.m_camera.backgroundColor = this.m_OvrCamera.backgroundColor;
		float selfCameraFOV = ControllerShortcutSettingData.config.selfCameraFOV;
		if (this.m_fBackFOV != selfCameraFOV)
		{
			CMSystem.SerializeConfig sconfig = GameMain.Instance.CMSystem.SConfig;
			this.m_camera.fieldOfView = sconfig.OvrIkSelfCamFovMin + (sconfig.OvrIkSelfCamFovMax - sconfig.OvrIkSelfCamFovMin) * selfCameraFOV;
			this.m_fBackFOV = selfCameraFOV;
		}
	}

	private RenderTexture m_rtMoniPlaneTex;

	private Transform m_trCamMoniPlane;

	private Transform m_trEyeTarget;

	private Camera m_camera;

	private Camera m_OvrCamera;

	private RenderTexture m_rtPreview;

	private Coroutine m_coCountdown;

	private Coroutine m_coSnap;

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

	private GameObject m_goCountCanvas;

	private Text m_txCountText;

	private float m_fBackFOV;
}
