using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class BulletScript : MonoBehaviour
{
    public DataBullet dataBullet;
    private float damage;
    private float speed;
    private int ammo;
    public GameObject particalClash;
    public float lifetime = 3f;
    private Rigidbody rb;
    private float time = 0f;
    /// <summary>
    /// Sobald life Time vorbei ist Destroy die Bullet.
    /// </summary>
    /// <param name="lifetime">Lifetime der Bullet</param>
    private void die(float lifetime){
        if(time > lifetime){
            GameObject.Destroy(this.gameObject);
        }
    }
    /// <summary>
    /// clashPartical spawned clashPartical am EinschlagPunkt der Kugel.
    /// </summary>
    /// <param name="collision">Die Collision mit der Bullet.</param>
    public void clashParticle(Collision collision){
        ContactPoint contact = collision.GetContact(0);
        Instantiate(particalClash,transform.position,Quaternion.LookRotation(contact.normal));
    }
    /// <summary>
    /// Wenn die Bullet etwas behrührt zerstört sie sich selbst.
    /// </summary>
    /// <param name="collision">Die Collision mit der Bullet</param>
    private void OnCollisionEnter(Collision collision){
        Debug.Log(collision.gameObject.name + " UND HIER IST DER TAG: " + collision.gameObject.tag);
        if(collision.transform.tag == "Enemy"){
            collision.transform.GetComponent<EnemyHealth>().takeDamage(damage);
        }
        clashParticle(collision);
        GameObject.Destroy(this.gameObject);
        
    }
    // Start is called before the first frame update
    void Start()
    {
        damage = dataBullet.damage;
        ammo = dataBullet.ammo;
        speed = dataBullet.speed;

        rb = this.GetComponent<Rigidbody>();

        Physics.IgnoreCollision(GetComponent<Collider>(),GameObject.Find("Player").GetComponent<Collider>());
    }
    

    // Update is called once per frame
    void Update()
    {
        rb.velocity = transform.forward * speed;
        die(lifetime);
        time += Time.deltaTime;
    }
}
