using System;
using System.Collections.Generic;
using UnityEngine;
using Wizard.Battle.Player.ClassCharacter;

public class Charactor3dInformation : MonoBehaviour
{
	public GameObject[] Meshes;

	public Material[] Materials;

	public Animator[] Animators;

	public Transform[] LeftEyeInfoObject;

	public Transform[] RightEyeInfoObject;

	public Effect3dInformation EffectInfo;

	public Transform BodyNeck;

	public Transform FaceNeck;

	public Transform BodyHead;

	public Transform FaceHead;

	public Transform BodyHip;

	public Transform TailHip;

	public Transform BodyProp;

	public Transform PropProp;

	public GameObject Prop;

	public Transform HeadAttach;

	public Transform HeadCenterOffset;

	public Transform HeadTubeCenterOffset;

	public Transform HeadAttachR;

	public Transform HeadAttachL;

	private Material EyeMaterial;

	private Material FaceSkinMaterial;

	private Material HairMaterial;

	private Material BodyMaterial;

	public GameObject Quad;

	private string _oldMotion = "";

	private Class3dCharacterBase _characterBase;

	private string _characterId = "";

	private bool _isInitialized;

	private bool _isLoadMesh;

	private bool _isLoadedMesh;

	public bool IsTestScene;

	private List<Mesh> _meshList = new List<Mesh>();

	private Class3dPostImageEffect _postEffect;

	private void Start()
	{
		string text = base.name;
		text = text.Replace("chr", "");
		_characterId = text.Replace("(Clone)", "");
		if (Prop != null)
		{
			Prop.SetActive(value: true);
		}
		if (!IsTestScene)
		{
			_isLoadMesh = true;
		}
	}

	private void Update()
	{
		if ((_isLoadedMesh && !_isInitialized) || (IsTestScene && !_isInitialized))
		{
			Initialize();
			_isInitialized = true;
		}
		if (_isLoadMesh && !_isLoadedMesh)
		{
			InitializeMesh(_characterBase, _postEffect);
			_isLoadedMesh = true;
		}
		if (EyeMaterial != null)
		{
			Vector4[] values = new Vector4[3]
			{
				new Vector4(LeftEyeInfoObject[0].localPosition.x, LeftEyeInfoObject[0].localPosition.y, LeftEyeInfoObject[0].localPosition.z, LeftEyeInfoObject[0].localScale.x),
				new Vector4(LeftEyeInfoObject[1].localPosition.x, LeftEyeInfoObject[1].localPosition.y, LeftEyeInfoObject[1].localPosition.z, LeftEyeInfoObject[1].localScale.x),
				new Vector4(LeftEyeInfoObject[2].localPosition.x, LeftEyeInfoObject[2].localPosition.y, LeftEyeInfoObject[2].localPosition.z, LeftEyeInfoObject[2].localScale.x)
			};
			EyeMaterial.SetVectorArray("_HighParam1", values);
			Vector4[] values2 = new Vector4[2]
			{
				new Vector4(RightEyeInfoObject[0].localPosition.x, RightEyeInfoObject[0].localPosition.y, RightEyeInfoObject[0].localPosition.z, RightEyeInfoObject[0].localScale.x),
				new Vector4(RightEyeInfoObject[1].localPosition.x, RightEyeInfoObject[1].localPosition.y, RightEyeInfoObject[1].localPosition.z, RightEyeInfoObject[1].localScale.x)
			};
			EyeMaterial.SetVectorArray("_HighParam2", values2);
		}
	}

