using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class PlayerManager : MonoBehaviour {

    public Text healthText;
    public int playerHealth;
    public int healthDamageIndex;
    public Rigidbody2D bullet;
    public float maxSpeed = 25f;
    public Transform bulletSpawn;

    // Use this for initialization
    void Start () {
        playerHealth = 50;
        healthDamageIndex = 1;
	}

    // Update is called once per frame
    void Update()
    {
        if (healthDamageIndex >= 15)
        {
            playerHealth--;
            healthDamageIndex = 1;
            healthText.text = playerHealth.ToString();
        }

        if (playerHealth <= 0)
        {
            SceneManager.LoadScene(0);
        }

        if (Input.GetButtonDown("Fire1"))
        {
            Fire();
        }
    }

    public void DamagePlayer()
    {
        healthDamageIndex++;
    }

    void OnParticleCollision(GameObject other)
    {
        Debug.Log("Particle Collision");

        if (other.transform.tag == "Player")
        {
            Debug.Log("Player Hit");
        }
        //int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents);

        //Rigidbody2D rb = other.GetComponent<Rigidbody2D>();
        //int i = 0;

        //while (i < numCollisionEvents)
        //{
        //    if (rb)
        //    {
        //        pm.DamagePlayer();
        //    }
        //    i++;
        //}
    }

    void Fire()
    {
        Rigidbody2D bulletInstance = Instantiate(bullet, bulletSpawn.position, Quaternion.Euler(new Vector3(0, 0, 1))) as Rigidbody2D;
        bulletInstance.velocity = transform.forward * maxSpeed;
        Physics2D.IgnoreCollision(bulletInstance.GetComponent<Collider2D>(), GetComponent<Collider2D>());
    }
}
