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

public class CharaModelReferencer : MonoBehaviour
{
    private void Awake()
	{
		root = base.transform.Find("root");
		if(root != null)
		{
			boneList.Clear();
			MakeBoneList(root);
		}
    }

    private void Update()
	{
		UpdateOsage();
	}

	private void LateUpdate()
    {
		Transform left = boneList.FirstOrDefault(b => b.name == "j_thigh_l");
		Transform right = boneList.FirstOrDefault(b => b.name == "j_thigh_r");
		LateUpdateOsage(left, right);
	}

    public void JoinReferenceParam()
	{
		if (this.refAnimationObj == null) return;

		SimpleAnimation component = this.root.GetComponent<SimpleAnimation>();

		if (component == null) return;

		component.clip = null;

		for(int i = component.GetStates().Count() - 1; i >= 0; i--)
		{
			component.RemoveState(component.GetStates().ToList()[i].name);
        }

		foreach (CharaModelReferencer.RefAnime refAnime in this.refAnimationObj.GetComponent<CharaModelReferencer>().prefabAnimeList)
		{
			if (refAnime.clip != null)
			{
				component.AddState(refAnime.clip, refAnime.name);
			}
		}

		this.rootPos = (this.basePos = this.root.localPosition);
		if (this.refAnimationObj.GetComponent<CharaModelReferencer>().scaleParamList.Count > 0)
		{
			this.root.localPosition = (this.rootPos = new Vector3(this.root.localPosition.x, this.refAnimationObj.GetComponent<CharaModelReferencer>().rootPosY, this.root.localPosition.z));
		}

		component.gameObject.SetActive(false);

		setScaleOne();

		foreach (Transform transform in this.boneList)
		{
			this.setOsage(transform);
			this.setScale(transform);
		}

		component.gameObject.SetActive(true);
	}

    private void MakeBoneList(Transform bone)
	{
		if (bone.GetComponent<CharaModelReferencer>() != null)
		{
			return;
		}
		if (bone.name.IndexOf("j_") == 0 || bone.name == "root" || bone.name == "pelvis")
		{
			this.boneList.Add(bone);
		}
		foreach (object obj in bone)
		{
			Transform bone2 = (Transform)obj;
			this.MakeBoneList(bone2);
		}
	}

	private void setOsage(Transform bone)
	{
		Osage osage = bone.GetComponent<Osage>();
		if (osage == null && bone.name.IndexOf("j_osg") == 0)
		{
			osage = bone.gameObject.AddComponent<Osage>();
		}
		CharaModelReferencer.OsageParam osageParam = this.refAnimationObj.GetComponent<CharaModelReferencer>().osageParamList.Find((CharaModelReferencer.OsageParam itm) => itm.boneName == bone.name);
		if (osageParam != null)
		{
			if (osage == null)
			{
				osage = bone.gameObject.AddComponent<Osage>();
			}
			osage.boneAxis = osageParam.boneAxis;
			osage.stiffnessForce = osageParam.stiffnessForce;
			osage.dragForce = osageParam.dragForce;
			osage.springForce = osageParam.springForce;
		}
		else if (osage != null && bone.name.IndexOf("j_osg") != 0)
		{
            UnityEngine.Object.Destroy(osage);
			osage = null;
		}
		if (osage != null)
		{
			this.osageList.Add(osage);
		}
	}

	private void setScale(Transform bone)
	{
		CharaModelReferencer.ScaleParam scaleParam = this.refAnimationObj.GetComponent<CharaModelReferencer>().scaleParamList.Find((CharaModelReferencer.ScaleParam itm) => itm.boneName == bone.name);
		if (scaleParam != null)
		{
			bone.localScale = scaleParam.boneScale;
		}
	}

	public void setScaleOne()
	{
		if (this.refAnimationObj == null)
		{
			return;
		}
		foreach (Transform transform in this.boneList)
		{
			transform.localScale = Vector3.one;
		}
		this.root.localPosition = (this.rootPos = Vector3.zero);
	}

