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

public class LevelGoal : MonoBehaviour {

	[SerializeField] private List<GameObject> objectives;

    [SerializeField]
    private ParticleSystem allDoneParticles;

	public AudioClip teleporterActiveSound;
	public AudioClip teleporterActivatedSound;

	// Use this for initialization
	void Awake () {
        if (allDoneParticles)
        {
            allDoneParticles.enableEmission = false;
        }
	}

    bool allDone()
    {
        foreach (GameObject e in objectives)
        {
            if (e == null)
            {
                continue;
            }
            Objective o = e.GetComponent<Objective>();
            if (o == null)
            {
                continue;
            }
            if (!o.objective) return false;
        }

        return true;
	}

	void OnTriggerEnter(Collider other)
	{
        if (other.tag != "Hero")
        {
            return;
        }

		if (!allDone())
        {
            return;
        }

		other.GetComponent<Hero>().LevelComplete();
		if(audio != null)
		{
			audio.clip = teleporterActivatedSound;
			audio.loop = false;
			audio.Play ();
		}
	}
	
	// Update is called once per frame
    void Update()
    {
        if (allDoneParticles)
        {
            allDoneParticles.enableEmission = allDone();
            if (allDone())
            {
                if (audio != null && !audio.isPlaying)
                {
                    audio.clip = teleporterActiveSound;
                    audio.loop = true;
                    audio.Play();
                }
            }
			else
            {
                if (audio != null && audio.isPlaying && audio.clip == teleporterActiveSound)
                {
                    audio.Stop();
                }
            }
        }
	}
}
