using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Water : MonoBehaviour
{
    Rigidbody2D body;
    public float effect = 0.05f;
    public float waterOffset = 0.1f;
    private void FixedUpdate()
    {
        if (body == null) { return; }

        body.velocity = Vector3.Lerp(body.velocity, new Vector2(0, -0.01f), effect);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        body = collision.GetComponent<Rigidbody2D>();

      //  GameObject FX = ObjectPool.Spawn(LevelGenerator.instance.WaterSplashFX, new Vector3(collision.transform.position.x, transform.position.y + waterOffset), ttl: 2500) ;
        GameObject FX = ObjectPool.Spawn(LevelGenerator.instance.WaterSplashFX, collision.transform.position, ttl: 2500);

        FX.GetComponent<ParticleSystem>().Play();
        FX.transform.eulerAngles = new Vector3(-90, 0, 0);

        int splashHardness = 1;

        if (collision.attachedRigidbody.velocity.sqrMagnitude > 40) splashHardness = 2;
        if (collision.attachedRigidbody.velocity.sqrMagnitude > 100) splashHardness = 3;

        Debug.Log(collision.attachedRigidbody.velocity.sqrMagnitude);

        AudioManager.SplashSFX(splashHardness);

        if(body.GetComponent<GolfBall>() != null)
        {
            body.transform.GetChild(0).gameObject.SetActive(true);
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (body.GetComponent<GolfBall>() != null)
        {
            body.transform.GetChild(0).gameObject.SetActive(true);
        }
        body = null;
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawLine(transform.position + new Vector3(0.5f, waterOffset), transform.position + new Vector3(-0.5f, waterOffset));
    }
}
