using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using MyRoomCustom;
using UnityEngine;
using UnityEngine.Rendering;

public class BgMgr : MonoBehaviour
{
	public GameObject Parent
	{
		get
		{
			return this.m_goParent;
		}
	}

	public GameObject BgObject
	{
		get
		{
			return this.m_goBg;
		}
	}

	private void Awake()
	{
		this.m_goParent = UTY.GetChildObject(base.gameObject, "BG", false);
		NDebug.Assert(this.m_goParent != null, "__GameMain__/BG が見つかりません");
	}

	public void OnDestroy()
	{
		foreach (KeyValuePair<string, BgMgr.AssetBundleObj> keyValuePair in this.asset_bundle_dic)
		{
			keyValuePair.Value.ab.Unload(true);
		}
		this.asset_bundle_dic.Clear();
	}

	public void ChangeBg(string f_strPrefubName)
	{
		if (!string.IsNullOrEmpty(f_strPrefubName) && 0 <= f_strPrefubName.IndexOf("?"))
		{
			f_strPrefubName = ScriptManager.ReplacePersonal(GameMain.Instance.CharacterMgr.GetMaid(0), f_strPrefubName);
		}
		base.StopCoroutine("CoWaitMovePrefabToChara");
		this.SetPos(Vector3.zero);
		this.SetRot(Vector3.zero);
		this.DelPrefabFromBgAll();
		if (this.m_goBg != null)
		{
			UnityEngine.Object.Destroy(this.m_goBg);
			DeskManager.OnChangeBG(string.Empty, null);
			this.m_goBg = null;
			this.SaveOvrDDPos(true);
		}
		this.m_strBGName = string.Empty;
		if (!string.IsNullOrEmpty(f_strPrefubName))
		{
			UnityEngine.Object @object = this.CreateAssetBundle(f_strPrefubName);
			if (@object == null)
			{
				@object = Resources.Load("BG/" + f_strPrefubName);
				if (@object == null)
				{
					@object = Resources.Load("BG/2_0/" + f_strPrefubName);
				}
			}
			if (@object != null)
			{
				this.m_strBGName = f_strPrefubName;
				this.m_goBg = (UnityEngine.Object.Instantiate(@object) as GameObject);
				MeshRenderer[] componentsInChildren = this.m_goBg.GetComponentsInChildren<MeshRenderer>();
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					if (componentsInChildren[i] != null && componentsInChildren[i].gameObject.name.ToLower().IndexOf("castshadow") < 0)
					{
						componentsInChildren[i].shadowCastingMode = ShadowCastingMode.Off;
					}
				}
				this.m_goBg.transform.SetParent(this.m_goParent.transform, false);
				if (GameMain.Instance.VRMode && Application.isPlaying && this.AddHitToBg(f_strPrefubName + "_hit") == null && this.AddPrefabToBg(f_strPrefubName + "_Hit", f_strPrefubName + "_Hit", null, Vector3.zero, Vector3.zero) == null)
				{
					this.AddPrefabToBg(f_strPrefubName + "_hit", f_strPrefubName + "_hit", null, Vector3.zero, Vector3.zero);
				}
				this.ShowOvrDD(false);
				DeskManager.OnChangeBG(f_strPrefubName.ToLower(), this.m_goBg);
			}
			else
			{
				Debug.LogError("BGプレハブが見つかりませんでした。BG/" + f_strPrefubName);
			}
		}
		Resources.UnloadUnusedAssets();
	}

	public void ChangeBgMyRoom(string guid)
	{
		base.StopCoroutine("CoWaitMovePrefabToChara");
		this.SetPos(Vector3.zero);
		this.SetRot(Vector3.zero);
		this.DelPrefabFromBgAll();
		if (this.m_goBg != null)
		{
			UnityEngine.Object.Destroy(this.m_goBg);
			DeskManager.OnChangeBG(string.Empty, null);
			this.m_goBg = null;
			this.SaveOvrDDPos(true);
		}
		this.m_strBGName = string.Empty;
		if (!string.IsNullOrEmpty(guid) && CreativeRoomManager.IsExistSaveData(guid))
		{
			this.m_strBGName = "マイルーム:" + guid;
			this.m_goBg = CreativeRoomManager.InstantiateRoom(guid);
			MeshRenderer[] componentsInChildren = this.m_goBg.GetComponentsInChildren<MeshRenderer>();
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (componentsInChildren[i] != null && componentsInChildren[i].gameObject.name.ToLower().IndexOf("castshadow") < 0)
				{
					componentsInChildren[i].shadowCastingMode = ShadowCastingMode.Off;
				}
			}
			this.m_goBg.transform.SetParent(this.m_goParent.transform, false);
			DeskManager.OnChangeBG(this.m_strBGName.ToLower(), this.m_goBg);
		}
		Resources.UnloadUnusedAssets();
	}

	public GameObject CreateAssetBundle(string name)
	{
		if (string.IsNullOrEmpty(name))
		{
			return null;
		}
		name = name.ToLower();
		bool flag = false;
		string text = string.Empty;
		AFileSystemBase afileSystemBase = null;
		if (GameUty.BgFiles.ContainsKey(name + ".asset_bg"))
		{
			flag = true;
			text = name + ".asset_bg";
			afileSystemBase = GameUty.BgFiles[name + ".asset_bg"];
		}
		UnityEngine.Object @object = null;
		if (flag && !string.IsNullOrEmpty(text))
		{
			if (!this.asset_bundle_dic.ContainsKey(text))
			{
				using (AFileBase afileBase = afileSystemBase.FileOpen(text))
				{
					byte[] binary = afileBase.ReadAll();
					AssetBundle assetBundle = AssetBundle.LoadFromMemory(binary);
					BgMgr.AssetBundleObj value = default(BgMgr.AssetBundleObj);
					if (assetBundle.mainAsset != null)
					{
						value.obj = assetBundle.mainAsset;
					}
					else
					{
						value.obj = assetBundle.LoadAllAssets<GameObject>()[0];
					}
					value.ab = assetBundle;
					this.asset_bundle_dic.Add(text, value);
					@object = value.obj;
					string f_strFileName = name + ".asset_bg_shader";
					AFileSystemBase afileSystemBase2 = null;
					if (GameUty.FileSystem.IsExistentFile(f_strFileName))
					{
						afileSystemBase2 = GameUty.FileSystem;
					}
					else if (GameUty.FileSystemOld.IsExistentFile(f_strFileName))
					{
						afileSystemBase2 = GameUty.FileSystemOld;
					}
					if (afileSystemBase2 != null)
					{
						byte[] array = null;
						using (AFileBase afileBase2 = afileSystemBase2.FileOpen(f_strFileName))
						{
							array = afileBase2.ReadAll();
						}
						if (array != null)
						{
							this.LoadAssetBgShaderData(@object as GameObject, array);
						}
					}
				}
			}
			else
			{
				@object = this.asset_bundle_dic[text].obj;
			}
		}
		return (!(@object == null)) ? (@object as GameObject) : null;
	}

	private void LoadAssetBgShaderData(GameObject bgObject, byte[] binary)
	{
		if (binary == null || binary.Length <= 0)
		{
			return;
		}
		using (MemoryStream memoryStream = new MemoryStream(binary))
		{
			using (BinaryReader binaryReader = new BinaryReader(memoryStream))
			{
				int num = binaryReader.ReadInt32();
				List<Shader> list = new List<Shader>();
				Dictionary<string, List<int>> dictionary = new Dictionary<string, List<int>>();
				int num2 = (int)binaryReader.ReadInt16();
				for (int i = 0; i < num2; i++)
				{
					string text = binaryReader.ReadString();
					Shader shader = Shader.Find(text);
					if (shader == null)
					{
						Debug.LogError("shader名[" + text + "]からshaderを特定できませんでした");
					}
					else
					{
						list.Add(shader);
					}
				}
				num2 = (int)binaryReader.ReadInt16();
				for (int j = 0; j < num2; j++)
				{
					string key = binaryReader.ReadString();
					List<int> list2 = new List<int>();
					int num3 = (int)binaryReader.ReadInt16();
					for (int k = 0; k < num3; k++)
					{
						list2.Add(binaryReader.ReadInt32());
					}
					dictionary.Add(key, list2);
				}
				Renderer[] componentsInChildren = bgObject.GetComponentsInChildren<Renderer>(true);
				foreach (Renderer renderer in componentsInChildren)
				{
					List<int> list3 = dictionary[renderer.gameObject.name];
					int num4 = 0;
					foreach (Material material in renderer.sharedMaterials)
					{
						if (!(material == null))
						{
							int index = list3[num4];
							material.shader = list[index];
							num4++;
						}
					}
				}
			}
		}
	}

	public void SetPos(Vector3 f_vWorldPos)
	{
		this.m_goParent.transform.position = f_vWorldPos;
	}

	public void SetRot(Vector3 f_vWorldRot)
	{
		this.m_goParent.transform.rotation = Quaternion.Euler(f_vWorldRot);
	}

	public void DeleteBg()
	{
		this.ChangeBg(null);
	}

	public string GetBGName()
	{
		return this.m_strBGName;
	}

	public GameObject AddHitToBg(string f_strSrc)
	{
		UnityEngine.Object @object = Resources.Load("BG_Hit/" + f_strSrc);
		if (@object == null)
		{
			@object = this.CreateAssetBundle(f_strSrc);
		}
		if (@object == null)
		{
			return null;
		}
		GameObject gameObject = UnityEngine.Object.Instantiate(@object) as GameObject;
		gameObject.name = f_strSrc;
		if (this.m_goBg != null)
		{
			gameObject.transform.SetParent(this.m_goBg.transform, false);
		}
		else
		{
			gameObject.transform.SetParent(this.m_goParent.transform, false);
		}
		return gameObject;
	}

	public GameObject AddPrefabToBg(string f_strSrc, string f_strName, string f_strDest, Vector3 f_vPos, Vector3 f_vRot)
	{
		GameObject gameObject = null;
		if (!this.m_dicAttachObj.TryGetValue(f_strName, out gameObject))
		{
			UnityEngine.Object @object = Resources.Load("Prefab/" + f_strSrc);
			if (@object == null)
			{
				@object = this.CreateAssetBundle(f_strSrc);
			}
			if (@object == null)
			{
				return null;
			}
			gameObject = (UnityEngine.Object.Instantiate(@object) as GameObject);
			gameObject.name = f_strName;
			this.m_dicAttachObj.Add(f_strName, gameObject);
		}
		if (gameObject == null)
		{
			Debug.Log("指定のプレハブが見つかりませんでした。[" + f_strSrc + "]");
			return null;
		}
		GameObject gameObject2;
		if (this.m_goBg != null)
		{
			if (!string.IsNullOrEmpty(f_strDest))
			{
				gameObject2 = UTY.GetChildObject(this.m_goBg, f_strDest, false);
			}
			else
			{
				gameObject2 = this.m_goBg;
			}
		}
		else
		{
			gameObject2 = this.m_goParent;
		}
		gameObject.transform.SetParent(gameObject2.transform, false);
		gameObject.transform.localPosition = f_vPos;
		gameObject.transform.localRotation = Quaternion.Euler(f_vRot);
		return gameObject;
	}

	public GameObject AddAssetsBundleToBg(string f_strSrc, string f_strName, string f_strDest, Vector3 f_vPos, Vector3 f_vRot)
	{
		GameObject gameObject = null;
		if (!this.m_dicAttachObj.TryGetValue(f_strName, out gameObject))
		{
			UnityEngine.Object @object = this.CreateAssetBundle(f_strSrc);
			if (@object == null)
			{
				Debug.Log("指定のAssetsBundle見つかりませんでした。[" + f_strSrc + "]");
				return null;
			}
			gameObject = (UnityEngine.Object.Instantiate(@object) as GameObject);
			gameObject.name = f_strName;
			this.m_dicAttachObj.Add(f_strName, gameObject);
		}
		GameObject gameObject2;
		if (this.m_goBg != null)
		{
			if (!string.IsNullOrEmpty(f_strDest))
			{
				gameObject2 = UTY.GetChildObject(this.m_goBg, f_strDest, false);
			}
			else
			{
				gameObject2 = this.m_goBg;
			}
		}
		else
		{
			gameObject2 = this.m_goParent;
		}
		gameObject.transform.SetParent(gameObject2.transform, false);
		gameObject.transform.localPosition = f_vPos;
		gameObject.transform.localRotation = Quaternion.Euler(f_vRot);
		return gameObject;
	}

	public void PrefabAnimatorSetParam<T>(string f_strName, string f_strParam, T f_value) where T : IConvertible
	{
		GameObject gameObject = null;
		if (this.m_dicAttachObj.TryGetValue(f_strName, out gameObject))
		{
			Animator component = gameObject.GetComponent<Animator>();
			if (component != null)
			{
				if (f_value.GetType() == typeof(int))
				{
					component.SetInteger(f_strParam, f_value.ToInt32(null));
				}
				else if (f_value.GetType() == typeof(float))
				{
					component.SetFloat(f_strParam, (float)f_value.ToDouble(null));
				}
				else if (f_value.GetType() == typeof(bool))
				{
					component.SetBool(f_strParam, f_value.ToBoolean(null));
				}
			}
		}
		else
		{
			Debug.LogWarning("プレハブが見つかりません。" + f_strName);
		}
	}

	public void PrefabAnimatorTrigger(string f_strName, string f_strParam, bool f_bReset)
	{
		GameObject gameObject = null;
		if (this.m_dicAttachObj.TryGetValue(f_strName, out gameObject))
		{
			Animator component = gameObject.GetComponent<Animator>();
			if (component != null)
			{
				if (f_bReset)
				{
					component.ResetTrigger(f_strParam);
				}
				else
				{
					component.SetTrigger(f_strParam);
				}
			}
		}
		else
		{
			Debug.LogWarning("プレハブが見つかりません。" + f_strName);
		}
	}

	public void MovePrefabToChara(string f_strPrefabName, Maid f_maid, string f_strDestBone, Vector3 f_vOffsPos, Vector3 f_vOffsRot, float f_fTime)
	{
		BgMgr.MovePfefab value = new BgMgr.MovePfefab(f_strPrefabName, f_maid, f_strDestBone, f_vOffsPos, f_vOffsRot, f_fTime);
		base.StartCoroutine("CoWaitMovePrefabToChara", value);
	}

	private IEnumerator CoWaitMovePrefabToChara(BgMgr.MovePfefab f_mp)
	{
		float fNowTime = 0f;
		if (f_mp.maid == null || f_mp.maid.body0 == null)
		{
			yield break;
		}
		GameObject objTarget = null;
		Transform trTarget = CMT.SearchObjName(f_mp.maid.body0.m_trBones, f_mp.strDestBone, true);
		if (trTarget != null)
		{
			this.m_dicAttachObj.TryGetValue(f_mp.strPrefabName, out objTarget);
		}
		if (objTarget == null)
		{
			yield break;
		}
		while (!(trTarget == null))
		{
			objTarget.transform.position = trTarget.position + f_mp.vOffsPos;
			yield return null;
			if ((fNowTime += Time.deltaTime) > f_mp.fTime)
			{
				yield break;
			}
		}
		yield break;
	}

	public GameObject GetPrefabFromBg(string f_strName)
	{
		if (!this.m_dicAttachObj.ContainsKey(f_strName))
		{
			return null;
		}
		return this.m_dicAttachObj[f_strName];
	}

	public void DelPrefabFromBg(string f_strName)
	{
		GameObject obj = null;
		if (this.m_dicAttachObj.TryGetValue(f_strName, out obj))
		{
			this.m_dicAttachObj.Remove(f_strName);
			UnityEngine.Object.Destroy(obj);
		}
	}

	public void DelPrefabFromBgAll()
	{
		base.StopCoroutine("CoWaitMovePrefabToChara");
		foreach (KeyValuePair<string, GameObject> keyValuePair in this.m_dicAttachObj)
		{
			if (keyValuePair.Value != null)
			{
				UnityEngine.Object.DestroyImmediate(keyValuePair.Value);
			}
		}
		this.m_dicAttachObj.Clear();
	}

	public void OnConnectedToServer()
	{
		this.SetPos(Vector3.zero);
		this.SetRot(Vector3.zero);
	}

	public void OnSaveDD()
	{
		this.SaveOvrDDPos(true);
	}

	private void ShowOvrDD(bool f_bShowForce)
	{
		if (this.m_goParent == null)
		{
			return;
		}
		string text = this.GetBGName();
		if (string.IsNullOrEmpty(text))
		{
			text = "__NoBG";
		}
		if (!GameMain.Instance.CMSystem.SConfig.OvrDDtoBG)
		{
			text = "__NoDDtoBG";
		}
		if (!f_bShowForce)
		{
			CMSystem.TransDD transDD;
			if (!GameMain.Instance.CMSystem.OvrDDBG.TryGetValue(text, out transDD))
			{
				if (this.desktop_screen_ != null)
				{
					UnityEngine.Object.DestroyImmediate(this.desktop_screen_);
					this.desktop_screen_ = null;
				}
				return;
			}
			if (!transDD.m_bShow)
			{
				if (this.desktop_screen_ != null)
				{
					UnityEngine.Object.DestroyImmediate(this.desktop_screen_);
					this.desktop_screen_ = null;
				}
				return;
			}
		}
		if (this.desktop_screen_ == null)
		{
			UnityEngine.Object @object = Resources.Load("Prefab/DesktopScreen");
			if (@object != null)
			{
				this.desktop_screen_ = (UnityEngine.Object.Instantiate(@object) as GameObject);
			}
		}
		if (this.desktop_screen_ != null)
		{
			this.desktop_screen_.transform.SetParent(this.m_goParent.transform);
			this.desktop_screen_.transform.localPosition = new Vector3(0f, 2f, 1f);
			this.desktop_screen_.transform.localRotation = Quaternion.identity;
			this.desktop_screen_.transform.localScale = Vector3.one;
			CMSystem.TransDD transDD2;
			if (GameMain.Instance.CMSystem.OvrDDBG.TryGetValue(text, out transDD2))
			{
				this.desktop_screen_.transform.localPosition = transDD2.m_vPos;
				this.desktop_screen_.transform.localRotation = transDD2.m_qRot;
				this.desktop_screen_.transform.localScale = transDD2.m_vScale;
			}
		}
	}

	private void SaveOvrDDPos(bool f_bShowing)
	{
		if (this.desktop_screen_ == null)
		{
			return;
		}
		string text = this.GetBGName();
		if (string.IsNullOrEmpty(text))
		{
			text = "__NoBG";
		}
		if (!GameMain.Instance.CMSystem.SConfig.OvrDDtoBG)
		{
			text = "__NoDDtoBG";
		}
		CMSystem.TransDD transDD = null;
		if (!GameMain.Instance.CMSystem.OvrDDBG.TryGetValue(text, out transDD))
		{
			transDD = new CMSystem.TransDD();
			GameMain.Instance.CMSystem.OvrDDBG.Add(text, transDD);
		}
		transDD.m_vPos = this.desktop_screen_.transform.localPosition;
		transDD.m_qRot = this.desktop_screen_.transform.localRotation;
		transDD.m_vScale = this.desktop_screen_.transform.localScale;
		transDD.m_bShow = f_bShowing;
	}

	public void Update()
	{
		if (Input.GetKeyDown(KeyCode.F10) && NInput.GetKey(KeyCode.LeftShift))
		{
			this.SetShowDesktopScreen(!this.isShowDesktopScreen);
		}
		else if (Input.GetKey(KeyCode.F11) && NInput.GetKey(KeyCode.LeftShift))
		{
			if (this.desktop_screen_ != null)
			{
				this.desktop_screen_.transform.localScale += new Vector3(0.1f, 0.1f, 0.1f) * Time.deltaTime;
			}
		}
		else if (Input.GetKey(KeyCode.F12) && NInput.GetKey(KeyCode.LeftShift) && this.desktop_screen_ != null)
		{
			this.desktop_screen_.transform.localScale += new Vector3(0.1f, 0.1f, 0.1f) * -Time.deltaTime;
		}
	}

	public bool isShowDesktopScreen
	{
		get
		{
			return this.desktop_screen_ != null;
		}
	}

	public void SetShowDesktopScreen(bool isShow)
	{
		if (isShow)
		{
			if (this.desktop_screen_ == null)
			{
				this.ShowOvrDD(true);
			}
		}
		else if (this.desktop_screen_ != null)
		{
			this.SaveOvrDDPos(false);
			UnityEngine.Object.DestroyImmediate(this.desktop_screen_);
			this.desktop_screen_ = null;
		}
	}

	public GameObject current_bg_object
	{
		get
		{
			return this.m_goBg;
		}
	}

	public GameObject bg_parent_object
	{
		get
		{
			return this.m_goParent;
		}
	}

	private GameObject m_goParent;

	private GameObject m_goBg;

	private Dictionary<string, BgMgr.AssetBundleObj> asset_bundle_dic = new Dictionary<string, BgMgr.AssetBundleObj>();

	private string m_strBGName = string.Empty;

	private Dictionary<string, GameObject> m_dicAttachObj = new Dictionary<string, GameObject>();

	private GameObject desktop_screen_;

	private struct AssetBundleObj
	{
		public AssetBundle ab;

		public UnityEngine.Object obj;
	}

	private class MovePfefab
	{
		public MovePfefab(string f_strPrefabName, Maid f_maid, string f_strDestBone, Vector3 f_vOffsPos, Vector3 f_vOffsRot, float f_fTime)
		{
			this.strPrefabName = f_strPrefabName;
			this.maid = f_maid;
			this.strDestBone = f_strDestBone;
			this.vOffsPos = f_vOffsPos;
			this.vOffsRot = f_vOffsRot;
			this.fTime = f_fTime;
		}

		public string strPrefabName;

		public Maid maid;

		public string strDestBone;

		public Vector3 vOffsPos;

		public Vector3 vOffsRot;

		public float fTime;
	}
}
