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

public class THair1
{
	public THair1(TBoneHair_ bonehair_, Transform t, Transform root_oya_, TBodyHit bh, THair1 p1 = null, THair1 p2 = null)
	{
		this.boDBL = (p1 != null);
		if (this.boDBL)
		{
			this.boDBL = true;
			this.pair = p1;
			this.pair2 = p2;
		}
		this.BodyHit = bh;
		this.bonehair = bonehair_;
		string text = t.name.ToLower();
		if (text.Contains("skirt"))
		{
			for (int i = 1; i <= 24; i++)
			{
				if (t.name.Contains(i.ToString("D2")))
				{
					this.SkirtIDX = i;
					break;
				}
			}
		}
		if (text.Contains("_h_"))
		{
			this.boHard = true;
		}
		this.root = t;
		this.root_oya = root_oya_;
		this.root_oya_eulerAngles = new Vector3(this.root_oya.eulerAngles.x, this.root_oya.eulerAngles.y, this.root_oya.eulerAngles.z);
		this.SoftG = new Vector3(0f, -0.003f, 0f);
		this.pair = null;
		this.hplist = new List<THp>();
		THp thp = new THp();
		thp.t = this.root_oya;
		THp prevSeg = thp;
		thp.PrevSeg = null;
		thp.defQ = thp.t.localRotation;
		thp.defLclPos_child = t.localPosition;
		thp.v = t.position;
		this.hplist.Add(thp);
		Vector3 b = t.position;
		if (p1 != null)
		{
			b = (p1.hplist[0].v + p2.hplist[0].v) * 0.5f;
		}
		Transform t_oya = this.root_oya;
		for (int j = 0; j < 30; j++)
		{
			if (t.childCount == 0)
			{
				break;
			}
			Transform child = t.GetChild(0);
			if (child == null)
			{
				break;
			}
			for (int k = 1; k <= this.HairDiv; k++)
			{
				THp thp2 = new THp();
				thp2.v = (child.position * (float)k + t.position * (float)(this.HairDiv - k)) / (float)this.HairDiv;
				if (p1 != null)
				{
					thp2.v = (p1.hplist[k].v + p1.hplist[k - 1].v + p2.hplist[k].v + p2.hplist[k - 1].v) * 0.25f;
				}
				if (bh.SphereCheck(thp2.v))
				{
					thp2.boUmari = true;
				}
				thp2.defLclPos_child = child.localPosition * (float)k / (float)this.HairDiv;
				Vector3 vector = t.TransformPoint(thp2.defLclPos_child);
				vector.y = 0f;
				thp2.vone = vector.normalized;
				thp2.mae = Mathf.Clamp(thp2.vone.z * 0.7f + 0.5f, 0f, 1f);
				thp2.WideCircleLen = (thp2.v - this.hplist[0].t.position).magnitude;
				thp2.len = (thp2.v - b).magnitude;
				thp2.lenxlen = thp2.len * thp2.len;
				thp2.SegLength = (t.position - thp2.v).magnitude;
				if (thp2.SegLength <= 0f)
				{
					Debug.LogError("hp seg len==0");
				}
				thp2.t = t;
				thp2.t_oya = t_oya;
				thp2.defQ = t.localRotation;
				thp2.invQ = Quaternion.Inverse(t.localRotation);
				thp2.vec_up = thp2.t.up;
				thp2.rotRev = 90f;
				if (t.transform.right.y < 0f)
				{
					this.boXREV = true;
				}
				thp2.PrevSeg = prevSeg;
				if (k % this.HairDiv == 0)
				{
					prevSeg = thp2;
				}
				this.hplist.Add(thp2);
				b = thp2.v;
			}
			t_oya = t;
			t = child;
		}
		if (this.boDBL)
		{
			this.SoftG = p1.SoftG;
			for (int l = 1; l < this.hplist.Count; l++)
			{
				this.hplist[l].SoftPer = p1.hplist[l].SoftPer;
				this.hplist[l].SoftTgtG = p1.hplist[l].SoftTgtG;
			}
		}
		this.pointCount = this.hplist.Count / this.HairDiv;
	}

	public void SetPair(THair1 tp, THair1 tp2)
	{
		this.pair = tp;
		this.pair2 = tp2;
		for (int i = 0; i < this.hplist.Count; i++)
		{
			this.hplist[i].PairLength = (this.hplist[i].v - tp.hplist[i].v).magnitude * 0.5f;
		}
	}

