using System.Collections;
using System.Collections.Generic;
using NBF;
// using UltimateWater;
using UnityEngine;

public class FFish : MonoBehaviour
{
    public FishSpecies fishSpecies;

    public float fishWeight = 0.2f;

    public float fishStrenght = 1f;

    public float fishStamina = 1f;

    private float attackAgresive = 2.5f;

    public SkinnedMeshRenderer m_Renderer;

    public Transform leftHandHandlerFishView;

    public Transform rightHandHandlerFishView;

    [HideInInspector] public bool isFishView;

    [HideInInspector] public FRod currentRod;

    public Transform baitContainer;

    public SpringJoint joiner;

    public Transform jointAnchor;

    [HideInInspector] private Rigidbody currentJoinObject;

    private Collider colider;

    public float waterDrag = 3f;

    public bool isInWater;

    private bool isTake;

    private float takeTimer;

    private float dropTimer = 2f;

    private float takeDelay;

    private float angularVelocityY;

    public float objectDisplacement;

    private Animator animator;

    [HideInInspector] public Rigidbody rigidbody;

    private float waterHeightPosition;

    private float normalDrag;

    [SerializeField] private float speed;

    [SerializeField] private float timeMove;

    private float timeCatch;

    private float timeToTake;

    [HideInInspector] public Vector3 TargetPoint;

    [HideInInspector] public FFishSystem fFishSystem;

    public Transform currentSearchBaitLure;

    [HideInInspector] public FLure currentSearchLure;

    private FHook currentSearchHook;

    [HideInInspector] public Rigidbody currentBaitLure;

    private Vector3 startPosition;

    public ParticleSystem bubbleParticle;

    // private WaterForce waterForce;

    public bool isGetFish;

    [HideInInspector] public float getFishCounter;

    private float jumpDelay = 5f;

    private bool isFeeding;

    private float timeFeeding = 30f;

    [HideInInspector] public FishFeedingZone currentFeedingZone;

    [HideInInspector] public int feedingZoneMoveState;

    private TextMesh currentDebugText;

    [HideInInspector] public float TimerToDisable;

    private AudioSource audioSource;

    [HideInInspector] public List<Material> orginalMaterials;

    private int boostlevel;

    public float fightingTime;

    private bool fishingReel;

    private float pullingTime;

    private float currentSmoothiess = -1f;

    private bool switchEagle;

    private Vector3 nextTargetPosition = Vector3.zero;

    private Vector3 lastValidPosition = Vector3.zero;

    private bool forceToLastValidPosition;

    private float checkUnhookTimer = 1f;

    private float randomSpeed;

    private GameObject firstPoint;

    private void Start()
    {
        colider = GetComponent<Collider>();
        animator = GetComponent<Animator>();
        rigidbody = GetComponent<Rigidbody>();
        joiner = GetComponent<SpringJoint>();
        normalDrag = 1f;
        startPosition = transform.position;
        waterHeightPosition = 0f;
        orginalMaterials = new List<Material>();
        orginalMaterials.AddRange(m_Renderer.sharedMaterials);
        transform.SetAsFirstSibling();
        // waterForce = base.transform.gameObject.AddComponent<WaterForce>();
        // waterForce.Force = 0f;
        // waterForce.Radius = 0.1f * fishWeight;
        RandomNewDstPointFreeMove();
        if (transform.position.y < 0f && !fFishSystem.isNewSpawnerSystem)
        {
            StartCoroutine(SetDisable());
        }

        fightingTime = 0f;
    }

    private IEnumerator SetDisable()
    {
        yield return null;
        gameObject.SetActive(value: false);
    }

    private void Update()
    {
        if (isFishView)
        {
            rigidbody.isKinematic = true;
            animator.SetFloat("speed", 0f);
            animator.SetBool("fishView", value: true);
            if ((bool)currentDebugText)
            {
                Destroy(currentDebugText);
            }

            return;
        }

        if ((bool)currentRod)
        {
            if ((bool)currentRod.Owner &&
                rigidbody.constraints != RigidbodyConstraints.FreezeRotationZ)
            {
                rigidbody.constraints = RigidbodyConstraints.FreezeRotationZ;
                rigidbody.rotation = Quaternion.identity;
            }
        }
        else if (rigidbody.constraints != 0)
        {
            rigidbody.constraints = RigidbodyConstraints.None;
        }

        ShowWaterFX();
        // ShowWaterBubbleFX();
        jumpDelay = Mathf.MoveTowards(jumpDelay, 0f, Time.deltaTime);
        TimerToDisable = Mathf.MoveTowards(TimerToDisable, 0f, Time.deltaTime);
    }

