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

public class StressLights : MonoBehaviour {

    public static StressLights instance;

    public float totalPossibleStress;
    [ReadOnly] public float currentStress;

    [ReadOnly] public StressLight [] childLights;

    private void Awake ()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(this);
        }
    }

    private void Start ()
    {
        childLights = GetComponentsInChildren<StressLight>();
    }

    public void addStress (float stressChange)
    {
        setStress(currentStress + stressChange);
    }

    public void setStress (float stress)
    {
        currentStress = stress;
        if (currentStress < 0)
            currentStress = 0;
        for (int i = 0; i < childLights.Length; i++)
        {
            childLights [i].SetOn(( (currentStress / totalPossibleStress) > ((float)i / (float)childLights.Length) ));
        }

        if (currentStress > totalPossibleStress)
        {
            IncomingConnectionController.instance.LostGame();
        }
    }

    public void Clear ()
    {
        setStress(0);
    }
}
