using System;
using System.Collections;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using Wizard.Battle.View.Vfx;

public class Effect3dInformation : MonoBehaviour
{
	public enum ModelBone
	{
		None = 1000,
		Hip = 0,
		Waist = 1,
		Chest = 2,
		Neck = 3,
		Head = 4,
		Shoulder_L = 5,
		Arm_L = 6,
		Elbow_L = 7,
		Wrist_L = 8,
		Shoulder_R = 9,
		Arm_R = 10,
		Elbow_R = 11,
		Wrist_R = 12,
		Eye_L = 13,
		Eye_R = 14,
		Mouth_Root = 15,
		Prop_Under_Root = 16,
		Prop_Top_Root = 17,
		Bone_Max = 18
	}

	[Serializable]
	public class EffectData
	{
		public GameObject Effect;

		private GameObject _effectInstance;

		private GameObject _attachRoot;

		public ModelBone AttachBone = ModelBone.None;

		private ModelBone _selectBone = ModelBone.None;

		public Vector3 Offset;

		public Vector3 RotateOffset;

		public ClassCharaPrm.MotionType PlayMotionType = ClassCharaPrm.MotionType.extra;

		private ClassCharaPrm.MotionType _nowMotionType = ClassCharaPrm.MotionType.idle;

		public float DelayTime;

		public float PlayTime;

		private bool _isPlay;

		public string EffectPath = "";

		private bool _isLoad;

		private bool _isSetShader;

		private bool _isLoadMaterial;

		private bool _isLoadingMaterial;

		private string _testResoucePath = "Assets/_Wizard/Resources/";

		private string _testEffectResoucePath = "Model/Effect/";

		private string _resoucePath = "Assets/_WizardResources2/Resources/";

		private string _effectResoucePath = "Jpn/Effect/Effects/";

		public void UpdatePosition(Effect3dInformation info)
		{
			if (Effect != null)
			{
				if (_effectInstance == null)
				{
					LoadEffectInstance(info);
				}
				else
				{
					if (Effect.name != _effectInstance.name)
					{
						LoadEffectInstance(info);
					}
					if (!_isLoadingMaterial)
					{
						if (!info.IsTestScene)
						{
							LoadParticleMaterials(_effectInstance);
						}
						_isLoadingMaterial = true;
					}
					else if (_isLoadMaterial && !_isSetShader)
					{
						SetParticleShader(_effectInstance);
						_isSetShader = true;
					}
				}
			}
			if (_effectInstance != null && _isPlay)
			{
				if (AttachBone != _selectBone || _attachRoot == null)
				{
					_attachRoot = info._boneList[AttachBone];
					_selectBone = AttachBone;
				}
				Vector3 position = _attachRoot.transform.position;
				Quaternion quaternion = Quaternion.Euler(RotateOffset);
				_effectInstance.transform.localRotation = _attachRoot.transform.rotation * quaternion;
				Vector3 vector = _effectInstance.transform.rotation * Offset;
				_effectInstance.transform.localPosition = position + vector;
			}
		}

		public void DestroyEffect()
		{
			if (_effectInstance != null)
			{
				UnityEngine.Object.Destroy(_effectInstance);
			}
		}

		public void LoadEffectInstance(Effect3dInformation info)
		{
			SequentialVfxPlayer sequentialVfxPlayer = SequentialVfxPlayer.Create();
			if (!(_effectInstance == null) || !(EffectPath != "") || _isLoad)
			{
				return;
			}
			if (EffectPath.Contains(_testResoucePath))
			{
				EffectPath = EffectPath.Replace(_testResoucePath, "");
			}
			if (EffectPath.Contains(_testEffectResoucePath))
			{
				EffectPath = EffectPath.Replace(_testEffectResoucePath, "");
			}
			if (EffectPath.Contains(_resoucePath))
			{
				EffectPath = EffectPath.Replace(_resoucePath, "");
			}
			if (EffectPath.Contains(_effectResoucePath))
			{
				EffectPath = EffectPath.Replace(_effectResoucePath, "");
			}
			if (EffectPath.Contains(".prefab"))
			{
				EffectPath = EffectPath.Replace(".prefab", "");
			}
			_ = _effectResoucePath + EffectPath;
			Toolbox.ResourcesManager.GetAssetTypePath(EffectPath, ResourcesManager.AssetLoadPathType.Effect2D);
			sequentialVfxPlayer.Register(InstantVfx.Create(delegate
			{
				if (info.IsLoad && !(_effectInstance != null))
				{
					string assetTypePath = Toolbox.ResourcesManager.GetAssetTypePath(EffectPath, ResourcesManager.AssetLoadPathType.Effect2D, isfetch: true);
					UnityEngine.Object original = Toolbox.ResourcesManager.LoadObject(assetTypePath);
					_effectInstance = UnityEngine.Object.Instantiate(original, info.transform.parent) as GameObject;
					Effect = _effectInstance;
					_effectInstance.SetLayer(0, isSetChildren: true);
					_isPlay = true;
				}
			}));
			sequentialVfxPlayer.Register(WaitVfx.Create(0.1f));
			sequentialVfxPlayer.Register(InstantVfx.Create(delegate
			{
				if (!(_effectInstance == null))
				{
					_effectInstance.SetActive(value: false);
					_isPlay = false;
					_isLoad = true;
				}
			}));
			BattleManagerBase.GetIns().VfxMgr.RegisterImmediateVfx(sequentialVfxPlayer);
		}

