using System;
using System.IO;
using UnityEngine;

[AddComponentMenu("Dynamic Bone/Dynamic Bone Mune Collider")]
public class DynamicBoneMuneCollider : DynamicBoneColliderBase
{
	public override string TypeName
	{
		get
		{
			return "dbm";
		}
	}

	public override void Serialize(BinaryWriter f_bw, int f_nVersion)
	{
		base.Serialize(f_bw, f_nVersion);
		f_bw.Write(this.m_Radius);
		f_bw.Write(this.m_Height);
		f_bw.Write(this.m_fScaleRateMulMax);
		f_bw.Write(this.m_CenterRateMax.x);
		f_bw.Write(this.m_CenterRateMax.y);
		f_bw.Write(this.m_CenterRateMax.z);
	}

	public override void Deserialize(BinaryReader f_br, int f_nVersion, Transform f_trBodyBoneRoot)
	{
		base.Deserialize(f_br, f_nVersion, f_trBodyBoneRoot);
		this.m_Radius = f_br.ReadSingle();
		this.m_Height = f_br.ReadSingle();
		this.m_fScaleRateMulMax = f_br.ReadSingle();
		this.m_CenterRateMax.Set(f_br.ReadSingle(), f_br.ReadSingle(), f_br.ReadSingle());
	}

	private void OnValidate()
	{
		this.m_Radius = Mathf.Max(this.m_Radius, 0f);
		this.m_Height = Mathf.Max(this.m_Height, 0f);
	}

	public override bool Collide(ref Vector3 particlePosition, float particleRadius)
	{
		float num = 0f;
		if (this.m_maid != null)
		{
			num = this.m_maid.body0.jbMuneL.BlendValue / 1.3f;
		}
		float num2 = 1f + this.m_fScaleRateMulMax * num;
		Vector3 vector = Vector3.Lerp(this.m_Center, this.m_CenterRateMax, num);
		float num3 = this.m_Radius * Mathf.Abs(base.transform.lossyScale.x * num2);
		float num4 = this.m_Height * 0.5f - this.m_Radius;
		if (num4 <= 0f)
		{
			if (this.m_Bound == DynamicBoneColliderBase.Bound.Outside)
			{
				DynamicBoneMuneCollider.OutsideSphere(ref particlePosition, particleRadius, base.transform.TransformPoint(vector), num3);
			}
			else
			{
				DynamicBoneMuneCollider.InsideSphere(ref particlePosition, particleRadius, base.transform.TransformPoint(vector), num3);
			}
		}
		else
		{
			Vector3 position = vector;
			Vector3 position2 = vector;
			DynamicBoneColliderBase.Direction direction = this.m_Direction;
			if (direction != DynamicBoneColliderBase.Direction.X)
			{
				if (direction != DynamicBoneColliderBase.Direction.Y)
				{
					if (direction == DynamicBoneColliderBase.Direction.Z)
					{
						position.z -= num4;
						position2.z += num4;
					}
				}
				else
				{
					position.y -= num4;
					position2.y += num4;
				}
			}
			else
			{
				position.x -= num4;
				position2.x += num4;
			}
			if (this.m_Bound == DynamicBoneColliderBase.Bound.Outside)
			{
				DynamicBoneMuneCollider.OutsideCapsule(ref particlePosition, particleRadius, base.transform.TransformPoint(position), base.transform.TransformPoint(position2), num3);
			}
			else
			{
				DynamicBoneMuneCollider.InsideCapsule(ref particlePosition, particleRadius, base.transform.TransformPoint(position), base.transform.TransformPoint(position2), num3);
			}
		}
		return false;
	}

	private static void OutsideSphere(ref Vector3 particlePosition, float particleRadius, Vector3 sphereCenter, float sphereRadius)
	{
		float num = sphereRadius + particleRadius;
		float num2 = num * num;
		Vector3 a = particlePosition - sphereCenter;
		float sqrMagnitude = a.sqrMagnitude;
		if (sqrMagnitude > 0f && sqrMagnitude < num2)
		{
			float num3 = Mathf.Sqrt(sqrMagnitude);
			particlePosition = sphereCenter + a * (num / num3);
		}
	}

