using NBF;
using UnityEngine;

public class FHook : FPlayerGear
{
    [HideInInspector] public Rigidbody rigidbody;

    [HideInInspector] public FWaterDisplacement waterDisplacement;

    // [HideInInspector] public FRod currentRod;

    [Tooltip("Punkt połaczenia z rybą")] public Rigidbody fishJoiner;

    public Vector3 rotationInFishJaw = Vector3.zero;

    public bool isLookingDisable;

    private float hookTimer;

    public int hookNotAcceptFishCounter;


    public HookAsset hookAsset;

    private void Awake()
    {
        hookAsset = GetComponent<HookAsset>();
    }

    protected override void OnStart()
    {
        rigidbody = GetComponent<Rigidbody>();
        waterDisplacement = GetComponent<FWaterDisplacement>();
    }

    protected override void OnUpdate()
    {
        if (!Owner.Gears.Rod)
        {
            return;
        }

        if ((bool)Owner.Gears.Rod.currentFish)
        {
            waterDisplacement.isFreeze = true;
            waterDisplacement.useSplashes = false;
            AddHookNotAcceptedCount(reset: true);
        }
        else
        {
            waterDisplacement.isFreeze = false;
            waterDisplacement.useSplashes = true;
            if (waterDisplacement.isInWater)
            {
                Quaternion b = Quaternion.Euler(0f, rigidbody.transform.localEulerAngles.y,
                    rigidbody.linearVelocity.x - 1f);
                rigidbody.rotation = Quaternion.Slerp(rigidbody.rotation, b, Time.deltaTime * 1f);
                if ((bool)Owner.Gears.Rod.takeFish)
                {
                    AddHookNotAcceptedCount(reset: true);
                }
                else
                {
                    AddHookNotAcceptedCount(reset: false);
                }
            }
            else
            {
                AddHookNotAcceptedCount(reset: true);
            }
        }

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

    private void AddHookNotAcceptedCount(bool reset)
    {
        // if (GameManager.Instance._playerData.Player[GameManager.Instance._playerData.currentPlayerProfileIndex]
        //         .gameMode == GameManager.PlayerData.CPlayer.GameMode.Realistic ||
        //     FScriptsHandler.Instance.m_PlayerMain.currentRod != currentRod)
        // {
        //     return;
        // }

        if (reset)
        {
            hookTimer = 0f;
            hookNotAcceptFishCounter = 0;
            return;
        }

        hookTimer += Time.deltaTime;
        if (hookTimer >= 300f && hookNotAcceptFishCounter > 5)
        {
            hookTimer = 0f;
            hookNotAcceptFishCounter = 0;
            // GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("HOOK_NOT_ACCEPT_FISH_INFO"), FScriptsHandler.Instance.m_Canvas.transform);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        // if (!waterDisplacement.isInWater && (bool)Owner.Gears.Rod &&
        //     (bool)Owner.Gears.Rod.lineHandler && !Owner.Gears.Rod.currentFish &&
        //     Owner.Data.lineLength > 5f &&
        //     Vector3.Distance(transform.position, Owner.Gears.Rod.transform.position) > 5f)
        // {
        //     GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("HOOK_ON_THE_GROUND"), FScriptsHandler.Instance.m_Canvas.transform, deleteLast: true);
        // }
    }
}