using UnityEngine;
using System.Collections;

public class OpponentDetector : MonoBehaviour {
    public float speed = 1f;
    public bool hideWhenCollapsed = false;
    private GameManager gameManager;
    private bool expand = false;
    private float maxZ;
    public float maxExpandTime = 0;
    // private Light[] lights;

    void Start() {
        // lights = GetComponentsInChildren<Light>();
        gameManager = FindObjectOfType<GameManager>();
        maxZ = transform.localScale.z;
    }

    void Update() {
        float x = transform.position.x;
        expand = true;
        foreach (Opponent opponent in gameManager.opponents) {
            // take care of killed opponents
            if (opponent == null) {
                break;
            }
            if (opponent.transform.position.x >= x - opponent.range && opponent.transform.position.x <= x + opponent.range) {
                expand = false;
                break;
            }

        }

        Vector3 oldScale = transform.localScale;
        transform.localScale = Vector3.Lerp(transform.localScale, new Vector3(transform.localScale.x, transform.localScale.y, expand ? maxZ : 0.02f), Time.deltaTime * speed);
        if (hideWhenCollapsed && (expand || Vector3.Distance(oldScale, transform.localScale) < 0.005f)) {
            foreach (Transform child in transform) {
                child.gameObject.SetActive(expand);
            }
        }
        // setLightsState(expand);
    }

    // was a nice idea but in playtesting switching on and off lights distracted too much
    //private void setLightsState(bool state) {
    //    foreach (Light light in lights) {
    //        light.enabled = state;
    //    }
    //}
}
