using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class ShootCutInTex : MonoBehaviour
{
	private void Init(string cut_name, string label)
	{
		ShootCutInTex.m_IsShoot = true;
		this.m_CutName = (ShootCutInTex.CutInName)Enum.Parse(typeof(ShootCutInTex.CutInName), cut_name);
		this.m_JumpLabel = label;
		this.m_MyCamera = base.GetComponent<Camera>();
		this.m_MyCamera.enabled = false;
		float distance = GameMain.Instance.MainCamera.GetDistance();
		Vector2 aroundAngle = GameMain.Instance.MainCamera.GetAroundAngle();
		Vector3 targetPos = GameMain.Instance.MainCamera.GetTargetPos();
		base.transform.rotation = Quaternion.identity;
		base.transform.Rotate(Vector3.up * aroundAngle.x, Space.World);
		base.transform.Rotate(Vector3.right * aroundAngle.y, Space.Self);
		base.transform.position = base.transform.rotation * Vector3.forward * -distance + targetPos;
		this.CameraValCpy(GameMain.Instance.MainCamera.camera, this.m_MyCamera);
		this.m_MyCamera.aspect = 1.77777779f;
		if (RhythmAction_Mgr.IsVSDance && RhythmAction_Mgr.NowState == RhythmAction_Mgr.DanceState.Dance_First)
		{
			ShootCutInTex.TextureClear(DanceBattle_Mgr.CharaType.Enemy);
			ShootCutInTex.SetEnemyCutInTex(false);
		}
		base.StartCoroutine(this.MakeCutInTex());
	}

	private bool CheckPlayerCutInTex()
	{
		if (this.m_CutName == ShootCutInTex.CutInName.対峙演出 || this.m_CutName == ShootCutInTex.CutInName.逆転)
		{
			return RhythmAction_Mgr.NowDance == RhythmAction_Mgr.DanceType.Challenge;
		}
		if (ShootCutInTex.m_PlayerCutInTexList.ContainsKey(this.m_CutName))
		{
			return true;
		}
		string text = Path.GetFullPath(".\\") + "AppealImage/";
		if (!Directory.Exists(text))
		{
			Directory.CreateDirectory(text);
		}
		string text2 = DanceSelect.SelectedMaid[0].status.fullNameJpStyle;
		text2 = text2.Replace(" ", string.Empty) + "_アピール.png";
		bool result = false;
		try
		{
			using (FileStream fileStream = new FileStream(text + text2, FileMode.Open, FileAccess.Read))
			{
				BinaryReader binaryReader = new BinaryReader(fileStream);
				byte[] data = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
				Texture2D texture2D = new Texture2D(1920, 1080);
				texture2D.LoadImage(data);
				texture2D.Apply(true, true);
				Sprite value = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), Vector2.one * 0.5f);
				ShootCutInTex.m_PlayerCutInTexList.Add(ShootCutInTex.CutInName.性格固有1, value);
				ShootCutInTex.m_PlayerCutInTexList.Add(ShootCutInTex.CutInName.性格固有2, value);
				ShootCutInTex.m_PlayerCutInTexList.Add(ShootCutInTex.CutInName.ジョブ固有, value);
				binaryReader.Close();
				result = true;
			}
		}
		catch (Exception ex)
		{
			if (!File.Exists(text + text2))
			{
				Debug.Log("ShootCutInTex.cs:プレイヤー側の画像が存在しなかったので撮影した画像を使います。");
			}
			else
			{
				Debug.LogError("ShootCutInTex.cs:画像読み込みエラー。エラー箇所:" + ex.StackTrace);
			}
		}
		return result;
	}

	private void CameraValCpy(Camera orijin, Camera cpy)
	{
		cpy.cullingMask = orijin.cullingMask;
		cpy.clearFlags = orijin.clearFlags;
		cpy.backgroundColor = orijin.backgroundColor;
		cpy.farClipPlane = orijin.farClipPlane;
		cpy.nearClipPlane = orijin.nearClipPlane;
		cpy.orthographic = orijin.orthographic;
		cpy.fieldOfView = orijin.fieldOfView;
		cpy.orthographicSize = orijin.orthographicSize;
	}

	private IEnumerator MakeCutInTex()
	{
		yield return null;
		if (this.CheckPlayerCutInTex())
		{
			this.ShootEnd();
			yield break;
		}
		while (GameMain.Instance.CharacterMgr.IsBusy())
		{
			yield return null;
		}
		Time.timeScale = 20f;
		List<Maid> active_maid = new List<Maid>();
		List<Transform> eye_target = new List<Transform>();
		for (int i = 0; i < DanceSelect.SelectedMaid.Count; i++)
		{
			if (DanceSelect.SelectedMaid[i] && DanceSelect.SelectedMaid[i].Visible)
			{
				Maid maid2 = DanceSelect.SelectedMaid[i];
				active_maid.Add(maid2);
				eye_target.Add(maid2.body0.trsLookTarget);
				maid2.EyeToTargetObject(base.transform, 0f);
			}
		}
		for (;;)
		{
			if (!active_maid.Any((Maid maid) => maid.GetAnimation().isPlaying || maid.boFaceAnime))
			{
				break;
			}
			yield return new WaitForEndOfFrame();
		}
		Time.timeScale = 1f;
		for (int j = 0; j < active_maid.Count; j++)
		{
			active_maid[j].EyeToTargetObject(eye_target[j], 0f);
		}
		if (GameMain.Instance.VRMode)
		{
			GameMain.Instance.OvrMgr.OvrCamera.ShowUI(false);
			GameMain.Instance.OvrMgr.GetVRControllerTransform(true).gameObject.SetActive(false);
			GameMain.Instance.OvrMgr.GetVRControllerTransform(false).gameObject.SetActive(false);
		}
		RenderTexture rend_tex = new RenderTexture(1920, 1080, 24);
		rend_tex.isPowerOfTwo = false;
		rend_tex.antiAliasing = ((QualitySettings.antiAliasing == 0) ? 2 : QualitySettings.antiAliasing);
		RenderTexture orijin_rendtex = this.m_MyCamera.targetTexture;
		RenderTexture active_tex = RenderTexture.active;
		RenderTexture.active = rend_tex;
		this.m_MyCamera.targetTexture = rend_tex;
		this.m_MyCamera.fieldOfView = 35f;
		this.m_MyCamera.enabled = true;
		this.m_MyCamera.Render();
		this.m_MyCamera.enabled = false;
		Texture2D tex = new Texture2D(1920, 1080, TextureFormat.RGB24, false);
		tex.ReadPixels(new Rect(0f, 0f, 1920f, 1080f), 0, 0);
		tex.Apply();
		ShootCutInTex.m_PlayerCutInTexList.Add(this.m_CutName, Sprite.Create(tex, new Rect(0f, 0f, 1920f, 1080f), Vector2.one * 0.5f));
		this.m_MyCamera.targetTexture = orijin_rendtex;
		RenderTexture.active = active_tex;
		if (GameMain.Instance.VRMode)
		{
			GameMain.Instance.OvrMgr.OvrCamera.ShowUI(true);
			GameMain.Instance.OvrMgr.GetVRControllerTransform(true).gameObject.SetActive(true);
			GameMain.Instance.OvrMgr.GetVRControllerTransform(false).gameObject.SetActive(true);
		}
		this.ShootEnd();
		yield break;
	}

	private void ShootEnd()
	{
		CameraMain.CameraType cameraType = GameMain.Instance.MainCamera.GetCameraType();
		bool control = GameMain.Instance.MainCamera.GetControl();
		GameMain.Instance.MainCamera.Reset(cameraType, control);
		if (!string.IsNullOrEmpty(this.m_JumpLabel))
		{
			GameMain.Instance.ScriptMgr.adv_kag.JumpLabel(this.m_JumpLabel);
			GameMain.Instance.ScriptMgr.adv_kag.Exec();
		}
		else
		{
			Debug.LogError("ShootCutInTex.cs:撮影終了後に飛ぶラベルが設定されてません");
		}
		UnityEngine.Object.Destroy(base.gameObject);
	}

	public static void SetEnemyCutInTex(bool is_second)
	{
		ShootCutInTex.m_EnemyCutInTexList.Clear();
		ShootCutInTex.m_EnemyCutInTexList.Add(ShootCutInTex.CutInName.対峙演出, VsDanceDataMgr.Instance.GetCurrentEnemy(is_second).ConfrontCutin);
		ShootCutInTex.m_EnemyCutInTexList.Add(ShootCutInTex.CutInName.性格固有1, VsDanceDataMgr.Instance.GetCurrentEnemy(is_second).PersonalCutin1);
		ShootCutInTex.m_EnemyCutInTexList.Add(ShootCutInTex.CutInName.性格固有2, VsDanceDataMgr.Instance.GetCurrentEnemy(is_second).PersonalCutin2);
		ShootCutInTex.m_EnemyCutInTexList.Add(ShootCutInTex.CutInName.ジョブ固有, VsDanceDataMgr.Instance.GetCurrentEnemy(is_second).JobCutin);
		ShootCutInTex.m_EnemyCutInTexList.Add(ShootCutInTex.CutInName.逆転, VsDanceDataMgr.Instance.GetCurrentEnemy(is_second).ReversalCutin);
	}

	public static void SetPlayerCutInTex()
	{
		ShootCutInTex.m_IsShoot = false;
		Sprite value = Resources.Load<Sprite>("SceneDance/Rhythm_Action/Sprite/CutInTex/Personal1");
		ShootCutInTex.m_PlayerCutInTexList.Add(ShootCutInTex.CutInName.性格固有1, value);
		value = Resources.Load<Sprite>("SceneDance/Rhythm_Action/Sprite/CutInTex/Personal2");
		ShootCutInTex.m_PlayerCutInTexList.Add(ShootCutInTex.CutInName.性格固有2, value);
		value = Resources.Load<Sprite>("SceneDance/Rhythm_Action/Sprite/CutInTex/Job");
		ShootCutInTex.m_PlayerCutInTexList.Add(ShootCutInTex.CutInName.ジョブ固有, value);
		value = Resources.Load<Sprite>("SceneDance/Rhythm_Action/Sprite/CutInTex/Reversal");
		ShootCutInTex.m_PlayerCutInTexList.Add(ShootCutInTex.CutInName.逆転, value);
		value = Resources.Load<Sprite>("SceneDance/Rhythm_Action/Sprite/CutInTex/Confront");
		ShootCutInTex.m_PlayerCutInTexList.Add(ShootCutInTex.CutInName.対峙演出, value);
	}

	public static bool IsContainsTex(DanceBattle_Mgr.CharaType chara_type, ShootCutInTex.CutInName cut_name)
	{
		if (chara_type == DanceBattle_Mgr.CharaType.Player)
		{
			return ShootCutInTex.m_PlayerCutInTexList.ContainsKey(cut_name) && ShootCutInTex.m_PlayerCutInTexList[cut_name];
		}
		return ShootCutInTex.m_EnemyCutInTexList.ContainsKey(cut_name) && ShootCutInTex.m_EnemyCutInTexList[cut_name];
	}

	public static Sprite GetCutInTex(DanceBattle_Mgr.CharaType chara_type, ShootCutInTex.CutInName cut_name)
	{
		if (chara_type == DanceBattle_Mgr.CharaType.Player)
		{
			if (ShootCutInTex.m_PlayerCutInTexList.ContainsKey(cut_name))
			{
				return ShootCutInTex.m_PlayerCutInTexList[cut_name];
			}
			Debug.LogError("プレイヤーの" + cut_name + "カットイン用の画像が存在しません");
			return null;
		}
		else
		{
			if (chara_type != DanceBattle_Mgr.CharaType.Enemy)
			{
				return null;
			}
			if (ShootCutInTex.m_EnemyCutInTexList.ContainsKey(cut_name))
			{
				return ShootCutInTex.m_EnemyCutInTexList[cut_name];
			}
			Debug.LogError("敵の" + cut_name + "カットイン用の画像が存在しません");
			return null;
		}
	}

	public static void TextureClear(DanceBattle_Mgr.CharaType chara_type)
	{
		if (chara_type == DanceBattle_Mgr.CharaType.Player)
		{
			if (ShootCutInTex.m_IsShoot)
			{
				foreach (Sprite sprite in ShootCutInTex.m_PlayerCutInTexList.Values)
				{
					if (sprite)
					{
						if (sprite.texture)
						{
							UnityEngine.Object.DestroyImmediate(sprite.texture, true);
						}
						UnityEngine.Object.DestroyImmediate(sprite, true);
					}
				}
			}
			ShootCutInTex.m_PlayerCutInTexList.Clear();
		}
		else
		{
			ShootCutInTex.m_EnemyCutInTexList.Clear();
		}
	}

	public static void CutInTexShoot(string cut_name, string label)
	{
		GameObject gameObject = new GameObject("CutIn_Shoot", new Type[]
		{
			typeof(ShootCutInTex),
			typeof(Camera)
		});
		gameObject.GetComponent<ShootCutInTex>().Init(cut_name, label);
	}

	private const int m_TimeSpeed = 20;

	private const int m_CutInTexWidth = 1920;

	private const int m_CutInTexHeight = 1080;

	private const string m_PlayerTexPath = "AppealImage/";

	private const float m_CameraFov = 35f;

	private const int m_AntiAliasDefo = 2;

	private const string m_PersonalImage1 = "Sprite/CutInTex/Personal1";

	private const string m_PersonalImage2 = "Sprite/CutInTex/Personal2";

	private const string m_JobImage = "Sprite/CutInTex/Job";

	private const string m_ReversalImage = "Sprite/CutInTex/Reversal";

	private const string m_ConfrontImage = "Sprite/CutInTex/Confront";

	private ShootCutInTex.CutInName m_CutName;

	private string m_JumpLabel = string.Empty;

	private Camera m_MyCamera;

	private static Dictionary<ShootCutInTex.CutInName, Sprite> m_PlayerCutInTexList = new Dictionary<ShootCutInTex.CutInName, Sprite>();

	private static Dictionary<ShootCutInTex.CutInName, Sprite> m_EnemyCutInTexList = new Dictionary<ShootCutInTex.CutInName, Sprite>();

	private static bool m_IsShoot = false;

	public enum CutInName
	{
		対峙演出,
		性格固有1,
		性格固有2,
		ジョブ固有,
		逆転
	}
}
