using System;
using UnityEngine;

// Token: 0x0200006D RID: 109
public class Osage : MonoBehaviour
{
    // Token: 0x06000295 RID: 661 RVA: 0x00014858 File Offset: 0x00012A58
    private void Initialize()
    {
        if (this.trs != null)
        {
            return;
        }
        this.trs = base.transform;
        this.localRotation = this.trs.localRotation;
        this.initialLocalPos = this.trs.localPosition;
        this.scale = this.trs.localScale.x;
        Transform parent = this.trs.parent;
        while (parent != null)
        {
            this.scale *= parent.localScale.x;
            parent = parent.parent;
        }
    }

    // Token: 0x06000296 RID: 662 RVA: 0x000148F0 File Offset: 0x00012AF0
    private void Awake()
    {
        this.Initialize();
        this.childBone = null;
        foreach (object obj in base.transform)
        {
            Transform transform = (Transform)obj;
            if (transform.name.IndexOf("j_osg") == 0)
            {
                this.childBone = transform;
                break;
            }
        }
        this.parentBone = ((base.transform.parent == null || base.transform.parent.GetComponent<Osage>() == null) ? null : base.transform.parent);
        this.reset = 0;
    }

    // Token: 0x06000297 RID: 663 RVA: 0x000149B0 File Offset: 0x00012BB0
    private void Start()
    {
        if (this.trs == null)
        {
            return;
        }
        if (this.childBone == null)
        {
            this.springLength = this.boneAxis.magnitude * this.scale;
            this.crntPos = this.trs.TransformPoint(this.boneAxis);
        }
        else
        {
            this.springLength = Vector3.Distance(this.trs.position, this.childBone.position);
            this.crntPos = this.childBone.position;
        }
        this.prevPos = this.crntPos;
        this.oldPos = new Vector3(0f, -9999f, 0f);
    }

    // Token: 0x06000298 RID: 664 RVA: 0x00014A64 File Offset: 0x00012C64
    private void UpdateSpring(bool rst, Transform thighL, Transform thighR, bool mov)
    {
        if (!enabled) return;
        if (mov)
        {
            this.trs.localRotation = Quaternion.identity * this.localRotation;
            Vector3 fromDirection = this.trs.TransformDirection(this.boneAxis);
            if (rst)
            {
                this.crntPos = fromDirection.normalized * this.springLength + this.trs.position;
                this.prevPos = this.crntPos;
            }
            this.oldPos = this.trs.position;
            Vector3 a = this.trs.rotation * (this.boneAxis * this.stiffnessForce);
            a += (this.prevPos - this.crntPos) / this.scale * this.dragForce;
            a += this.springForce;
            Vector3 vector = this.crntPos;
            this.crntPos = this.crntPos - this.prevPos + this.crntPos + a * this.scale;
            this.crntPos = (this.crntPos - this.trs.position).normalized * this.springLength + this.trs.position;
            this.prevPos = vector;
            Quaternion lhs = Quaternion.FromToRotation(fromDirection, this.crntPos - this.trs.position);
            this.trs.rotation = lhs * this.trs.rotation;
            if (thighL != null && thighR != null && base.name == "j_osg_apron_a")
            {
                float num = 90f - thighL.eulerAngles.z;
                if (num < 0f)
                {
                    num = 0f;
                }
                float num2 = 90f - thighR.eulerAngles.z;
                if (num2 < 0f)
                {
                    num2 = 0f;
                }
                float num3 = this.localRotation.eulerAngles.x - (num + num2) * 0.5f;
                Vector3 eulerAngles = this.trs.eulerAngles;
                if (eulerAngles.x > num3)
                {
                    eulerAngles.x = num3;
                }
                if ((num3 -= eulerAngles.x) < 0f)
                {
                    num3 = 0f;
                }
                eulerAngles.y -= eulerAngles.z;
                eulerAngles.z = (num2 - num) * 0.5f;
                if (eulerAngles.z < 0f)
                {
                    eulerAngles.z += num3;
                }
                else
                {
                    eulerAngles.z -= num3;
                }
                this.trs.eulerAngles = eulerAngles;
            }
        }
        else
        {
            this.prevPos = this.crntPos;
            this.crntPos = this.trs.TransformDirection(this.boneAxis).normalized * this.springLength + this.trs.position;
            this.oldPos = this.trs.position;
        }
        Osage osage = (this.childBone == null) ? null : this.childBone.GetComponent<Osage>();
        if (osage != null)
        {
            osage.UpdateSpring(rst, null, null, mov);
        }
    }