    private void FixedUpdate()
    {
        CheckIsWater();
        if (m_Renderer.enabled)
        {
            if (transform.position.y < 0.2f)
            {
                SetMaterialSmootness(0.1f);
            }
            else
            {
                SetMaterialSmootness(0.8f);
            }
        }

        if (isFishView)
        {
            return;
        }

        if (!isGetFish)
        {
            if (animator.runtimeAnimatorController != null)
            {
                if (isInWater)
                {
                    animator.SetFloat("speed", speed * 0.25f);
                    animator.SetFloat("AngularVelocityY", angularVelocityY);
                }
                else
                {
                    animator.SetFloat("speed", 0.1f);
                    animator.SetFloat("AngularVelocityY", 0f);
                }
            }

            FreeMoveNew();
            FightingRealistic();
            if ((bool)currentRod && isTake && !currentRod.currentFish)
            {
                takeTimer += Time.deltaTime;
                if (takeTimer > dropTimer)
                {
                    UnJoinFish();
                    takeTimer = 0f;
                }
            }
        }
        else if (animator.runtimeAnimatorController != null)
        {
            animator.SetFloat("speed", 0.1f);
            animator.SetFloat("AngularVelocityY", 0f);
        }
    }

    private void PlaySoundFight()
    {
        // if (!audioSource)
        // {
        //     if (!FScriptsHandler.Instance.m_PlayerMain.underWaterCamera)
        //     {
        //         return;
        //     }
        //
        //     audioSource = gameObject.AddComponent<AudioSource>();
        //     audioSource.playOnAwake = false;
        //     audioSource.spatialBlend = 1f;
        // }

        // if ((bool)audioSource && !audioSource.isPlaying)
        // {
        //     int num = Random.Range(0, FScriptsHandler.Instance.fishBubbleSound.Length - 1);
        //     audioSource.clip = FScriptsHandler.Instance.fishBubbleSound[num];
        //     audioSource.Play();
        // }
    }

    private void DestroyAudioSource()
    {
        if (!audioSource)
        {
            Destroy(audioSource);
        }
    }

    private void SetMaterialSmootness(float value)
    {
        if (currentSmoothiess != value)
        {
            Material[] materials = m_Renderer.materials;
            for (int i = 0; i < materials.Length; i++)
            {
                materials[i].SetFloat("_GlossMapScale", value);
            }

            currentSmoothiess = value;
        }
    }

    private void SetNewFeedingZoneDst()
    {
        if (fFishSystem == null)
        {
            fFishSystem = FindObjectOfType<FFishSystem>();
        }

        if (!(currentFeedingZone == null))
        {
            currentFeedingZone = currentFeedingZone.fFishSystem.GetNewTargetFeedingZone(this);
        }
    }

    private void SetAngularAnim(Vector3 targetPosition)
    {
        float num = Vector3.Angle(targetPosition - transform.position, transform.forward);
        if (angularVelocityY == 0f &&
            ((num > -80f && num < 80f && speed > 0.8f) || rigidbody.angularVelocity.sqrMagnitude > 1f))
        {
            if (rigidbody.angularVelocity.sqrMagnitude > 1f)
            {
                angularVelocityY = rigidbody.angularVelocity.sqrMagnitude;
            }
            else
            {
                angularVelocityY = num;
            }
        }
        else
        {
            angularVelocityY = 0f;
        }
    }