	private void Initialize()
	{
		SkinnedMeshRenderer[] componentsInChildren = GetComponentsInChildren<SkinnedMeshRenderer>();
		MeshRenderer meshRenderer = null;
		if (Prop != null)
		{
			MeshRenderer[] componentsInChildren2 = Prop.GetComponentsInChildren<MeshRenderer>();
			if (componentsInChildren2.Length != 0)
			{
				meshRenderer = componentsInChildren2[0];
			}
		}
		int num = 0;
		SkinnedMeshRenderer[] array = componentsInChildren;
		for (int i = 0; i < array.Length; i++)
		{
			Material[] materials = array[i].materials;
			num += materials.Length;
		}
		num += ((meshRenderer != null) ? 1 : 0);
		Materials = new Material[num];
		int num2 = 0;
		array = componentsInChildren;
		foreach (SkinnedMeshRenderer skinnedMeshRenderer in array)
		{
			Material[] materials2 = skinnedMeshRenderer.materials;
			for (int j = 0; j < materials2.Length; j++)
			{
				skinnedMeshRenderer.sharedMaterials[j].shader = Shader.Find(materials2[j].shader.name);
				Materials[num2] = skinnedMeshRenderer.sharedMaterials[j];
				if (skinnedMeshRenderer.sharedMaterials[j].name.Contains("face"))
				{
					FaceSkinMaterial = skinnedMeshRenderer.sharedMaterials[j];
				}
				if (skinnedMeshRenderer.sharedMaterials[j].name.Contains("hair"))
				{
					HairMaterial = skinnedMeshRenderer.sharedMaterials[j];
				}
				if (skinnedMeshRenderer.sharedMaterials[j].name.Contains("eye"))
				{
					EyeMaterial = skinnedMeshRenderer.sharedMaterials[j];
				}
				if (skinnedMeshRenderer.sharedMaterials[j].name.Contains("bdy"))
				{
					BodyMaterial = skinnedMeshRenderer.sharedMaterials[j];
				}
				num2++;
			}
		}
		if (meshRenderer != null)
		{
			meshRenderer.sharedMaterial.shader = Shader.Find(meshRenderer.sharedMaterial.shader.name);
			Materials[num2] = meshRenderer.sharedMaterial;
		}
		InitializeMaterials();
		EffectInfo = GetComponent<Effect3dInformation>();
		if (Prop != null)
		{
			Prop.SetActive(value: false);
		}
	}

	public void OnApplicationFocus(bool focus)
	{
		if (focus)
		{
			InitializeMaterials();
		}
	}

	public void InitializeMaterials()
	{
		Material[] materials = Materials;
		foreach (Material material in materials)
		{
			if (!(material == null))
			{
				material.SetColor("_GlobalToonColor", Color.white);
				material.SetColor("_GlobalRimColor", Color.white);
				material.SetFloat("_GlobalOutlineWidth", 1f);
				material.SetFloat("_GlobalOutlineOffset", 1f);
				material.SetColor("_Global_FogColor", new Color(0.76f, 1f, 1f, 1f));
				material.SetVector("_Global_FogMinDistance", Vector4.zero);
				material.SetVector("_Global_FogLength", new Vector4(1000f, 1000f, 1000f, 1000f));
				material.SetInt("_Global_MaxDensity", 1);
				material.SetFloat("_Global_MaxHeight", 100f);
				material.SetVector("_Global_FogWorld_Origin", Vector4.zero);
				material.SetColor("_LightColorWizard", Color.white);
				material.SetFloat("_RimStep", 0.5f);
				material.SetFloat("_RimStep2", 0.1f);
				material.SetFloat("_RimSpecRate", 0.5f);
			}
		}
	}

