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

[CreateAssetMenu(fileName = "Physic Settings", menuName = "Physic Settings")]
public class PhysicSettings : ScriptableObject
{
    [Range(0f,1f)]
    public float dampingRatio;
    [Range(0f, 100000f)]
    public float frequency;

    public float levelingForce;

    public bool freezeRotation = false;

    public void Apply(Transform player) {
        foreach (SpringJoint2D joint in player.GetComponentsInChildren<SpringJoint2D>()) {
            joint.dampingRatio = dampingRatio;
            joint.frequency = frequency;

            joint.attachedRigidbody.freezeRotation = freezeRotation;
        }

        RelativeJoint2D relativeJoint = player.GetComponent<RelativeJoint2D>();
        Rigidbody2D rb = player.GetComponent<Rigidbody2D>();
        //Debug.Log($"rb rotation: {rb.rotation}");
        //if (Mathf.Abs(rb.rotation) > 180f) {
        //    relativejoint.angularoffset = rb.
        //}
        relativeJoint.maxTorque = levelingForce;

        float rot = rb.rotation;
        rot %= 360;
        rot = rot > 180 ? rot - 360 : rot;
        rb.AddTorque(levelingForce * Time.deltaTime * -Mathf.Sign(rot));
    }
}