    // Token: 0x06000299 RID: 665 RVA: 0x00014DBC File Offset: 0x00012FBC
    public void UpdateOsage()
    {
        if (!enabled) return;
        if (this.trs == null) return;
        if (this.parentBone != null) return;
        /*
		if (TimeManager.Pause)
		{
			return;
		}
		*/
        this.trs.localRotation = Quaternion.identity * this.localRotation;
    }

    // Token: 0x0600029A RID: 666 RVA: 0x00014E0C File Offset: 0x0001300C
    public void LateUpdateOsage(Transform thighL, Transform thighR)
    {
        if (!enabled) return;
        if (this.trs == null) return;
        if (this.parentBone != null) return;
        /*
		if (TimeManager.Pause)
		{
			return;
		}
		*/
        if (Vector3.Distance(this.oldPos, this.trs.position) / this.scale > 0.1f && this.reset < 2)
        {
            this.reset = 2;
        }
        bool mov = false;
        Quaternion lhs = this.trs.localRotation;
        this.trs.localRotation = Quaternion.identity * this.localRotation;
        if (lhs == this.trs.localRotation)
        {
            mov = true;
        }
        else
        {
            this.trs.localRotation = lhs;
        }
        this.UpdateSpring(this.reset > 0, thighL, thighR, mov);
        if (this.reset > 0)
        {
            this.reset--;
        }
    }

    // Token: 0x0600029B RID: 667 RVA: 0x00014EE8 File Offset: 0x000130E8
    public void Reset()
    {
        if (this.reset < 3)
        {
            this.reset = 3;
        }
    }

    // Token: 0x17000073 RID: 115
    // (get) Token: 0x0600029C RID: 668 RVA: 0x00014EFA File Offset: 0x000130FA
    public Vector3 InitialLocalPos
    {
        get
        {
            return this.initialLocalPos;
        }
    }

    // Token: 0x17000074 RID: 116
    // (get) Token: 0x0600029D RID: 669 RVA: 0x00014F02 File Offset: 0x00013102
    public Quaternion LocalRotation
    {
        get
        {
            return this.localRotation;
        }
    }

    // Token: 0x0600029E RID: 670 RVA: 0x00014F0C File Offset: 0x0001310C
    public void ResetForce(Osage inSrc = null)
    {
        this.Initialize();
        if (inSrc != null)
        {
            inSrc.Initialize();
            this.initialLocalPos = inSrc.InitialLocalPos;
            this.localRotation = inSrc.LocalRotation;
        }
        this.trs.localPosition = this.initialLocalPos;
        this.trs.localRotation = this.localRotation;
    }

    // Token: 0x0400040D RID: 1037
    public Vector3 boneAxis = new Vector3(0f, 0f, -1f);

    // Token: 0x0400040E RID: 1038
    public float stiffnessForce = 0.1f;

    // Token: 0x0400040F RID: 1039
    public float dragForce = 0.01f;

    // Token: 0x04000410 RID: 1040
    public Vector3 springForce = new Vector3(0f, -0.001f, 0f);

    // Token: 0x04000411 RID: 1041
    private Transform childBone;

    // Token: 0x04000412 RID: 1042
    private Transform parentBone;

    // Token: 0x04000413 RID: 1043
    private float springLength;

    // Token: 0x04000414 RID: 1044
    private Quaternion localRotation;

    // Token: 0x04000415 RID: 1045
    private Transform trs;

    // Token: 0x04000416 RID: 1046
    private Vector3 crntPos;

    // Token: 0x04000417 RID: 1047
    private Vector3 prevPos;

    // Token: 0x04000418 RID: 1048
    private Vector3 oldPos;

    // Token: 0x04000419 RID: 1049
    private int reset;

    // Token: 0x0400041A RID: 1050
    private Vector3 initialLocalPos;

    // Token: 0x0400041B RID: 1051
    private float scale;
}