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

public class Atom : MonoBehaviour
{
	[SerializeField]
	private LayerMask objectsLayer;

	[SerializeField]
	private ObjectsManager objectsManager;

	[SerializeField]
	private float rotSpeed = 1;

	[SerializeField]
	private AtomStats atomStats;

	[SerializeField]
	private Score score;

	[SerializeField]
	private AudioSource audioSource;
	[SerializeField]
	private AudioClip[] badHit;
	[SerializeField]
	private AudioClip[] goodHit;

	private Vector3 rotationVector;

	void Start()
	{
		rotationVector = new Vector3(Random.Range(0, rotSpeed), Random.Range(0, rotSpeed), Random.Range(0, rotSpeed));
	}

	void Update()
	{
		//Rotacionando o atomo
		transform.Rotate(rotationVector);

		Collider2D col = Physics2D.OverlapPoint(transform.position, objectsLayer);

		if(col)
		{
			col.gameObject.SetActive(false);

			int type = col.GetComponent<BaseObject>().Type;

			if(col.CompareTag(Tags.gamaTag))
			{
				objectsManager.EmitGama(col.GetComponent<GamaRayObject>().Type);

				atomStats.RemoveHealth(1);
				
				audioSource.PlayOneShot(badHit[type]);
			}
			else if(col.CompareTag(Tags.energyTag))
			{
				score.IncreaseScore(50);

				audioSource.PlayOneShot(goodHit[type]);
			}
		}
	}

	void OnDrawGizmosSelected()
	{
		Gizmos.color = Color.red;

		Gizmos.DrawWireSphere(transform.position, 0.01f);
	}
}