    private void FreeMoveNew()
    {
        if (isGetFish)
        {
            return;
        }

        takeDelay = Mathf.MoveTowards(takeDelay, 0f, Time.deltaTime);
        timeMove = Mathf.MoveTowards(timeMove, 0f, Time.deltaTime);
        if ((bool)currentSearchBaitLure)
        {
            if ((bool)currentSearchHook && currentSearchHook.isLookingDisable)
            {
                currentSearchBaitLure = null;
            }

            if ((bool)currentSearchLure && currentSearchLure.isLookingDisable)
            {
                currentSearchBaitLure = null;
            }
        }

        if (!isInWater)
        {
            return;
        }

        rigidbody.isKinematic = false;
        rigidbody.mass = fishWeight * 10f;
        rigidbody.angularDamping = 3f;
        rigidbody.useGravity = false;
        float num = 0f - Mathf.Clamp01(fishWeight) * 0.5f;
        if (forceToLastValidPosition && lastValidPosition != Vector3.zero)
        {
            if (fishingReel && (double)fishStamina <= 0.035 && fishWeight >= 300f &&
                currentRod.Owner.Gears.Reel.reelingDrag > 0f)
            {
                Vector3 position = SceneSettings.Instance.transform.position;
                Quaternion b = Quaternion.LookRotation(position - transform.position);
                float value = Vector3.Distance(transform.position, position);
                rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b, Time.deltaTime * (5f + speed));
                rigidbody.transform.Translate(Vector3.forward * Time.deltaTime * speed);
                speed = Mathf.MoveTowards(speed, Mathf.Clamp01(value) * 0.25f + 0.1f, Time.deltaTime);
                SetAngularAnim(position);
                return;
            }

            Quaternion b2 = Quaternion.LookRotation(lastValidPosition - transform.position);
            float num2 = Vector3.Distance(transform.position, lastValidPosition);
            rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b2, Time.deltaTime * (5f + speed));
            if (!joiner || !(currentRod.Owner.Gears.Reel.reelingDrag > 0.99f))
            {
                rigidbody.transform.Translate(Vector3.forward * Time.deltaTime * speed);
                speed = Mathf.MoveTowards(speed, Mathf.Clamp01(num2) * 0.25f + 0.1f, Time.deltaTime);
                SetAngularAnim(lastValidPosition);
                if (num2 < 0.2f || timeMove == 0f)
                {
                    forceToLastValidPosition = false;
                }
                else if ((bool)joiner && (num2 < 0.2f || timeMove == 0f))
                {
                    forceToLastValidPosition = false;
                }

                if (fishingReel)
                {
                    Vector3 position2 = SceneSettings.Instance.transform.position;
                    Quaternion b3 = Quaternion.LookRotation(position2 - transform.position);
                    float value2 = Vector3.Distance(transform.position, position2);
                    rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b3, Time.deltaTime * (5f + speed));
                    rigidbody.transform.Translate(Vector3.forward * Time.deltaTime * speed);
                    speed = Mathf.MoveTowards(speed, Mathf.Clamp01(value2) * 0.25f + 0.1f, Time.deltaTime);
                    SetAngularAnim(position2);
                }
            }

            return;
        }

        if (Vector3.Distance(transform.position, nextTargetPosition) < 0.25f || timeMove == 0f ||
            nextTargetPosition == Vector3.zero)
        {
            if (fishingReel && (double)fishStamina <= 0.035 && fishWeight >= 300f)
            {
                Vector3 position3 = SceneSettings.Instance.transform.position;
                Quaternion b4 = Quaternion.LookRotation(position3 - transform.position);
                float value3 = Vector3.Distance(transform.position, position3);
                rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b4, Time.deltaTime * (5f + speed));
                rigidbody.transform.Translate(Vector3.forward * Time.deltaTime * speed);
                speed = Mathf.MoveTowards(speed, Mathf.Clamp01(value3) * 0.25f + 0.1f, Time.deltaTime);
                SetAngularAnim(position3);
                return;
            }

            Vector3 zero = Vector3.zero;
            if ((bool)currentSearchBaitLure)
            {
                if (currentSearchBaitLure.transform.position.y > 0.15f)
                {
                    currentSearchBaitLure = null;
                    return;
                }

                zero = currentSearchBaitLure.position;
                if (Vector3.Distance(transform.position, currentSearchBaitLure.transform.position) < 1.5f)
                {
                    colider.enabled = false;
                    if (Vector3.Distance(transform.position, currentSearchBaitLure.transform.position) < 0.3f)
                    {
                        Take(currentSearchBaitLure);
                        colider.enabled = true;
                        Debug.Log("Take");
                    }
                }
                else
                {
                    colider.enabled = true;
                }
            }
            else
            {
                zero = RandomSphereMovePoint();
                // SearchingBait();
            }

            Quaternion b5 = Quaternion.Euler(0f, rigidbody.transform.localEulerAngles.y, 0f);
            rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b5, Time.deltaTime * speed);
            speed = Mathf.MoveTowards(speed, 0.05f, Time.deltaTime * 0.5f);
            rigidbody.transform.Translate(Vector3.forward * Time.deltaTime * speed);
            if ((zero.y >= num || !ProbeDeepToTerenBottom(zero)) && !currentSearchBaitLure)
            {
                zero = lastValidPosition;
                return;
            }

            if (ProbeDeepWaterToTerrain(transform.position) > 0.3f && transform.position.y < 0f)
            {
                lastValidPosition = transform.position;
            }

            if ((bool)firstPoint)
            {
                firstPoint.transform.position = lastValidPosition;
            }

            nextTargetPosition = zero;
            if (!joiner)
            {
                timeMove = Random.Range(8f, 15f);
            }
            else
            {
                timeMove = Random.Range(5f, 15f);
            }

            return;
        }

        Quaternion b6 = Quaternion.LookRotation(nextTargetPosition - transform.position);
        rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b6, Time.deltaTime * (5f * speed));
        Quaternion b7 = Quaternion.Euler(0f, rigidbody.transform.localEulerAngles.y, 0f);
        rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b7, Time.deltaTime * (1f + speed * 0.5f));
        if ((bool)currentSearchBaitLure)
        {
            if ((bool)currentSearchLure)
            {
                speed = Mathf.MoveTowards(speed, attackAgresive, Time.deltaTime);
            }

            speed = Mathf.Clamp(speed, 3f, 6f);
        }
        else if ((bool)joiner)
        {
            float num3 = Mathf.Clamp01(1f - Mathf.Clamp01(Mathf.Abs(transform.position.y))) *
                         Mathf.Clamp(fishStrenght, 0.3f, 1f);
            float num4 = Mathf.Clamp(fishWeight * 0.2f * fishStrenght, 0.3f, 1.5f);
            speed = Mathf.MoveTowards(speed, (num4 + num3 + timeMove * 8f) * Mathf.Clamp(fishStamina, 0.5f, 1f),
                Time.deltaTime);
            speed = Mathf.Clamp(speed, 0f, 2f);
        }
        else
        {
            speed = Mathf.MoveTowards(speed, 1.5f / timeMove, Time.deltaTime);
            speed = Mathf.Clamp(speed, 0.1f, 2f);
        }

        SetAngularAnim(nextTargetPosition);
        if (!animator.GetBool("jump"))
        {
            rigidbody.transform.Translate(Vector3.forward * Time.deltaTime * speed);
        }

        if ((bool)joiner)
        {
            float num5 = Vector3.SignedAngle(transform.forward, Vector3.up, Vector3.forward);
            float y = transform.position.y;
            if (num5 > -60f && num5 < 60f && y > 0f - colider.bounds.size.y * 3f)
            {
                AddJump();
            }
            else if (animator.runtimeAnimatorController != null)
            {
                animator.SetBool("jump", value: false);
            }
        }
    }

    private Vector3 RandomSphereMovePoint()
    {
        return transform.position + Random.onUnitSphere * Random.Range(2f, 20f);
    }
    
    private void GetFish()
    {
        if (animator.runtimeAnimatorController != null)
        {
            animator.SetBool("jump", value: false);
            animator.SetFloat("AngularVelocityY", 0f);
        }

        isGetFish = true;
        speed = 0f;
        rigidbody.mass = 2f;
        rigidbody.useGravity = true;
        rigidbody.linearDamping = normalDrag;
        rigidbody.angularDamping = normalDrag * 0.2f;
        GetComponent<Collider>().enabled = false;
        ReJoinInOutWater();
    }

    private bool CheckUnnhookFish()
    {
        // if ((bool)TutorialManager.Instance)
        // {
        //     return false;
        // }

        if (isGetFish)
        {
            return false;
        }

        // if (GameManager.Instance._playerData.Player[GameManager.Instance._playerData.currentPlayerProfileIndex]
        //         .gameMode == GameManager.PlayerData.CPlayer.GameMode.Normal)
        // {
        //     return false;
        // }

        timeCatch += Time.deltaTime;
        checkUnhookTimer = Mathf.MoveTowards(checkUnhookTimer, 0f, Time.deltaTime);
        if (checkUnhookTimer > 0f)
        {
            return false;
        }

        checkUnhookTimer = 30f;
        float num = timeCatch * 0.02f;
        if (Vector3.Distance(transform.position, currentRod.transform.position) < 10f)
        {
            return false;
        }

        var gear = currentRod.Owner.Gears;

        Vector2 vector = Vector2.zero;
        if ((bool)gear.Hook)
        {
            vector = GameHooks.Get(gear.Hook.GearData.configId).GetFishAcceptWeightBySize();
        }

        if ((bool)gear.Lure)
        {
            vector = GameBaits.Get(gear.Lure.GearData.configId).weightFishAccept;
        }

        float num2 = 0f;
        if (fishWeight < vector.x)
        {
            num2 = vector.x - fishWeight;
        }
        else if (fishWeight > vector.y)
        {
            num2 = fishWeight - vector.y;
        }

        num += num2 * 5f;
        if (gear.Hook && GameHooks.Get(gear.Hook.GearData.configId).zadzior != 0 &&
            num > 30f &&
            num2 < 1f)
        {
            num -= 30f;
        }

        if (Random.Range(0, 100) < num)
        {
            Debug.Log("Unhook fish currentUnhookSuccess:" + num + " fishWeightAcceptDiferent:" + num2);
            if ((bool)currentRod.currentFish)
            {
                // GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("FISH_UNHOOKED"),
                //     FScriptsHandler.Instance.m_Canvas.transform);
            }

            UnJoinFish();
            checkUnhookTimer = 1f;
            return true;
        }

        return false;
    }

    private void AddBoostStamina()
    {
        switch (boostlevel)
        {
            case 0:
                if (fishStamina <= 0.7f)
                {
                    fishStamina += Random.Range(0.1f, 0.2f);
                    boostlevel++;
                    Debug.Log("Add stamina boost level: " + boostlevel);
                }

                break;
            case 1:
                if (fishStamina <= 0.5f)
                {
                    fishStamina += Random.Range(0.1f, 0.2f);
                    boostlevel++;
                    Debug.Log("Add stamina boost level: " + boostlevel);
                }

                break;
            case 2:
                if (fishStamina <= 0.2f)
                {
                    fishStamina += Random.Range(0.2f, 0.3f);
                    boostlevel++;
                    Debug.Log("Add stamina boost level: " + boostlevel);
                }

                break;
        }
    }

    private void FightingRealistic()
    {
        if (joiner == null || isGetFish)
        {
            return;
        }

        if ((bool)currentBaitLure)
        {
            currentBaitLure.transform.localPosition = Vector3.zero;
        }

        // if (currentRod.linelenghtDiferent > 0f &&
        //     Vector3.Distance(transform.position, currentRod.transform.position) < 5f && InputManager.isPullUpRod)
        // {
        //     getFishCounter = Mathf.MoveTowards(getFishCounter, 1f, Time.deltaTime * 0.5f);
        //     // FScriptsHandler.Instance.m_HudManager.ShowGetFishCircle(getFishCounter);
        //     if (Mathf.Approximately(getFishCounter, 1f))
        //     {
        //         GetFish();
        //         // FScriptsHandler.Instance.m_HudManager.ShowGetFishCircle(-1f);
        //         return;
        //     }
        // }
        // else if (currentRod.linelenghtDiferent > 0f &&
        //          Vector3.Distance(transform.position, currentRod.transform.position) < 5f)
        // {
        //     getFishCounter = 0f;
        //     // FScriptsHandler.Instance.m_HudManager.ShowGetFishCircle(0f);
        // }
        // else
        // {
        //     getFishCounter = 0f;
        //     // FScriptsHandler.Instance.m_HudManager.ShowGetFishCircle(-1f);
        // }

        if (CheckUnnhookFish() && getFishCounter == 0f)
        {
            return;
        }

        AddBoostStamina();
        fightingTime += Time.deltaTime;
        fishStrenght = fishWeight * fishStamina - currentRod.currentLineStrenght;
        fishStrenght = Mathf.Clamp01(fishStrenght);
        rigidbody.mass = fishWeight * 20f * fishStamina;
        float num = fishWeight * fishStamina - currentRod.currentLineStrenght;
        var reel = currentRod.Owner.Gears.Reel;
        if ((bool)currentRod.Owner)
        {
            // if (InputManager.isPullUpRod)
            // {
            //     pullingTime += Time.deltaTime;
            //     if (currentRod.Owner.handPullUp < 1f)
            //     {
            //         if (reel.reelingDrag > 0f)
            //         {
            //             if (reel.reelingDrag < 1f)
            //             {
            //                 fishingReel = true;
            //             }
            //
            //             int num2 = 2;
            //             if (pullingTime < num2)
            //             {
            //                 float num3 = currentRod.Owner.handPullUp *
            //                     currentRod.currentLineStrenght * 0.25f / fishWeight;
            //                 num -= num3;
            //                 fishStrenght -= num3;
            //                 fishStamina = Mathf.MoveTowards(fishStamina, 0.03f, Time.deltaTime * num3);
            //             }
            //         }
            //     }
            //     else if (reel.reelingDrag > 0f)
            //     {
            //         fishingReel = true;
            //         fishStamina = Mathf.MoveTowards(fishStamina, 0.03f,
            //             Time.deltaTime * 0.025f * currentRod.currentLineStrenght *
            //             reel.reelingDrag);
            //         if (fishStamina <= 0.035 && fishWeight >= 300f)
            //         {
            //             Vector3 position = SceneSettings.Instance.transform.position;
            //             Quaternion b = Quaternion.LookRotation(position - transform.position);
            //             float value = Vector3.Distance(transform.position, position);
            //             rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b, Time.deltaTime * (5f + speed));
            //             rigidbody.transform.Translate(Vector3.forward * Time.deltaTime * speed);
            //             speed = Mathf.MoveTowards(speed, Mathf.Clamp01(value) * 0.25f + 0.1f, Time.deltaTime);
            //             SetAngularAnim(position);
            //         }
            //     }
            // }
            // else
            // {
            //     if (pullingTime != 0f)
            //     {
            //         pullingTime = 0f;
            //     }
            //
            //     fishingReel = false;
            //     if (reel.reelingDrag > 0f)
            //     {
            //         fishStamina = Mathf.MoveTowards(fishStamina, 0.03f,
            //             Time.deltaTime * 0.001f * currentRod.currentLineStrenght *
            //             reel.reelingDrag);
            //     }
            // }
        }

        // if ((bool)currentRod.currentPlayer &&
        //     currentRod.currentPlayer.firstPersonController.m_CharacterController.velocity != Vector3.zero &&
        //     currentRod.currentPlayer.transform.parent.name == "_GAMEPLAY_V2")
        // {
        //     joiner.maxDistance = Vector3.Distance(jointAnchor.transform.position, currentJoinObject.transform.position);
        // }

        num = Mathf.Clamp(num, -1f, 1f);
        if (num > 0f)
        {
            joiner.maxDistance = Mathf.MoveTowards(joiner.maxDistance,
                Vector3.Distance(jointAnchor.transform.position, currentJoinObject.transform.position),
                Time.deltaTime * num);
        }
        else if (currentRod.lineHandler.lineType == FLineHandler.LineType.OneSegment)
        {
            joiner.maxDistance = Mathf.MoveTowards(joiner.maxDistance,
                currentRod.Owner.Data.lineLength, Time.deltaTime * (0f - num) * 10f);
        }
        else
        {
            joiner.maxDistance = Mathf.MoveTowards(joiner.maxDistance,
                currentRod.Owner.Data.lineLength +
                currentRod.lineHandler.LineConnector_2.maxDistance + 0.5f,
                Time.deltaTime * (0f - num) * 10f);
        }
    }

    private void AddJump()
    {
        if (!(jumpDelay > 0f))
        {
            Debug.Log("Jump start");
            rigidbody.AddForce((Vector3.up + transform.forward) * Random.Range(100, 220), ForceMode.Acceleration);
            rigidbody.AddTorque(Vector3.up * Random.Range(150, 250), ForceMode.Acceleration);
            // BuildSplashJump(new Vector3(transform.position.x, waterHeightPosition, transform.position.z));
            if (animator.runtimeAnimatorController != null)
            {
                animator.SetBool("jump", value: true);
            }

            jumpDelay = Random.Range(5, 15);
            if (fishStamina < 0.2f)
            {
                fishStamina += 0.1f;
                fishStamina = Mathf.Clamp(fishStamina, 0f, 0.5f);
            }
        }
    }

    public void OnEnableFormFishSystem()
    {
        _ = (bool)currentFeedingZone;
    }

    public void RandomNewDstPointFreeMove()
    {
        if (joiner != null)
        {
            return;
        }

        if (!currentFeedingZone)
        {
            SetNewFeedingZoneDst();
            return;
        }

        if (Vector3.Distance(transform.position, currentFeedingZone.transform.position) <
            currentFeedingZone.rangeZone)
        {
            SetNewFeedingZoneDst();
        }

        float num = ProbeDeepWaterToTerrain(currentFeedingZone.transform.position);
        if ((bool)currentFeedingZone)
        {
            TargetPoint = new Vector3(
                currentFeedingZone.transform.position.x +
                Random.Range(0f - currentFeedingZone.rangeZone, currentFeedingZone.rangeZone),
                Random.Range(0f - num, 0f),
                currentFeedingZone.transform.position.z +
                Random.Range(0f - currentFeedingZone.rangeZone, currentFeedingZone.rangeZone));
        }
        else
        {
            TargetPoint = transform.position + Random.onUnitSphere * 5f;
            TargetPoint = new Vector3(TargetPoint.x, Random.Range(0f - num, 0f), TargetPoint.z);
        }

        fishStamina += 0.01f;
        fishStamina = Mathf.Clamp(fishStamina, 0.3f, 1f);
    }

    private void OnDrawGizmosSelected()
    {
        if (colider == null) return;
        Vector3 targetPoint = TargetPoint;
        Debug.Log("Target position: " + targetPoint.ToString() + " minimum deep: " +
                  (waterHeightPosition - colider.bounds.size.y));
        Gizmos.color = Color.white;
        Gizmos.DrawSphere(lastValidPosition, 0.2f);
    }

    public void JoinFish(Rigidbody joinObject)
    {
        currentJoinObject = joinObject;
        TargetPoint = transform.position;
        if (GetComponent<SpringJoint>() == null)
        {
            joiner = transform.gameObject.AddComponent<SpringJoint>();
        }

        joiner.anchor = jointAnchor.localPosition;
        joiner.autoConfigureConnectedAnchor = false;
        joiner.connectedAnchor = Vector3.zero;
        joiner.spring = 1500f;
        joiner.maxDistance = Vector3.Distance(jointAnchor.transform.position, currentJoinObject.transform.position);
        joiner.connectedBody = currentJoinObject;
        timeCatch = 0f;
        PlaySoundFight();
    }

    public void UnJoinFish(bool resetPosition = false)
    {
        if (TryGetComponent<SpringJoint>(out var component))
        {
            Destroy(component);
        }

        if (TryGetComponent<ConfigurableJoint>(out var component2))
        {
            Destroy(component2);
        }

        joiner = null;
        isTake = false;
        if ((bool)currentRod)
        {
            currentRod.takeFish = null;
        }

        if ((bool)currentRod)
        {
            currentRod.currentFish = null;
        }

        currentRod = null;
        isGetFish = false;
        currentSearchBaitLure = null;
        currentSearchLure = null;
        currentSearchHook = null;
        boostlevel = 0;
        if ((bool)currentBaitLure)
        {
            currentBaitLure.transform.SetParent(transform.parent);
            if (currentBaitLure.TryGetComponent<FHook>(out var component3))
            {
                component3.isLookingDisable = false;
                component3.rigidbody.isKinematic = false;
            }

            if (currentBaitLure.TryGetComponent<FLure>(out var component4))
            {
                component4.isLookingDisable = false;
                component4.rigidbody.isKinematic = false;
                component4.currentFish = null;
            }

            currentBaitLure = null;
        }

        if (resetPosition)
        {
            transform.position = startPosition;
        }

        DestroyAudioSource();
    }

    public void ReJoin(Rigidbody newJoin)
    {
        if (joiner != null)
        {
            Destroy(joiner);
        }

        joiner = null;
        ConfigurableJoint configurableJoint = gameObject.AddComponent<ConfigurableJoint>();
        configurableJoint.anchor = jointAnchor.localPosition;
        configurableJoint.autoConfigureConnectedAnchor = false;
        configurableJoint.connectedAnchor = Vector3.zero;
        configurableJoint.xMotion = ConfigurableJointMotion.Locked;
        configurableJoint.yMotion = ConfigurableJointMotion.Locked;
        configurableJoint.zMotion = ConfigurableJointMotion.Locked;
        configurableJoint.angularXMotion = ConfigurableJointMotion.Free;
        configurableJoint.angularYMotion = ConfigurableJointMotion.Free;
        configurableJoint.angularZMotion = ConfigurableJointMotion.Free;
        newJoin.transform.position = jointAnchor.position;
        configurableJoint.connectedBody = newJoin;
        currentBaitLure.transform.SetParent(transform.parent);
    }
    

    public void Take(Transform takeObject)
    {
        if (isTake)
        {
            return;
        }

        if (takeObject.TryGetComponent<FHook>(out var component))
        {
            var rod = component.Owner.Gears.Rod;
            if ((bool)rod && !rod.currentFish && !rod.takeFish)
            {
                Debug.Log("Branie Haczyk!!");
                currentRod = rod;
                currentRod.takeFish = this;
                currentBaitLure = component.rigidbody;
                JoinFish(currentRod.lineHandler.EndLineRigidbody_0);
                currentBaitLure.transform.SetParent(baitContainer);
                currentBaitLure.isKinematic = true;
                currentBaitLure.transform.position = jointAnchor.position;
                currentBaitLure.transform.localEulerAngles = component.rotationInFishJaw;
                isTake = true;
                isGetFish = false;
                component.isLookingDisable = true;
                currentSearchBaitLure = null;
                currentSearchLure = null;
                currentSearchHook = null;
                component.transform.localPosition = Vector3.zero;
                // if ((bool)component.Owner.Gears.currentFeeder)
                // {
                //     dropTimer = Random.Range(10f, 20f);
                // }
                // else
                {
                    dropTimer = Random.Range(1f, 10f);
                }
            }
        }
        else
        {
            if (!takeObject.TryGetComponent<FLure>(out var component2))
            {
                return;
            }

            Debug.Log("Branie Lure!!");
            if ((bool)component2.Owner.Gears.Rod && !component2.Owner.Gears.Rod.currentFish &&
                !component2.Owner.Gears.Rod.takeFish)
            {
                component2.currentFish = this;
                currentRod = component2.Owner.Gears.Rod;
                currentRod.takeFish = this;
                currentBaitLure = component2.rigidbody;
                JoinFish(currentRod.lineHandler.EndLineRigidbody_0);
                currentBaitLure.isKinematic = true;
                currentBaitLure.transform.position = jointAnchor.position;
                currentBaitLure.transform.localEulerAngles = component2.lureAsset.rotationInFishJaw;
                currentBaitLure.transform.SetParent(baitContainer);
                isTake = true;
                isGetFish = false;
                component2.isLookingDisable = true;
                currentSearchBaitLure = null;
                currentSearchLure = null;
                currentSearchHook = null;
                dropTimer = Random.Range(2f, 10f);
            }
        }
    }

    public void ReJoinInOutWater()
    {
        if (currentRod.lineHandler.lineType == FLineHandler.LineType.OneSegment)
        {
            ReJoin(currentBaitLure.GetComponent<FLure>().fishJoiner);
        }

        if (currentRod.lineHandler.lineType == FLineHandler.LineType.TwoSegment)
        {
            ReJoin(currentBaitLure.GetComponent<FHook>().fishJoiner);
        }

        if (currentRod.lineHandler.lineType == FLineHandler.LineType.ThereSegment)
        {
            ReJoin(currentBaitLure.GetComponent<FHook>().fishJoiner);
        }

        currentBaitLure.isKinematic = false;
        Debug.Log("Out Water rejoin");
    }

    private float ProbeDeepWaterToTerrain(Vector3 probeObject)
    {
        int mask = LayerMask.GetMask("Terrain");
        float result = 0f;
        if (Physics.Raycast(new Vector3(probeObject.x, 0f, probeObject.z), -Vector3.up, out var hitInfo,
                float.PositiveInfinity, mask))
        {
            result = hitInfo.distance;
        }

        return result;
    }

    private bool ProbeDeepToTerenBottom(Vector3 probeObject)
    {
        int mask = LayerMask.GetMask("Terrain");
        if (Physics.Raycast(probeObject, -Vector3.up, out var _, float.PositiveInfinity, mask))
        {
            return true;
        }

        return false;
    }

    private void ShowWaterFX()
    {
        // if (!m_Renderer.isVisible)
        // {
        //     return;
        // }
        //
        // if (Vector3.Distance(FScriptsHandler.Instance.m_PlayerMain.transform.position, transform.position) < 3f)
        // {
        //     // waterForce.Force = 0f;
        //     return;
        // }
        //
        // float num = waterHeightPosition - transform.position.y;
        // if (num < 0.1f && num > -1f)
        // {
        //     float num2 = Mathf.Abs(num);
        //     // waterForce.Force = (30f - 30f * num2) * (rigidbody.linearVelocity.sqrMagnitude + speed);
        //     // waterForce.Force = Mathf.Clamp(waterForce.Force, 0f, 25f);
        // }
        // else
        // {
        //     // waterForce.Force = 0f;
        // }
    }

    
    private void CheckIsWater()
    {
        if (!colider || !rigidbody)
        {
            return;
        }

        if (transform.position.y < 0f)
        {
            if (!isInWater)
            {
                isInWater = true;
                rigidbody.linearDamping = waterDrag;
                rigidbody.useGravity = false;
                fFishSystem.AddInwaterFishObject(this);
            }
        }
        else
        {
            if (!isInWater)
            {
                return;
            }

            isInWater = false;
            rigidbody.linearDamping = normalDrag;
            rigidbody.useGravity = true;
            fFishSystem.DeleteInwaterFishObject(this);
            if (!joiner)
            {
                timeMove = 0f;
                if ((bool)currentSearchBaitLure)
                {
                    currentSearchBaitLure = null;
                }
            }
        }
    }

    private void OnTriggerEnter(Collider col)
    {
        if (col.transform.CompareTag("Water"))
        {
            // BuildSplash(transform.position, 0f);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.CompareTag("Water"))
        {
            // BuildSplash(transform.position, 0.1f);
        }
    }

    private void OnDestroy()
    {
        if ((bool)fFishSystem && (enabled || fFishSystem.isNewSpawnerSystem))
        {
            fFishSystem.DeleteInwaterFishObject(this);
            if (fFishSystem.inwaterFishObjectsSpawner.Contains(this))
            {
                fFishSystem.inwaterFishObjectsSpawner.Remove(this);
            }
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        forceToLastValidPosition = true;
    }

    private void OnEnable()
    {
        if ((bool)fFishSystem)
        {
            RandomNewDstPointFreeMove();
        }
    }

    private void OnDisable()
    {
        if ((bool)fFishSystem)
        {
            if (transform.position.y <= 0f)
            {
                fFishSystem.AddInwaterFishObject(this);
            }

            if ((bool)firstPoint)
            {
                Destroy(firstPoint.gameObject);
            }
        }
    }

    public void EnableFish()
    {
        gameObject.SetActive(value: true);
        lastValidPosition = transform.position;
        firstPoint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        firstPoint.GetComponent<MeshRenderer>().enabled = false;
        Destroy(firstPoint.GetComponent<Collider>());
        firstPoint.transform.localScale = Vector3.one * 0.17f;
        firstPoint.transform.position = lastValidPosition;
    }
}