using UnityEngine;
using System.Collections;

public class Door : MonoBehaviour {

    [SerializeField]
    private bool startsOpen;

    [SerializeField]
    private float openZ = -3.0f;

    [SerializeField]
    private float closedZ = 0.0f;

    [SerializeField]
    private float openCloseSpeed = 10.0f;

    public bool isOpen { get; private set; }

    public Objective[] laserGoals = new Objective[0];
	
	// Update is called once per frame
	void Update () {
	    if (laserGoals.Length > 0)
        {
            bool allGoals = true;
            foreach (Objective laserGoal in laserGoals)
            {
                if (laserGoal == null)
                {
                    continue;
                }

                if (!laserGoal.objective)
                {
                    allGoals = false;
                    break;
                }
            }

            SetOpen(!allGoals);
        }

        float targetZ = isOpen ? openZ : closedZ;

        Vector3 pos = transform.localPosition;

        pos.z = Mathf.MoveTowards(pos.z, targetZ, openCloseSpeed * Time.deltaTime);

        transform.localPosition = pos;
	}

    void SetOpen(bool open)
    {
        if (isOpen == open)
        { 
            return; 
        }

        isOpen = open;
        // TODO - Play a sound.
    }

    void Start()
    {
        isOpen = startsOpen;
    }
}
