using UnityEngine;
using System.Collections;

public class Soulzooka : MonoBehaviour {

	static float globalCooldown = 0;

	public float radius = 5f;

	void Update() {
		globalCooldown -= Time.deltaTime;
	}

	public int CurrentSoulLevel() {
		Collider2D[] cols = Physics2D.OverlapCircleAll(this.transform.position, radius);
		
		SoulChamber theChamber = null;
		
		foreach(Collider2D col in cols) {
			SoulChamber sc = col.GetComponent<SoulChamber>();
			if(sc == null)
				continue;
			
			if(sc.numSouls > 0 && (theChamber == null || sc.circleNumber > theChamber.circleNumber)) {
				theChamber = sc;
			}
			
		}
		
		if(theChamber == null)
			return 0;
		
		return theChamber.circleNumber;
	}

	public int SoulToBullet(GameObject target) {
		// Attempts to consume a soul, returns the soul value

		if(globalCooldown > 0)
			return 0;

		globalCooldown = 5f;

		//float lifespan = target.GetComponent<DemonLord>().lifeSpan;

		if(target.GetComponent<DemonLord>().GetPerc() <= 0.1f) {
			// Target too weak -- don't fire!
			return 0;
		}

		// Find the highest level soul.

		Collider2D[] cols = Physics2D.OverlapCircleAll(this.transform.position, radius);

		SoulChamber theChamber = null;

		foreach(Collider2D col in cols) {
			SoulChamber sc = col.GetComponent<SoulChamber>();
			if(sc == null)
				continue;

			if(sc.numSouls > 0 && (theChamber == null || sc.circleNumber > theChamber.circleNumber)) {
				theChamber = sc;
			}

		}

		if(theChamber == null)
			return 0;

		theChamber.numSouls--;

		return theChamber.circleNumber;
	}
}
