// using UltimateWater;

using NBF;
using UnityEngine;

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

    [HideInInspector] public FWaterDisplacement waterDisplacement;
    
    public float currentFloatDeepth = 0.2f;

    public float newDeepth { get; set; } = 0.21f;

    public float floatDisplacement;

    // private BoxCollider collider;

    private float startWaterInteractiveMultiple = 5f;

    private Transform currentWaterDrop;

    // [SerializeField] public Transform scallingObject;

    private bool destroyDropFx;

    protected override void OnStart()
    {
        rigidbody = GetComponent<Rigidbody>();
        waterDisplacement = GetComponent<FWaterDisplacement>();
        waterDisplacement.objectDisplacement = floatDisplacement;
        // collider = GetComponent<BoxCollider>();
        if (Owner.Gears.Rod)
        {
            Owner.Gears.Rod.lineHandler.SetSegmentTwoLenght(currentFloatDeepth);
        }

        // if ((bool)waterInteractive)
        // {
        //     waterInteractive.enabled = false;
        //     startWaterInteractiveMultiple = waterInteractive.Multiplier;
        // }

        Invoke("EnableWaterInteractive", 3f);
    }

    protected override void OnUpdate()
    {
        if ((bool)Owner.Gears.Rod)
        {
            if ((bool)Owner.Gears.Rod.currentFish)
            {
                waterDisplacement.isFreeze = true;
                waterDisplacement.useSplashes = false;
            }
            else
            {
                waterDisplacement.isFreeze = false;
                waterDisplacement.useSplashes = true;
            }

            ShowWaterFX();
            SetDeepth();
            // Scalling();
        }
    }

    protected override void OnFixedUpdate()
    {
        DisplcementController();
    }

    // private void Scalling()
    // {
    //     if ((bool)Owner.Gears.Rod.lineHandler &&
    //         (bool)Owner.Gears.Rod.lineHandler.EndLineRigidbody_0)
    //     {
    //         // float floatSize = GameManager.Instance._playerData
    //         //     .Player[GameManager.Instance._playerData.currentPlayerProfileIndex].floatSize;
    //
    //         float floatSize = 1; //Owner.Data.currentGear.bobber.Config.weight;
    //         float num = Vector3.Distance(
    //             Owner.Gears.Rod.lineHandler.EndLineRigidbody_0.transform.position,
    //             scallingObject.transform.position);
    //         // if ((bool)waterInteractive)
    //         // {
    //         //     waterInteractive.Multiplier = startWaterInteractiveMultiple / (scallingObject.localScale.x * 2f);
    //         // }
    //
    //         // if (FScriptsHandler.Instance.m_PlayerMain.currentCameraView !=
    //         //     FScriptsHandler.Instance.m_PlayerMain.m_Camera)
    //         // {
    //         //     scallingObject.localScale = Vector3.one;
    //         // }
    //         // else 
    //         if (waterDisplacement.isInWater && !Owner.Gears.Rod.currentFish)
    //         {
    //             float value = 1f + num * 0.4f / floatSize * floatSize;
    //             value = Mathf.Clamp(value, 0f, floatSize);
    //             scallingObject.localScale =
    //                 Vector3.MoveTowards(scallingObject.localScale, Vector3.one * value, Time.deltaTime * 5f);
    //         }
    //         else
    //         {
    //             scallingObject.localScale =
    //                 Vector3.MoveTowards(scallingObject.localScale, Vector3.one, Time.deltaTime * 10f);
    //         }
    //     }
    // }

    private void EnableWaterInteractive()
    {
        // if ((bool)waterInteractive)
        // {
        //     waterInteractive.enabled = true;
        // }
    }

    private void DisplcementController()
    {
        if (!Owner.Gears.Rod || !Owner.Gears.Weight)
        {
            return;
        }

        if (waterDisplacement.isInWater)
        {
            float value = floatDisplacement - Owner.Gears.Weight.weight + 1f;
            value = Mathf.Clamp(value, -1f, 2f);
            waterDisplacement.objectDisplacement = value;
            // if ((value > 1.5f && rigidbody.linearVelocity.magnitude < 1f) || ProbeDeep(transform) < currentFloatDeepth)
            // {
            //     collider.center = new Vector3(0f, collider.center.y, 0.07f);
            // }
            // else
            // {
            //     collider.center = new Vector3(0f, collider.center.y, 0f);
            // }
        }
        else
        {
            // collider.center = new Vector3(0f, collider.center.y, 0f);
        }
    }

    private float ProbeDeep(Transform probeObject)
    {
        int mask = LayerMask.GetMask("Terrain");
        float result = 0f;
        if (Physics.Raycast(probeObject.transform.position, -Vector3.up, out var hitInfo, float.PositiveInfinity, mask))
        {
            result = hitInfo.distance;
        }

        return result;
    }

    private void SetDeepth()
    {
        if ((bool)Owner.Gears.Rod && (bool)Owner.Gears.Hook &&
            !(Owner.Data.lineLength > 2f) && !Owner.Gears.Rod.currentFish)
        {
            if (newDeepth != currentFloatDeepth)
            {
                Owner.Gears.Rod.lineHandler.SetSegmentTwoLenght(newDeepth);
                currentFloatDeepth = newDeepth;
            }

            // if (InputManager.isDeepthFloatUp)
            // {
            //     newDeepth += 0.1f;
            //     newDeepth = Mathf.Clamp(newDeepth, 0.15f, 2.5f);
            //     Owner.Data.currentGear.SetBobberLastSetGroundValue(newDeepth);
            //     // GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("SET_FLOAT_DEEPTH_TO") + newDeepth.ToString("F2") + " m", FScriptsHandler.Instance.m_Canvas.transform, deleteLast: true);
            // }
            //
            // if (InputManager.isDeepthFloatDown)
            // {
            //     newDeepth -= 0.1f;
            //     newDeepth = Mathf.Clamp(newDeepth, 0.15f, 2.5f);
            //
            //     Owner.Data.currentGear.SetBobberLastSetGroundValue(newDeepth);
            //     // GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("SET_FLOAT_DEEPTH_TO") + newDeepth.ToString("F2") + " m", FScriptsHandler.Instance.m_Canvas.transform, deleteLast: true);
            // }

            if (Owner.Data.selectorRodSetting == SelectorRodSetting.Leeder &&
                Input.mouseScrollDelta.y != 0f && Time.timeScale != 0f)
            {
                newDeepth += Input.mouseScrollDelta.y * 0.02f;
                newDeepth = Mathf.Clamp(newDeepth, 0.15f, 2.5f);
                Owner.Data.currentGear.SetBobberLastSetGroundValue(newDeepth);
                // GameManager.Instance.ShowMessagePopup(LanguageManager.Instance.GetText("SET_FLOAT_DEEPTH_TO") + newDeepth.ToString("F2") + " m", FScriptsHandler.Instance.m_Canvas.transform, deleteLast: true);
            }
        }
    }

    private void DestroyDelayFxDrop()
    {
        if ((bool)currentWaterDrop)
        {
            Destroy(currentWaterDrop.gameObject);
            destroyDropFx = false;
        }
    }

    private void ShowWaterFX()
    {
        // float num = 0f - transform.position.y;
        // if (num < 0.1f && num > -0.1f)
        // {
        //     if (!currentWaterDrop)
        //     {
        //         currentWaterDrop = Instantiate(FScriptsHandler.Instance.waterFishSplash[2],
        //             transform.position, Quaternion.identity, transform.parent).transform;
        //     }
        //     else
        //     {
        //         currentWaterDrop.position = new Vector3(transform.position.x, 0f, transform.position.z);
        //     }
        // }
        // else if ((bool)currentWaterDrop)
        // {
        //     if (!destroyDropFx)
        //     {
        //         Invoke("DestroyDelayFxDrop", 4f);
        //     }
        //
        //     destroyDropFx = true;
        // }
    }

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

    #region Test

    public void Test(int type)
    {
    }

    #endregion
}