		public void SetParticleShader(GameObject effectObj)
		{
			if (!_isLoadMaterial)
			{
				return;
			}
			Renderer[] componentsInChildren = effectObj.transform.GetComponentsInChildren<Renderer>(includeInactive: true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (componentsInChildren[i].name.Contains("skn_") || componentsInChildren[i].name.Contains("emitter") || componentsInChildren[i].name.Contains("null"))
				{
					continue;
				}
				ParticleSystemRenderer particleSystemRenderer = componentsInChildren[i] as ParticleSystemRenderer;
				if (particleSystemRenderer != null)
				{
					string[] array = componentsInChildren[i].name.Split('&');
					if (particleSystemRenderer.trailMaterial != null || array.Length > 1)
					{
						particleSystemRenderer.trailMaterial = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(array[1], ResourcesManager.AssetLoadPathType.Effect2DMaterials, isfetch: true)) as Material;
						particleSystemRenderer.trailMaterial.shader = Shader.Find(particleSystemRenderer.trailMaterial.shader.name);
					}
					componentsInChildren[i].material = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(array[0], ResourcesManager.AssetLoadPathType.Effect2DMaterials, isfetch: true)) as Material;
					componentsInChildren[i].material.shader = Shader.Find(componentsInChildren[i].material.shader.name);
				}
				else
				{
					componentsInChildren[i].material = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(componentsInChildren[i].name, ResourcesManager.AssetLoadPathType.Effect2DMaterials, isfetch: true)) as Material;
					componentsInChildren[i].material.shader = Shader.Find(componentsInChildren[i].material.shader.name);
				}
			}
		}

		private void SetMaterial(Renderer renderer)
		{
			if (renderer.name.Contains("&"))
			{
				string[] array = renderer.name.Split('&');
				for (int i = 0; i < array.Length; i++)
				{
					Toolbox.ResourcesManager.GetAssetTypePath(array[i], ResourcesManager.AssetLoadPathType.Effect2DMaterials);
					renderer.material = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(renderer.name, ResourcesManager.AssetLoadPathType.Effect2DMaterials, isfetch: true)) as Material;
					renderer.materials[i].shader = Shader.Find(renderer.materials[i].shader.name);
				}
			}
			else
			{
				Toolbox.ResourcesManager.GetAssetTypePath(renderer.name, ResourcesManager.AssetLoadPathType.Effect2DMaterials);
				renderer.material = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(renderer.name, ResourcesManager.AssetLoadPathType.Effect2DMaterials, isfetch: true)) as Material;
				renderer.material.shader = Shader.Find(renderer.material.shader.name);
			}
		}

		public void LoadParticleMaterials(GameObject effectObj)
		{
			List<string> list = new List<string>();
			Renderer[] componentsInChildren = effectObj.transform.GetComponentsInChildren<Renderer>(includeInactive: true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (componentsInChildren[i].name.Contains("skn_") || componentsInChildren[i].name.Contains("emitter") || componentsInChildren[i].name.Contains("null"))
				{
					continue;
				}
				if (componentsInChildren[i].name.Contains("&"))
				{
					string[] array = componentsInChildren[i].name.Split('&');
					for (int j = 0; j < array.Length; j++)
					{
						string assetTypePath = Toolbox.ResourcesManager.GetAssetTypePath(array[j], ResourcesManager.AssetLoadPathType.Effect2DMaterials);
						if (assetTypePath.IsNotNullOrEmpty() && !list.Contains(assetTypePath))
						{
							list.Add(assetTypePath);
						}
					}
				}
				else
				{
					string assetTypePath2 = Toolbox.ResourcesManager.GetAssetTypePath(componentsInChildren[i].name, ResourcesManager.AssetLoadPathType.Effect2DMaterials);
					list.Add(assetTypePath2);
				}
			}
			BattleCoroutine.GetInstance().StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupSync(list, delegate
			{
				_isLoadMaterial = true;
			}));
		}

		public void UpdateInformation(ClassCharaPrm.MotionType type)
		{
			if (_effectInstance != null && PlayMotionType != type && _nowMotionType != type && _isPlay)
			{
				_effectInstance.SetActive(value: false);
			}
			_nowMotionType = type;
		}

		public void Play(Effect3dInformation info)
		{
			if (DelayTime > 0f)
			{
				if (_effectInstance == null)
				{
					Debug.LogError("Play effect play error! " + EffectPath);
				}
				else
				{
					info.StartCoroutine(PlayEffect(info));
				}
			}
			else if (!(_effectInstance == null))
			{
				_effectInstance.SetActive(value: true);
				_isPlay = true;
				if (PlayTime > 0f)
				{
					info.StartCoroutine(StopEffect());
				}
			}
		}

		private IEnumerator PlayEffect(Effect3dInformation info)
		{
			yield return new WaitForSeconds(DelayTime);
			_effectInstance.SetActive(value: true);
			_isPlay = true;
			if (PlayTime > 0f)
			{
				info.StartCoroutine(StopEffect());
			}
		}

		private IEnumerator StopEffect()
		{
			yield return new WaitForSeconds(PlayTime);
			_effectInstance.SetActive(value: false);
			_isPlay = false;
		}
	}

	[Serializable]
	public class EffectList
	{
		public EffectData[] _effectData;
	}

	public GameObject ModelRoot;

	public GameObject FaceRoot;

	public GameObject PropRoot;

	private Dictionary<ModelBone, GameObject> _boneList;

	public EffectList EffectsList;

	public bool FixedEffect;

	public bool SaveButton;

	public bool LoadButton;

	private string _number = "";

	public bool IsLoad;

	public bool IsTestScene;

	private void Start()
	{
		_boneList = new Dictionary<ModelBone, GameObject>();
		_boneList.Add(ModelBone.None, ModelRoot);
		for (int i = 0; i < 18; i++)
		{
			ModelBone key = (ModelBone)i;
			string text = key.ToString();
			GameObject children = GetChildren(ModelRoot.transform, text);
			if (children == null)
			{
				children = GetChildren(FaceRoot.transform, text);
				if (PropRoot != null && children == null)
				{
					string boneName = text.Replace("Prop_", "");
					children = GetChildren(PropRoot.transform, boneName);
				}
			}
			if (children != null)
			{
				_boneList.Add(key, children.gameObject);
			}
		}
		if (IsTestScene)
		{
			IsLoad = true;
			return;
		}
		List<string> list = new List<string>();
		EffectData[] effectData = EffectsList._effectData;
		foreach (EffectData effectData2 in effectData)
		{
			list.Add(Toolbox.ResourcesManager.GetAssetTypePath(effectData2.EffectPath, ResourcesManager.AssetLoadPathType.Effect2D));
		}
		StartCoroutine(Toolbox.ResourcesManager.LoadAssetGroupSync(list, delegate
		{
			IsLoad = true;
		}));
	}

	public void UpdateInfo(ClassCharaPrm.MotionType type)
	{
		if (IsLoad && EffectsList._effectData.Length != 0)
		{
			EffectData[] effectData = EffectsList._effectData;
			for (int i = 0; i < effectData.Length; i++)
			{
				effectData[i].UpdateInformation(type);
			}
		}
	}

	public void PlayEffect(ClassCharaPrm.MotionType type)
	{
		if (EffectsList._effectData.Length == 0)
		{
			return;
		}
		EffectData[] effectData = EffectsList._effectData;
		foreach (EffectData effectData2 in effectData)
		{
			if (effectData2.PlayMotionType == type)
			{
				effectData2.Play(this);
			}
		}
	}

	private void LateUpdate()
	{
		if (EffectsList._effectData.Length != 0)
		{
			EffectData[] effectData = EffectsList._effectData;
			for (int i = 0; i < effectData.Length; i++)
			{
				effectData[i].UpdatePosition(this);
			}
		}
	}

	public void DestroyEffect()
	{
		if (EffectsList._effectData != null)
		{
			EffectData[] effectData = EffectsList._effectData;
			for (int i = 0; i < effectData.Length; i++)
			{
				effectData[i]?.DestroyEffect();
			}
			EffectsList._effectData = null;
		}
	}

	private GameObject GetChildren(Transform t, string boneName)
	{
		if (t.gameObject.name.Contains(boneName))
		{
			return t.gameObject;
		}
		if (t.childCount == 0)
		{
			return null;
		}
		for (int i = 0; i < t.childCount; i++)
		{
			GameObject children = GetChildren(t.GetChild(i), boneName);
			if (children != null)
			{
				return children;
			}
		}
		return null;
	}

	public void SaveInfo()
	{
	}

	public void LoadInfo(string number = "3604")
	{
		EffectData[] effectData = EffectsList._effectData;
		for (int i = 0; i < effectData.Length; i++)
		{
			effectData[i].LoadEffectInstance(this);
		}
		_number = number;
	}
}