	private static void InsideSphere(ref Vector3 particlePosition, float particleRadius, Vector3 sphereCenter, float sphereRadius)
	{
		float num = sphereRadius - particleRadius;
		float num2 = num * num;
		Vector3 a = particlePosition - sphereCenter;
		float sqrMagnitude = a.sqrMagnitude;
		if (sqrMagnitude > num2)
		{
			float num3 = Mathf.Sqrt(sqrMagnitude);
			particlePosition = sphereCenter + a * (num / num3);
		}
	}

	private static void OutsideCapsule(ref Vector3 particlePosition, float particleRadius, Vector3 capsuleP0, Vector3 capsuleP1, float capsuleRadius)
	{
		float num = capsuleRadius + particleRadius;
		float num2 = num * num;
		Vector3 vector = capsuleP1 - capsuleP0;
		Vector3 vector2 = particlePosition - capsuleP0;
		float num3 = Vector3.Dot(vector2, vector);
		if (num3 <= 0f)
		{
			float sqrMagnitude = vector2.sqrMagnitude;
			if (sqrMagnitude > 0f && sqrMagnitude < num2)
			{
				float num4 = Mathf.Sqrt(sqrMagnitude);
				particlePosition = capsuleP0 + vector2 * (num / num4);
			}
		}
		else
		{
			float sqrMagnitude2 = vector.sqrMagnitude;
			if (num3 >= sqrMagnitude2)
			{
				vector2 = particlePosition - capsuleP1;
				float sqrMagnitude3 = vector2.sqrMagnitude;
				if (sqrMagnitude3 > 0f && sqrMagnitude3 < num2)
				{
					float num5 = Mathf.Sqrt(sqrMagnitude3);
					particlePosition = capsuleP1 + vector2 * (num / num5);
				}
			}
			else if (sqrMagnitude2 > 0f)
			{
				num3 /= sqrMagnitude2;
				vector2 -= vector * num3;
				float sqrMagnitude4 = vector2.sqrMagnitude;
				if (sqrMagnitude4 > 0f && sqrMagnitude4 < num2)
				{
					float num6 = Mathf.Sqrt(sqrMagnitude4);
					particlePosition += vector2 * ((num - num6) / num6);
				}
			}
		}
	}

	private static void InsideCapsule(ref Vector3 particlePosition, float particleRadius, Vector3 capsuleP0, Vector3 capsuleP1, float capsuleRadius)
	{
		float num = capsuleRadius - particleRadius;
		float num2 = num * num;
		Vector3 vector = capsuleP1 - capsuleP0;
		Vector3 vector2 = particlePosition - capsuleP0;
		float num3 = Vector3.Dot(vector2, vector);
		if (num3 <= 0f)
		{
			float sqrMagnitude = vector2.sqrMagnitude;
			if (sqrMagnitude > num2)
			{
				float num4 = Mathf.Sqrt(sqrMagnitude);
				particlePosition = capsuleP0 + vector2 * (num / num4);
			}
		}
		else
		{
			float sqrMagnitude2 = vector.sqrMagnitude;
			if (num3 >= sqrMagnitude2)
			{
				vector2 = particlePosition - capsuleP1;
				float sqrMagnitude3 = vector2.sqrMagnitude;
				if (sqrMagnitude3 > num2)
				{
					float num5 = Mathf.Sqrt(sqrMagnitude3);
					particlePosition = capsuleP1 + vector2 * (num / num5);
				}
			}
			else if (sqrMagnitude2 > 0f)
			{
				num3 /= sqrMagnitude2;
				vector2 -= vector * num3;
				float sqrMagnitude4 = vector2.sqrMagnitude;
				if (sqrMagnitude4 > num2)
				{
					float num6 = Mathf.Sqrt(sqrMagnitude4);
					particlePosition += vector2 * ((num - num6) / num6);
				}
			}
		}
	}

	[Tooltip("The radius of the sphere or capsule.")]
	public float m_Radius = 0.5f;

	[Tooltip("The height of the capsule.")]
	public float m_Height;

	public float m_fScaleRateMulMax = 1f;

	public Vector3 m_CenterRateMax = Vector3.zero;

	public Maid m_maid;
}
