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

public class AtomStats : MonoBehaviour
{
	[SerializeField]
	private int health = 10;

	[SerializeField]
	private UIManager uiManager;
	[SerializeField]
	private GameController gameController;
	[SerializeField]
	private ParticleSystem particleExplode;

	[SerializeField]
	private Texture[] damageTextures;
	[SerializeField]
	private Material nucleoMat;

	[SerializeField]
	private AtomExplode atomExplode;
	[SerializeField]
	private Atom atom;

	[SerializeField]
	private AudioClip explodeClip;

	private bool gameEnded = false;

	void Start()
	{
		uiManager.ChangeAtomLife(health);
		nucleoMat.mainTexture = damageTextures[5];
	}

	public void RemoveHealth(int amount)
	{
		health -= amount;

		nucleoMat.mainTexture = damageTextures[(int)(health / 2)];

		if(health <= 0)
		{
			if(!gameEnded)
			{
				gameEnded = true;

				particleExplode.Play();

				gameController.EndGame();

				atom.enabled = false;

				atomExplode.Explode();

				GetComponent<AudioSource>().PlayOneShot(explodeClip);
			}
				
			health = 0;
		}

		uiManager.ChangeAtomLife(health);
	}
}
