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

public class MinionManager : MonoBehaviour {

	public GameObject	prefabCursorFocus;
	public float					cursorFocusSpeed = 5.0f;

	List<GameObject>		listMinions = new List<GameObject>();
	GameObject				focusedMinion;
	GameObject				cursorFocus;
	int								index = 0;
	
	void Start(){
		GameObject[] minions= GameObject.FindGameObjectsWithTag("Minion");
		foreach (GameObject minion in minions){
			listMinions.Add(minion);
		}
		focusedMinion = listMinions[0];
		cursorFocus = Instantiate(prefabCursorFocus, focusedMinion.transform.position, Quaternion.identity) as GameObject;
	}
	
	void Update(){
		MoveFocusCursor();
	}
	
	void MoveFocusCursor(){
		if (listMinions.Count > 0)
			cursorFocus.transform.position = Vector3.Lerp(cursorFocus.transform.position, focusedMinion.transform.position, (Time.deltaTime * cursorFocusSpeed) / (Vector3.Distance(cursorFocus.transform.position,  focusedMinion.transform.position) / 1));
	}
	
	public void FocusNextMinion(){
		if (listMinions.Count <= 0){
			GameObject.Find("Referee").GetComponent<GameDirector>().Winner(true);
			return ;
		}
		++index;
		if (index >= listMinions.Count)
			index = 0;
		focusedMinion = listMinions[index];
	}
	
	public void FocusPrevMinion(){
		--index;
		if (index < 0)
			index = listMinions.Count - 1;
		focusedMinion = listMinions[index];
	}
	
	public void NewInputAction(Movements.e_movement newMovement){
		focusedMinion.GetComponent<Movements>().NewInputAction(newMovement);
	}
	
	public void AddNewMinion(GameObject newMinion){
		listMinions.Add(newMinion);
	}
	
	public void RemoveMinion(GameObject theMinion){
		foreach (GameObject minion in listMinions){
			if (minion == theMinion){
				if (minion == focusedMinion)
					FocusNextMinion();
				listMinions.Remove(minion);
				return ;
			}
		}
	}
}
