using NBF;
using UnityEngine;

public class FLure : FPlayerGear
{
    public enum MoveType
    {
        None = 0,
        PowolnyWleczony = 1,
        Wleczony = 2,
        Opadający = 3,
        PowolnyOpadający = 4
    }


    [HideInInspector] public Rigidbody rigidbody;

    [HideInInspector] public FWaterDisplacement waterDisplacement;

    public Rigidbody fishJoiner;

    public bool isLookingDisable;

    // private Animator animator;

    [HideInInspector] public FFish currentFish;
    
    [HideInInspector] public float percentEfficacy;

    public MoveType LureMoveType = MoveType.Wleczony;

    [HideInInspector] public MoveType currentMoveType;

    private MoveType newMoveType;

    private float delayMoveTypeTimer;

    private float moveTowardsFactor;

    private float moveUpperFactor;

    private float waterHeightPosition;

    private Transform currentWaterDrop;

    private float rotateVelo;

    private bool destroyDropFx;

    public LureAsset lureAsset;

    private void Awake()
    {
        lureAsset = GetComponent<LureAsset>();
    }

    protected override void OnStart()
    {
        rigidbody = GetComponent<Rigidbody>();
        waterDisplacement = GetComponent<FWaterDisplacement>();
        // animator = GetComponent<Animator>();
        waterHeightPosition = SceneSettings.Instance.WaterObject.position.y;
        if (!fishJoiner)
        {
            return;
        }

        FixedJoint component = fishJoiner.GetComponent<FixedJoint>();
        if ((bool)component && !component.connectedBody)
        {
            if ((bool)component.transform.parent.GetComponent<Rigidbody>())
            {
                component.connectedBody = component.transform.parent.GetComponent<Rigidbody>();
            }
            else
            {
                component.connectedBody = transform.GetComponent<Rigidbody>();
            }
        }
    }

    protected override void OnUpdate()
    {
        // transform.position = Rod.lineHandler.LineConnector_1.transform.position;
        RotationToVelocity();
        // ShowWaterFX();
        MoveTypeController();
        if ((bool)currentFish)
        {
            waterDisplacement.isFreeze = true;
            if (fishJoiner) fishJoiner.gameObject.SetActive(true);
            if (!currentFish.isGetFish && transform.parent == currentFish.baitContainer)
            {
                transform.localPosition = Vector3.zero;
                transform.localEulerAngles = lureAsset.rotationInFishJaw;
            }
        }
        else
        {
            waterDisplacement.isFreeze = false;
            if (fishJoiner) fishJoiner.gameObject.SetActive(false);
        }

        if (!Rod.takeFish && !Rod.currentFish && isLookingDisable)
        {
            isLookingDisable = false;
        }

        // if ((bool)animator && transform.position.y < 0.2f && animator.runtimeAnimatorController != null)
        // {
        //     animator.SetFloat("Speed", rigidbody.linearVelocity.magnitude);
        // }

        // CheckDistance(Rod.lineHandler.LineConnector_1.transform);
    }

    private void RotationToVelocity()
    {
        if (!waterDisplacement)
        {
            return;
        }

        if (transform.position.y > 0.02f)
        {
            // if (transform.position.y > 0.2f && (bool)animator)
            // {
            //     animator.SetFloat("Speed", 0f);
            // }

            rigidbody.freezeRotation = false;
        }
        else
        {
            if (Rod.linelenghtDiferent <= 0f)
            {
                return;
            }

            rigidbody.freezeRotation = true;
            Vector3 vector = new Vector3(rigidbody.linearVelocity.x, 0f, rigidbody.linearVelocity.z);
            if (vector != Vector3.zero)
            {
                Quaternion b = Quaternion.LookRotation(vector, Vector3.up);
                transform.rotation = Quaternion.Slerp(transform.rotation, b,
                    rigidbody.linearVelocity.magnitude * 5f * Time.deltaTime);
            }

            if (lureAsset.rotateVeloMaxAngle > 0f)
            {
                rotateVelo = lureAsset.rotateVeloMaxAngle *
                             (1f - Mathf.PingPong(
                                 Time.time * rigidbody.linearVelocity.normalized.magnitude *
                                 lureAsset.rotateVeloMaxSpeed, 2f));
            }
            else if (lureAsset.rotateVeloMaxAngle == 0f)
            {
                rotateVelo = Time.time * rigidbody.linearVelocity.normalized.magnitude * lureAsset.rotateVeloMaxSpeed;
            }

            if (rigidbody.linearVelocity.magnitude > 0f)
            {
                if (lureAsset.rotateVeloMaxAngle == 0f)
                {
                    transform.Rotate(Vector3.forward, rotateVelo);
                    return;
                }

                transform.Rotate(Vector3.up, rotateVelo);
                rigidbody.AddForce(transform.right * rotateVelo, ForceMode.Acceleration);
            }
        }
    }

    private void MoveTypeController()
    {
        if (transform.position.y > waterHeightPosition + 0.1f ||
            Vector3.Distance(Rod.transform.position, transform.position) < 5f)
        {
            moveTowardsFactor = 0f;
            moveUpperFactor = 0f;
            percentEfficacy = 0f;
            currentMoveType = MoveType.None;
            newMoveType = MoveType.None;
            return;
        }

        float num = (Mathf.Abs(rigidbody.linearVelocity.x) + Mathf.Abs(rigidbody.linearVelocity.z)) * 0.5f;
        moveTowardsFactor = Mathf.MoveTowards(moveTowardsFactor, Mathf.Clamp01(num * 1.5f), Time.deltaTime * 0.5f);
        moveUpperFactor = Mathf.MoveTowards(moveUpperFactor, Mathf.Clamp01((0f - rigidbody.linearVelocity.y) * 1.3f),
            Time.deltaTime * 0.5f);
        if (moveTowardsFactor > 0.01f && moveTowardsFactor < 0.5f)
        {
            newMoveType = MoveType.PowolnyWleczony;
        }
        else if (moveTowardsFactor >= 0.5f && moveTowardsFactor < 1f)
        {
            newMoveType = MoveType.Wleczony;
        }
        else if (moveTowardsFactor <= 0.1f)
        {
            if (moveUpperFactor > 0f && moveUpperFactor < 0.4f)
            {
                newMoveType = MoveType.PowolnyOpadający;
            }
            else if (moveUpperFactor >= 0.4f && moveUpperFactor < 0.8f)
            {
                newMoveType = MoveType.Opadający;
            }
            else
            {
                newMoveType = MoveType.None;
            }
        }
        else
        {
            newMoveType = MoveType.None;
        }

        if (currentMoveType == LureMoveType)
        {
            percentEfficacy = Mathf.MoveTowards(percentEfficacy, 1f, Time.deltaTime * 0.4f);
        }
        else
        {
            percentEfficacy = Mathf.MoveTowards(percentEfficacy, 0f, Time.deltaTime * 0.2f);
        }

        if (newMoveType != currentMoveType)
        {
            delayMoveTypeTimer += Time.deltaTime;
            if (delayMoveTypeTimer > 1f)
            {
                currentMoveType = newMoveType;
                delayMoveTypeTimer = 0f;
            }
        }
    }


    private void OnDestroy()
    {
        if ((bool)currentWaterDrop)
        {
            Destroy(currentWaterDrop.gameObject);
        }
    }
}