	public void CheckPair(int i)
	{
		if (this.pair == null)
		{
			Debug.LogError("ERR CheckPair " + i.ToString());
		}
		if (this.pair2 == null)
		{
			Debug.LogError("ERR CheckPair2 " + i.ToString());
		}
	}

	public void SetUpDefTgt(bool boInit = false)
	{
		this.HitHeightY = this.bonehair.body.BoneHitHeightY + 0.03f;
		this.boWarpInitMode = boInit;
		if (this.hplist.Count < 2)
		{
			return;
		}
		for (int i = 0; i < this.hplist.Count; i++)
		{
			this.hplist[i].v_old = this.hplist[i].v;
		}
		for (int j = 1; j < this.hplist.Count; j++)
		{
			THp thp = this.hplist[j];
			if (this.pair != null)
			{
				THp thp2 = this.pair.hplist[j];
				thp.MST = (thp.v - thp2.v).sqrMagnitude / thp.PairLength * 2f;
			}
		}
		if (this.boDBL)
		{
			for (int k = 1; k < this.hplist.Count; k++)
			{
				THp thp3 = this.hplist[k];
				thp3.vTGT = (this.pair.hplist[k].vTGT + this.pair2.hplist[k].vTGT + this.pair.hplist[k - 1].vTGT + this.pair2.hplist[k - 1].vTGT) * 0.25f;
				if (this.boDebugLine)
				{
					Debug.DrawLine(thp3.vTGT, thp3.vTGT - Vector3.right * 0.005f, Color.yellow);
				}
			}
			this.hplist[0].vTGT = (this.pair.hplist[0].vTGT + this.pair2.hplist[0].vTGT) * 0.5f;
			this.hplist[0].v = this.hplist[0].vTGT;
			if (boInit)
			{
				for (int l = 0; l < this.hplist.Count; l++)
				{
					this.hplist[l].v = this.hplist[l].vTGT;
					this.hplist[l].v_old = this.hplist[l].vTGT;
					this.hplist[l].g = Vector3.zero;
				}
			}
			return;
		}
		Vector3 a = this.hplist[1].t.position;
		Quaternion quaternion = this.root_oya.rotation;
		for (int m = 1; m < this.hplist.Count; m++)
		{
			THp thp4 = this.hplist[m];
			if (m % this.HairDiv == 1)
			{
				quaternion *= thp4.defQ;
				if (this.boXREV)
				{
					quaternion *= Quaternion.Euler(0f, 180f, 0f);
				}
			}
			Vector3 vector = a + quaternion * thp4.defLclPos_child;
			if (m % this.HairDiv == 0)
			{
				a = vector;
			}
			if (this.boDebugLine)
			{
				Debug.DrawLine(vector, vector - Vector3.right * 0.005f, Color.red);
			}
			float num = (float)m * 0.05f + 0.5f;
			if (num > 1f)
			{
			}
			thp4.vTGT = vector;
		}
		this.hplist[0].vTGT = this.root.position;
		this.hplist[0].v = this.root.position;
		if (boInit)
		{
			for (int n = 0; n < this.hplist.Count; n++)
			{
				this.hplist[n].v = this.hplist[n].vTGT;
				this.hplist[n].v_old = this.hplist[n].vTGT;
				this.hplist[n].g = Vector3.zero;
			}
		}
	}

