using UnityEngine;

public class Lampadaire : MonoBehaviour {

    private const float fps = 30f;

    private static Light myLight = null;

    private static bool fadeStarted = false;

    private const float borneDuration = 1.5f;

    private static float intensity = 0f;

    private static bool intensify = true;

	void Start () {
        fadeStarted = false;
        myLight = GetComponent<Light>();
        myLight.intensity = 0;
        intensify = true;
	}
	
	void Update () {
        if (Input.GetKeyDown(KeyCode.J)) Sun.FadeToNight();
        if (Bed.mustSleep && !fadeStarted) {
            fadeStarted = true;
            InvokeRepeating("Fade", 0, 1 / fps);
        }
	}

    void Fade() {
        intensity += (intensify ? 1 : -1) * 8 / (fps * borneDuration);
        if (intensity <= 0 && !intensify) {
            intensity = 0;
            intensify = true;
        } else if (intensity >= 8 && intensify) {
            intensity = 8;
            intensify = false;
        }
        myLight.intensity = intensity;
    }
}