	private void LateUpdate()
	{
		AnimatorClipInfo[] currentAnimatorClipInfo = Animators[0].GetCurrentAnimatorClipInfo(0);
		if (currentAnimatorClipInfo != null && currentAnimatorClipInfo.Length != 0)
		{
			string text = currentAnimatorClipInfo[0].clip.name;
			foreach (ClassCharaPrm.MotionType value5 in Enum.GetValues(typeof(ClassCharaPrm.MotionType)))
			{
				if (text.Contains(value5.ToString()))
				{
					if (EffectInfo != null)
					{
						EffectInfo.UpdateInfo(value5);
					}
					break;
				}
			}
			Vector3 localScale = HeadAttach.localScale;
			Color value = new Color(localScale.x, localScale.y, localScale.z);
			Vector3 vector = HeadAttach.localRotation * Vector3.up;
			Material[] materials = Materials;
			foreach (Material material in materials)
			{
				if (!(material == null))
				{
					material.SetColor("_CharaColor", value);
					material.SetVector("_WorldSpaceLightPosWizard", vector);
				}
			}
			string value2 = ClassCharaPrm.MotionType.extra.ToString();
			if (_characterBase != null && _characterBase.IsPlayer)
			{
				if (text.Equals(value2) && !_oldMotion.Equals(value2))
				{
					_characterBase.EnableEvolve(enable: true);
				}
				else if (!text.Equals(value2) && _oldMotion.Equals(value2))
				{
					_characterBase.EnableEvolve(enable: false);
				}
			}
			if (_postEffect != null && text.Equals(value2))
			{
				AnimatorStateInfo currentAnimatorStateInfo = Animators[0].GetCurrentAnimatorStateInfo(0);
				float num = (float)currentAnimatorClipInfo.Length * currentAnimatorStateInfo.normalizedTime;
				int num2 = (int)(85f * num) + 1;
				if (_postEffect._param.FadeOutEndFrame <= num2)
				{
					_postEffect._param.DiffusionThreshold = _postEffect._param.EndDiffusionThreshold;
				}
				else if (_postEffect._param.FadeOutStartFrame <= num2)
				{
					float num3 = _postEffect._param.FadeOutEndFrame - _postEffect._param.FadeOutStartFrame;
					float t = 0f;
					if (num3 > 0f)
					{
						t = 1f / num3;
					}
					_postEffect._param.DiffusionThreshold = Mathf.Lerp(_postEffect._param.DiffusionThreshold, _postEffect._param.EndDiffusionThreshold, t);
				}
				else
				{
					_postEffect._param.DiffusionThreshold = _postEffect._param.DefaultDiffusionThreshold;
				}
			}
			if (text != _oldMotion && BodyMaterial != null)
			{
				if (text.Contains(ClassCharaPrm.MotionType.idle.ToString()))
				{
					BodyMaterial.renderQueue = 1999;
				}
				if ((_characterId == "3606" && text.Contains("extra_2")) || (_characterId == "3603" && (text.Contains("extra_2") || text.Contains("shock") || text == "positive")) || (_characterId == "3617" && text.Contains("nagative_2_a")))
				{
					BodyMaterial.renderQueue = 2001;
				}
			}
			if (Prop != null)
			{
				if (text.Equals(value2) && !_oldMotion.Equals(value2))
				{
					Prop.SetActive(value: true);
				}
				else if (!text.Equals(value2) && _oldMotion.Equals(value2))
				{
					Prop.SetActive(value: false);
				}
			}
			_oldMotion = text;
		}
		if (HeadCenterOffset != null && FaceSkinMaterial != null)
		{
			Vector3 forward = BodyHead.forward;
			Vector3 up = BodyHead.up;
			Vector3 vector2 = BodyHead.position + up * HeadCenterOffset.localPosition.y + forward * HeadCenterOffset.localPosition.z;
			Vector3 vector3 = BodyHead.position + forward * HeadTubeCenterOffset.localPosition.z;
			FaceSkinMaterial.SetVector("_FaceCenterPos", vector3);
			HairMaterial.SetVector("_FaceCenterPos", vector2);
			EyeMaterial.SetVector("_FaceCenterPos", vector3);
			FaceSkinMaterial.SetVector("_FaceUp", up);
			HairMaterial.SetVector("_FaceUp", up);
			EyeMaterial.SetVector("_FaceUp", up);
			FaceSkinMaterial.SetVector("_FaceForward", forward);
			HairMaterial.SetVector("_FaceForward", forward);
			EyeMaterial.SetVector("_FaceForward", forward);
			if (HeadAttachR != null)
			{
				Material[] materials = Materials;
				foreach (Material material2 in materials)
				{
					if (!(material2 == null))
					{
						Vector3 localPosition = HeadAttachR.localPosition;
						material2.SetFloat("_RimStep", localPosition.x);
						material2.SetFloat("_RimFeather", localPosition.y);
						material2.SetFloat("_RimSpecRate", localPosition.z);
						if (material2.HasProperty("_RimColor"))
						{
							Color color = material2.GetColor("_RimColor");
							Vector3 localScale2 = HeadAttachR.localScale;
							Color value3 = new Color(localScale2.x, localScale2.y, localScale2.z, color.a);
							material2.SetColor("_RimColor", value3);
						}
						Vector3 localScale3 = HeadAttachL.localScale;
						Color value4 = new Color(localScale3.x, localScale3.y, localScale3.z, 0f);
						material2.SetColor("_ToonDarkColor", value4);
					}
				}
			}
		}
		UpdateTransform(FaceNeck, BodyNeck);
		FaceHead.localPosition = BodyHead.localPosition;
		FaceHead.localRotation = BodyHead.localRotation;
		UpdateTransform(TailHip, BodyHip);
		if (Prop != null && Prop.activeSelf && BodyProp != null && PropProp != null)
		{
			UpdateTransform(PropProp, BodyProp);
		}
	}

