using System;
using NBF;
using Obi;
using Unity.VisualScripting;
using UnityEngine;

public class FLineHandler : MonoBehaviour
{
    public enum LineType
    {
        None = 0,
        OneSegment = 1,
        TwoSegment = 2,
        ThereSegment = 3
    }

    public LineType lineType = LineType.TwoSegment;

    public ObiRope obiRopeSegment_1;

    public ObiRope obiRopeSegment_2;

    public ObiRope obiRopeSegment_3;
    
    public FixedLine LineConnector_0;

    public SpringJoint LineConnector_1;

    public SpringJoint LineConnector_2;

    public SpringJoint LineConnector_3;

    // [HideInInspector] public FFishingLine currentRodFishingLineComponent;

    // public ObiParticleAttachment toRodConnector;

    // public float PhisicsLineOut { get; set; }

    public float ObiLineOut;

    [HideInInspector] public Rigidbody EndLineRigidbody_0;

    [HideInInspector] public Rigidbody EndLineRigidbody_1;

    [HideInInspector] public Rigidbody EndLineRigidbody_2;

    [HideInInspector] public Rigidbody EndLineRigidbody_3;

    public JointPinchController pinchController;

    public FRod Rod;

    private Transform waterPlane;


    // public float ropeToHookDistance;

    void Start()
    {
        ObiLineOut = obiRopeSegment_1.stretchingScale;
        if ((bool)LineConnector_0)
        {
            EndLineRigidbody_0 = LineConnector_0.GetComponent<Rigidbody>();
        }

        if ((bool)LineConnector_1)
        {
            EndLineRigidbody_1 = LineConnector_1.GetComponent<Rigidbody>();
        }

        if ((bool)LineConnector_2)
        {
            EndLineRigidbody_2 = LineConnector_2.GetComponent<Rigidbody>();
            // var fixedJoint = LineConnector_2.GetComponent<FixedJoint>();
            pinchController = LineConnector_2.gameObject.AddComponent<JointPinchController>();
        }

        if ((bool)LineConnector_3)
        {
            EndLineRigidbody_3 = LineConnector_3.GetComponent<Rigidbody>();
        }

        waterPlane = GameObject.FindGameObjectWithTag("Water").transform;

        Debug.LogError($"rope.restLength={obiRopeSegment_1.restLength} LineConnector_1={LineConnector_1.maxDistance}");
    }


    void Update()
    {
        if (!Rod) return;


        // ropeToHookDistance = Vector3.Distance(toRodConnector.transform.position, LineConnector_1.transform.position);

        ObiLineOut = 0.1f + Rod.Owner.Data.lineLength;
        float target = (0f - Mathf.Clamp(Rod.linelenghtDiferent, -1f, 0f)) * 0.1f;
        if (Rod.linelenghtDiferent >= 0f)
        {
            obiRopeSegment_1.stretchCompliance = Mathf.MoveTowards(obiRopeSegment_1.stretchCompliance, target,
                Time.smoothDeltaTime * (1f * Rod.linelenghtDiferent));
        }
        else
        {
            obiRopeSegment_1.stretchCompliance = Mathf.MoveTowards(obiRopeSegment_1.stretchCompliance, target,
                Time.smoothDeltaTime * 0.1f);
        }

        if (Rod.Owner.Data.lineLength == 0f)
        {
            obiRopeSegment_1.stretchCompliance = 0f;
        }

        if ((bool)obiRopeSegment_2)
        {
            if (!Rod.currentFish)
            {
                obiRopeSegment_2.stretchCompliance = obiRopeSegment_2.stretchingScale * 0.004f;
            }
            else
            {
                obiRopeSegment_2.stretchCompliance = 0f;
            }

            //TODO:TEST
            obiRopeSegment_2.stretchingScale = 0.13F;
        }

        obiRopeSegment_1.stretchingScale = ObiLineOut;
        obiRopeSegment_1.stretchingScale = 1;
        LineConnector_1.maxDistance = 0.1f + Rod.Owner.Data.lineLength;
        if (Input.GetKey(KeyCode.E))
        {
            // var speed = 1;
            // obiRopeCursor_1.ChangeLength(LineConnector_1.maxDistance);
            // Debug.Log(obiRopeSegment_1.restLength);
        }

        // var addLength = LineConnector_1.maxDistance - obiRopeSegment_1.restLength;
        // if (Mathf.Abs(addLength) > 0.001f)
        // {
        //     obiRopeCursor_1.ChangeLength(LineConnector_1.maxDistance);
        // }

        // if (!Mathf.Approximately(LineConnector_1.maxDistance, obiRopeSegment_1.restLength))
        // {
        //     obiRopeCursor_1.ChangeLength(LineConnector_1.maxDistance);
        // }

        // obiRopeCursor_1.pos


        // LineConnector_1.minDistance = LineConnector_1.maxDistance;
    }

    public void SetSegmentTwoLenght(float lenght)
    {
        LineConnector_2.maxDistance = lenght;
        // obiRopeCursor_2.ChangeLength(lenght);
        // LineConnector_2.minDistance = LineConnector_2.maxDistance;
    }

    private void FixedUpdate()
    {
        // BindRod();
        LineWaterDisplacement();
    }

    private void BindRod()
    {
        if (!Rod || !Rod.rodAsset) return;
        LineConnector_0.transform.position = Rod.rodAsset.lineConnector.position;
    }

    private void LineWaterDisplacement()
    {
        if (!waterPlane)
        {
            return;
        }

        for (int i = 0; i < obiRopeSegment_1.activeParticleCount; i++)
        {
            if (obiRopeSegment_1.GetParticlePosition(i).y < waterPlane.position.y)
            {
                // obiRopeSegment_1.AddForceParticle(i, Vector3.up * 10f, ForceMode.Acceleration);
            }
        }
    }
}