	public void Calc(TBodyHit bh)
	{
		if (this.hplist.Count < 2)
		{
			return;
		}
		if (this.bonehair.trsKusari[this.KusariIDX] != null)
		{
			if (this.bonehair.trsKusari[this.KusariIDX] != null)
			{
				this.hplist[this.hplist.Count - 1].v = this.bonehair.trsKusari[this.KusariIDX].position;
			}
			for (int i = 1; i < this.hplist.Count - 1; i++)
			{
				THp thp = this.hplist[i];
				thp.g += new Vector3(0f, -0.001f, 0f);
				thp.g *= 0.99f;
				thp.v += thp.g;
			}
			for (int j = this.hplist.Count - 1; j >= 2; j--)
			{
				THp thp2 = this.hplist[j];
				Vector3 a = this.hplist[j - 1].v - thp2.v;
				if (a.sqrMagnitude > thp2.lenxlen)
				{
					float magnitude = a.magnitude;
					Vector3 b = a / magnitude * thp2.len;
					this.hplist[j - 1].v = thp2.v + b;
					this.hplist[j - 1].g -= a * 0.01f;
				}
				if (this.bonehair.bodyskin.m_bHitFloorY && thp2.v.y < this.HitHeightY)
				{
					thp2.v.y = this.HitHeightY;
					thp2.g *= 0.9f;
				}
			}
			for (int k = 1; k < this.hplist.Count; k++)
			{
				THp thp3 = this.hplist[k];
				Vector3 a2 = thp3.v - this.hplist[k - 1].v;
				if (a2.sqrMagnitude > thp3.lenxlen)
				{
					float magnitude2 = a2.magnitude;
					Vector3 b2 = a2 / magnitude2 * thp3.len;
					thp3.v = this.hplist[k - 1].v + b2;
					thp3.g -= a2 * 0.01f;
				}
				thp3.Secchi = bh.SphereMove_hair2(ref thp3.v, ref thp3.g, thp3.v_old);
				if (this.bonehair.bodyskin.m_bHitFloorY && thp3.v.y < this.HitHeightY)
				{
					thp3.v.y = this.HitHeightY;
					thp3.g *= 0.9f;
				}
			}
			return;
		}
		for (int l = this.hplist.Count - 1; l >= 1; l--)
		{
			THp thp4 = this.hplist[l];
			float softPer = thp4.SoftPer;
			Vector3 a3 = (thp4.vTGT - this.hplist[l - 1].vTGT) * thp4.SoftTgtG;
			thp4.g += this.SoftG * softPer + a3 * (1f - softPer);
			thp4.g *= 0.99f;
			Vector3 vector = this.hplist[l - 1].v - thp4.v;
			thp4.v += thp4.g;
			if ((this.bonehair.bodyskin.SlotId != TBody.SlotID.hairR && this.bonehair.bodyskin.SlotId != TBody.SlotID.hairT) || this.bonehair.body.Mode_HairR == TBody.ModeHirR.NoFront)
			{
			}
		}
		for (int m = 1; m < this.hplist.Count; m++)
		{
			THp thp5 = this.hplist[m];
			Vector3 a4 = thp5.v - this.hplist[m - 1].v;
			if (a4.sqrMagnitude > thp5.lenxlen)
			{
				float magnitude3 = a4.magnitude;
				Vector3 vector2 = this.hplist[m - 1].v + a4 / magnitude3 * thp5.len;
				if (!thp5.Secchi)
				{
					thp5.g += (vector2 - thp5.v) * 0.05f;
				}
				thp5.v = vector2;
			}
			thp5.Secchi = bh.SphereMove_hair2(ref thp5.v, ref thp5.g, thp5.v_old);
			if (this.bonehair.bodyskin.m_bHitFloorY && thp5.v.y < this.HitHeightY)
			{
				thp5.v.y = this.HitHeightY;
				thp5.g *= 0.9f;
			}
		}
	}

	public void Calc_skirt2(int i)
	{
		THp thp = this.hplist[i];
		this.hplist[i].PairLength = (this.hplist[i].vTGT - this.pair.hplist[i].vTGT).magnitude * 0.5f;
		float softPer = thp.SoftPer;
		Vector3 a = (thp.vTGT - this.hplist[i - 1].vTGT) * thp.SoftTgtG;
		thp.g += this.SoftG * softPer + a * (1f - softPer);
		float d = (float)((this.hplist.Count - i) / this.HairDiv);
		thp.g += (this.hplist[0].v - this.hplist[0].v_old) * 0.2f * d;
		thp.g *= 0.999f;
		thp.v += thp.g;
	}

	public void Calc_skirt_WideCircle(int i)
	{
		THp thp = this.hplist[i];
		Vector3 a = thp.v - this.hplist[0].v;
		float num = thp.WideCircleLen * 0.5f;
		if (a.sqrMagnitude < num * num)
		{
			float magnitude = a.magnitude;
			Vector3 v = this.hplist[0].v + a / magnitude * num;
			thp.v = v;
		}
		thp.Secchi = this.BodyHit.SphereMove_skrt(ref thp.v, ref thp.g);
	}

	public void Calc_skirt_up(int i)
	{
		THp thp = this.hplist[i];
		Vector3 a = thp.v - this.hplist[i - 1].v;
		if (a.sqrMagnitude > thp.lenxlen)
		{
			float magnitude = a.magnitude;
			Vector3 vector = this.hplist[i - 1].v + a / magnitude * thp.len;
			thp.g += (vector - thp.v) * 0.5f;
			thp.v = vector;
		}
		thp.Secchi = this.BodyHit.SphereMove_skrt(ref thp.v, ref thp.g);
	}