	public void UpdateOsage()
	{
		if (this.refAnimationObj == null)
		{
			return;
		}
		foreach (Osage osage in this.osageList)
		{
			osage.UpdateOsage();
		}
	}

	public void LateUpdateOsage(Transform thighL, Transform thighR)
	{
		if (this.refAnimationObj == null)
		{
			return;
		}
		foreach (Osage osage in this.osageList)
		{
			osage.LateUpdateOsage(thighL, thighR);
		}
	}

	public void SetRootPos(bool reset)
	{
		this.root.localPosition = (reset ? this.basePos : this.rootPos);
	}

	public void ResetOsage()
	{
		if (this.refAnimationObj == null)
		{
			return;
		}
		foreach (Osage osage in this.osageList)
		{
			osage.Reset();
		}
	}

	public GameObject refAnimationObj;

	private Transform root;

	public List<Transform> boneList = new List<Transform>();

	private List<Osage> osageList = new List<Osage>();

	private Vector3 basePos = Vector3.zero;

	private Vector3 rootPos = Vector3.zero;

	public CharaMotionDefine.MotionPersonalityType motionPersonalityType;

	public List<CharaModelReferencer.RefAnime> prefabAnimeList = new List<CharaModelReferencer.RefAnime>();

	public List<CharaModelReferencer.OsageParam> osageParamList = new List<CharaModelReferencer.OsageParam>();

	public float rootPosY;

	public List<CharaModelReferencer.ScaleParam> scaleParamList = new List<CharaModelReferencer.ScaleParam>();

	public Color eyeColor = new Color(0f, 0f, 0f, 0f);

	public string tailNodeName = "";

	public string earNodeName = "";

	public string variantCopyPrefabName = "";

	public bool ussVariantCopyPrefabVoice;

	[Serializable]
	public class RefAnime
	{
		// Token: 0x06004018 RID: 16408 RVA: 0x001C7E0F File Offset: 0x001C600F
		public RefAnime()
		{
		}

		// Token: 0x06004019 RID: 16409 RVA: 0x001C7E30 File Offset: 0x001C6030
		public RefAnime(string nam, AnimationClip clp, string se, int eb, int ef, int ea, int tb, int tf, int ta)
		{
			this.name = nam;
			this.clip = clp;
			this.seName = se;
			this.earBefore = eb;
			this.earFrame = ef;
			this.earAfter = ea;
			this.tailBefore = tb;
			this.tailFrame = tf;
			this.tailAfter = ta;
		}

		// Token: 0x0400481C RID: 18460
		public string name = "";

		// Token: 0x0400481D RID: 18461
		public AnimationClip clip;

		// Token: 0x0400481E RID: 18462
		public string seName = "";

		// Token: 0x0400481F RID: 18463
		public int earBefore;

		// Token: 0x04004820 RID: 18464
		public int earFrame;

		// Token: 0x04004821 RID: 18465
		public int earAfter;

		// Token: 0x04004822 RID: 18466
		public int tailBefore;

		// Token: 0x04004823 RID: 18467
		public int tailFrame;

		// Token: 0x04004824 RID: 18468
		public int tailAfter;
	}

	[Serializable]
	public class OsageParam
	{
		// Token: 0x04004825 RID: 18469
		public string boneName;

		// Token: 0x04004826 RID: 18470
		public Vector3 boneAxis;

		// Token: 0x04004827 RID: 18471
		public float stiffnessForce;

		// Token: 0x04004828 RID: 18472
		public float dragForce;

		// Token: 0x04004829 RID: 18473
		public Vector3 springForce;
	}

	[Serializable]
	public class ScaleParam
	{
		// Token: 0x0400482A RID: 18474
		public string boneName;

		// Token: 0x0400482B RID: 18475
		public Vector3 boneScale;
	}
}
