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

public class CannonBallController : MonoBehaviour
{
    public Collider2D collider;
    public Rigidbody2D body;
    public Vector2 poolOffscreenPosition;
    public float resetAfter;
    public float spawnTime;

    public GameObject explosionPrefab;
    public GameObject splashPrefab;

    void Start()
    {
        spawnTime = Time.time;
    }

    void FixedUpdate()
    {
        if (Time.time - spawnTime > resetAfter)
        {
            ResetCannonBall();
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Enemy"))
        {
            Debug.Log("HIT ENEMY!");
            ResetCannonBall();
        }
    }

    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.CompareTag("Enemy"))
        {
            var newExplosion = Instantiate(explosionPrefab);
            newExplosion.transform.position = transform.position;
            gameObject.SetActive(false);
        }

        else if (collider.gameObject.CompareTag("Wave"))
        {
            var newSplash = Instantiate(splashPrefab);
            newSplash.transform.position = new Vector2(transform.position.x, transform.position.y + 0.67f);
            gameObject.SetActive(false);
        }
    }

    private void ResetCannonBall()
    {
        CannonBallPoolManager.Instance.ResetCannonBall(this);
    }
}
