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

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

	[SerializeField]
	private float radius;

	[SerializeField]
	private int maxEnergy = 10;

	[SerializeField]
	private UIManager uiManager;

	[SerializeField]
	private Score score;
	[SerializeField]
	private int energy = 0;

	[SerializeField]
	private AudioSource audioSource;

	[SerializeField]
	private AudioClip energyAbsorbClip, gamaAbsorbClip;

	public int Energy
	{
		get{ return energy; }
	}

	void Start()
	{
		uiManager.ChangeEnergyBarValue(energy, maxEnergy);
	}

	void Update()
	{
		Collider2D col = Physics2D.OverlapCircle(transform.position, radius, objectsLayer);

		if(col)
		{
			if(col.CompareTag(Tags.energyTag))
			{
				BaseObject baseObject = col.GetComponent<BaseObject>();

				if(baseObject.Type == 0)
				{
					IncreaseEnergy(1);
				}
				else
				{
					IncreaseEnergy(2);
				}
					
				audioSource.PlayOneShot(energyAbsorbClip);

				score.IncreaseScore(50);
			}
			else if(col.CompareTag(Tags.gamaTag))
			{
				IncreaseEnergy(-1);

				audioSource.PlayOneShot(gamaAbsorbClip);

				score.IncreaseScore(-100);
			}

			col.gameObject.SetActive(false);
		}
	}

	public void IncreaseEnergy(int value)
	{
		energy += value;

		if(energy < 0)
			energy = 0;

		if(energy > maxEnergy)
			energy = maxEnergy;

		uiManager.ChangeEnergyBarValue(energy, maxEnergy);
	}

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

		Gizmos.DrawWireSphere(transform.position, radius);
	}
}