	public void Calc_skirt_pair(int i)
	{
		THp thp = this.hplist[i];
		THp thp2 = this.pair.hplist[i];
		THp thp3 = this.pair2.hplist[i];
		float num = 0.5f;
		float num2 = thp3.MST + thp2.MST;
		if (num2 != 0f)
		{
			num = thp3.MST / num2;
		}
		Vector3 a = thp2.v - thp.v;
		Vector3 a2 = thp.v * num + thp2.v * (1f - num);
		float magnitude = a.magnitude;
		if (magnitude < thp.PairLength)
		{
			return;
		}
		a /= magnitude;
		a.Normalize();
		this.BodyHit.SphereMove_skrt(ref a2, ref thp.g);
		if (this.boDebugLine && this.BodyHit.MST > 0f)
		{
			Debug.DrawLine(thp.v, thp.v + this.BodyHit.MST_v * 10f, Color.white);
		}
		Vector3 a3 = a2 + a * num * this.hplist[i].PairLength;
		Vector3 a4 = a2 - a * (1f - num) * this.hplist[i].PairLength;
		float num3 = 0.5f;
		thp2.v = thp2.v * (1f - num3) + a3 * num3;
		thp.v = thp.v * (1f - num3) + a4 * num3;
	}

	public void Proc(TBodyHit bh)
	{
		if (this.hplist.Count < 2)
		{
			return;
		}
		if (this.boDebugLine)
		{
			for (int i = 0; i < this.hplist.Count; i++)
			{
				THp thp = this.hplist[i];
				Color color = new Color(1f, 1f - thp.mae, thp.mae);
				color = Color.red;
				if (this.boDBL)
				{
					color = Color.yellow;
				}
				if (i > 0)
				{
					Debug.DrawLine(thp.v, this.hplist[i - 1].v, color);
				}
				color = Color.white;
				if (thp.Secchi)
				{
					color = Color.yellow;
				}
			}
		}
		if (this.boDBL)
		{
			return;
		}
		float num = 1f;
		Transform transform = this.root;
		int num2 = this.hplist.Count / this.HairDiv;
		for (int j = 0; j < num2; j++)
		{
			if (transform.childCount == 0)
			{
				break;
			}
			Transform child = transform.GetChild(0);
			if (child == null)
			{
				break;
			}
			Quaternion quaternion = default(Quaternion);
			Vector3 vector = this.hplist[j * this.HairDiv + this.HairDiv].v - this.hplist[j * this.HairDiv].v;
			float magnitude = vector.magnitude;
			if (magnitude != 0f)
			{
				float num3 = magnitude / this.hplist[j * this.HairDiv + this.HairDiv].SegLength;
				num = num3 / num;
				if (num <= 0f)
				{
					Debug.LogError("scl<=0");
				}
				if (num < 0.1f)
				{
					num = 0.1f;
				}
				if (num > 10f)
				{
					num = 10f;
				}
				float num4 = 1f;
				transform.localScale = new Vector3(num * num4, num * num4, num * num4);
				Vector3 vec_up = this.hplist[j * this.HairDiv + 1].vec_up;
				if (vector != Vector3.zero)
				{
					quaternion.SetLookRotation(vector, vec_up);
				}
				quaternion *= Quaternion.Euler(0f, this.hplist[j * this.HairDiv + 1].rotRev, 0f);
				Vector3 eulerAngles = this.root_oya.eulerAngles;
				if (this.pointCount > 1)
				{
					quaternion *= Quaternion.Euler(eulerAngles.y + bh.RotOffset, 0f, 0f);
				}
				transform.rotation = quaternion;
				transform.localRotation = this.hplist[j * this.HairDiv + 1].defQ;
				Vector3 fromDirection = child.position - transform.position;
				Vector3 toDirection = this.hplist[j * this.HairDiv + this.HairDiv].v - this.hplist[j * this.HairDiv].v;
				transform.rotation = Quaternion.FromToRotation(fromDirection, toDirection) * transform.rotation;
				transform = child;
			}
		}
	}

	public bool boWarpInitMode;

	public TBoneHair_ bonehair;

	public TBodyHit BodyHit;

	public bool boDebugLine;

	public HairMode Mode = HairMode.hair;

	public Vector3 SoftG;

	public bool boXREV;

	public List<THp> hplist;

	public int pointCount;

	public Transform root;

	public Transform root_oya;

	private THair1 pair;

	private THair1 pair2;

	public int HairDiv = 2;

	public Vector3 root_oya_eulerAngles;

	public Vector3 Calf_L_pos_old;

	public Vector3 Calf_R_pos_old;

	public Vector3 Thigh_L_pos_old;

	public Vector3 Thigh_R_pos_old;

	public int KusariIDX;

	public int SkirtIDX;

	public bool boHard;

	public float HitHeightY;

	public bool boDBL;
}
