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

public class SafeZoneBehaviour : MonoBehaviour {
	
	public 	bool 				m_HeroInSafeZone;
	public 	bool				m_HeroHasAssignment;
	public 	List<GameObject> 	m_Assignments;
	private int					m_AssignmentNr;
	
	public GameObject			m_Deadline;
	
	private float				m_Timer;
	public float				m_GoalTime;
	
	// Use this for initialization
	void Start () 
	{
		m_HeroInSafeZone = false;
		m_HeroHasAssignment = false;
		m_Assignments = new List<GameObject>();
		GameObject[] assignments = GameObject.FindGameObjectsWithTag("Assignment");
		foreach(GameObject go in assignments)
			m_Assignments.Add(go);
		
		m_Timer = m_GoalTime;
		m_AssignmentNr=0;
	}
	
	// Update is called once per frame
	void Update () 
	{
		m_Timer -= Time.deltaTime;
		if(m_Timer <= 0)
		{
			if(GameObject.FindGameObjectWithTag("Deadline")==null)
				Instantiate(m_Deadline);	
		}
		
		if(m_AssignmentNr == m_Assignments.Count)
		{
			Destroy(GameObject.Find("Door"));
		}

		if(m_HeroInSafeZone && m_HeroHasAssignment)
		{
			m_AssignmentNr++;
			m_HeroHasAssignment = false;
			
			GameObject ui = GameObject.Find("Interface");
			ui.GetComponent<Interface>().DeliverAssignment();
			GameObject.Find("Radio").GetComponent<Radio>().Bonus();
		}
	}
	
	void OnTriggerEnter(Collider coll)
	{
		if(coll.gameObject.GetComponent<Hero>()!=null)
		{
			m_HeroInSafeZone = true;
		}
	}
	
	void OnTriggerExit(Collider coll)
	{
		if(coll.gameObject.GetComponent<Hero>()!=null)
		{
			m_HeroInSafeZone = false;
		}
	}
	
	public void HeroTakesAssignemnt()
	{
		m_HeroHasAssignment = true;
	}
	
	public bool IsHeroDone()
	{
		return	m_AssignmentNr == m_Assignments.Count;
	}
}
