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

public class DemonLord : MonoBehaviour {

	public static List<DemonLord> allDemonLords;

	public float initLifespan = 60f;
	public float lifeSpan;

	GameObject healthBar;

	SoulChamber myChamber;
	float speed = 3f;

	float soulsuckCooldown = 1f;	// Will be overwritten by the soul level

	public GameObject soulOrbPrefab;

	Text hp_text;

	public GameObject spawnPrefab;
	public GameObject deathPrefab;


	// Use this for initialization
	void Start () {
		if(allDemonLords == null)
			allDemonLords = new List<DemonLord>();

		allDemonLords.Add(this);

		lifeSpan = initLifespan;
		healthBar = transform.Find ("Healthbar").gameObject;

		hp_text = GameObject.Find ("Demon HPs").GetComponent<Text>();
		ZoomToDemon.current.gameObject.SetActive(true);

		Instantiate(spawnPrefab, this.transform.position, Quaternion.identity);
	}

	void OnDestroy() {

		allDemonLords.Remove (this);
		hp_text.text = "";
		ZoomToDemon.current.gameObject.SetActive(false);
	}

	public float GetPerc() {
		return lifeSpan / initLifespan;
	}
	
	// Update is called once per frame
	void Update () {
		//Debug.Log ("HPs: " + lifeSpan);
		lifeSpan -= Time.deltaTime;

		if(lifeSpan <= 0) {
			Die();
			return;
		}

		healthBar.transform.localScale = new Vector3( 25f * (GetPerc()) , 1, 1 );
		hp_text.text = "Demon HPs: " + string.Format("{0:n0}", lifeSpan);


		if(SoulChamber.allChambers == null) {
			//Debug.LogError("How is the chamber list null?");
			return;
		}
		
		if(SoulChamber.allChambers.Count == 0) {
			//Debug.LogError("How is the chamber list empty?");
			return;
		}
		
		if(myChamber==null || myChamber.numSouls <= 0) {
			FindChamber();
		}

		MoveToSoulChamber();
	}

	void Die() {
		SoundManager.current.PlaySound("Demon Death");
		Instantiate(deathPrefab, this.transform.position, Quaternion.identity);
		Destroy(gameObject);
	}

	void FindChamber() {
		myChamber = null;
		float dist = Mathf.Infinity;

		foreach(SoulChamber sc in SoulChamber.allChambers) {
			if(sc.numSouls <= 0)
				continue;

			float d = Vector2.Distance(sc.transform.position, this.transform.position);

			// Discount d based on circle level
			d *= 1f - sc.circleNumber/20f;

			if(myChamber == null || d < dist) {
				myChamber = sc;
				dist = d;
			}
		}

		if(myChamber != null) {
			soulsuckCooldown = 1f;//((float)myChamber.circleNumber) / 10f;
		}
	}

	void MoveToSoulChamber() {
		if(myChamber == null) {
			return;
		}
		
		Vector2 dir = myChamber.transform.position - this.transform.position;
		
		if(dir.magnitude <= 2f) {
			soulsuckCooldown -= Time.deltaTime;
			if(soulsuckCooldown <= 0) {
				GameObject orb = (GameObject)Instantiate(soulOrbPrefab, myChamber.transform.position, Quaternion.identity);
				orb.GetComponent<SoulOrb>().target = this.transform;
				orb.GetComponent<SoulOrb>().SetColor( SoulChamber.soulColors[ myChamber.circleNumber -1 ] );
				
				soulsuckCooldown = 1f;//((float)myChamber.circleNumber) / 10f;
				myChamber.numSouls--;
			}
		}
		else {
			this.transform.Translate( dir.normalized * speed * Time.deltaTime );
		}
	}

}
