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

//Marvin Schneider
public class PlayerStats : MonoBehaviour
{
    public Player player;
    public Weapon weapon;
    public float health = 0;
    public float speed = 0;
    public float damage = 0;
    public float fireRate = 0;

    private bool isTakingDamage = false;


    void Start()
    {
        health = player.getCurrentHealth();
        speed = player.getCurrentSpeed();
        damage = weapon.getDamage();
        fireRate = weapon.getFireRate();
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Melee"))
        {
            if (!isTakingDamage)
            {
                StartCoroutine(TakeDamageOverTime(other.gameObject.GetComponent<EnemyStats>()));
            }

            if (health <= 0)
            {
                Destroy(this.gameObject);
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D other) {
        if (other.gameObject.CompareTag("Bullet") && !isTakingDamage){
            BulletScript bulletScript = other.gameObject.GetComponent<BulletScript>();
            health -= bulletScript.weapon.getDamage();

            if (health <= 0)
            {
                Destroy(this.gameObject);
            }
        }
    }

    private void OnCollisionExit2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Melee"))
        {
            isTakingDamage = false;
        }
    }

    private IEnumerator TakeDamageOverTime(EnemyStats enemyStats)
    {
        isTakingDamage = true;
        while (isTakingDamage && enemyStats != null)
        {
            health -= enemyStats.damage;
            
            yield return new WaitForSeconds(1f);
        }
    }
}