	private void UpdateTransform(Transform face, Transform body)
	{
		face.position = body.position;
		face.localRotation = body.rotation;
	}

	public void InitializeMesh(Class3dCharacterBase characterBase = null, Class3dPostImageEffect postEffect = null)
	{
		_characterBase = characterBase;
		_postEffect = postEffect;
		Mesh mesh = new Mesh();
		mesh.vertices = new Vector3[4]
		{
			new Vector3(-0.5f, -0.5f, 0f),
			new Vector3(-0.5f, 0.5f, 0f),
			new Vector3(0.5f, 0.5f, 0f),
			new Vector3(0.5f, -0.5f, 0f)
		};
		mesh.triangles = new int[6] { 0, 1, 3, 1, 2, 3 };
		mesh.uv = new Vector2[4]
		{
			new Vector2(0.65f, 0.39f),
			new Vector2(0.65f, 0.69f),
			new Vector2(0.35f, 0.69f),
			new Vector2(0.35f, 0.39f)
		};
		mesh.RecalculateBounds();
		mesh.RecalculateNormals();
		mesh.RecalculateTangents();
		Quad.GetComponent<MeshFilter>().mesh = mesh;
	}

	public void Destroy()
	{
		if (Materials != null)
		{
			for (int i = 0; i < Materials.Length; i++)
			{
				Material material = Materials[i];
				if (!(material == null))
				{
					UnityEngine.Object.Destroy(material);
					Materials[i] = null;
				}
			}
			Materials = null;
		}
		if (Quad != null)
		{
			if (Quad.GetComponent<MeshFilter>().mesh != null)
			{
				UnityEngine.Object.Destroy(Quad.GetComponent<MeshFilter>().mesh);
				Quad.GetComponent<MeshFilter>().mesh = null;
			}
			MeshRenderer component = Quad.GetComponent<MeshRenderer>();
			if (component != null && component.sharedMaterial != null)
			{
				RenderTexture renderTexture = component.sharedMaterial.mainTexture as RenderTexture;
				component.sharedMaterial.mainTexture = null;
				if (renderTexture != null)
				{
					renderTexture.Release();
				}
				UnityEngine.Object.Destroy(component.sharedMaterial);
				component.sharedMaterial = null;
			}
			UnityEngine.Object.Destroy(Quad);
			Quad = null;
		}
		if (_postEffect != null)
		{
			_postEffect.Destroy();
			_postEffect = null;
		}
	}
}
