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

public class LightBlinkScript : MonoBehaviour {
  //Target Object
  public GameObject LightObject;
  private Light LOlight;

  //Script Flexible Variables
  public bool MayBlink = false;
  public float relais = 0.5f;
  public float delay = 1f;
  public bool RandomDelay = true;
  public float RandomDelayMin = 0.1f;
  public float RandomDelayMax = 1f;

  //Script Settings
  private float intensityOfLight;
  private bool inBlink = false;
  public bool LightsON = true;

  //When this object spawns
  void Awake(){
   LOlight = LightObject.GetComponent ();
   intensityOfLight = LOlight.intensity;
  }

  // Update is called once per frame
  void Update () {
   //if the light may blink
   if (MayBlink) {
    Blink ();
   }
   else {
    ONoff ();
   }
  }

  //Blink Component
  void Blink(){
   //One Blink
   if (!inBlink) {
    inBlink = true;
    StartCoroutine (SetOff ());
   }
  }

  //Set the light off
  IEnumerator SetOff(){
   yield return new WaitForSeconds (relais);
   LOlight.intensity = 0;
   StartCoroutine (SetOn ());
  }

  //Set the light on
  IEnumerator SetOn(){
   yield return new WaitForSeconds (relais);
   LOlight.intensity = intensityOfLight;
   StartCoroutine (SetDelay ());
  }

  //Wait time between blinks
  IEnumerator SetDelay(){
   float theDelay = 1f;
   if (!RandomDelay) {
    theDelay = delay;
   }
   if (RandomDelay) {
    theDelay = Random.Range (RandomDelayMin, RandomDelayMax);
   }
   yield return new WaitForSeconds (theDelay);
   inBlink = false;
  }

  //Manual light switch
  void ONoff(){
   if (LightsON) {
    LOlight.intensity = intensityOfLight;
   }
   else {
    LOlight.intensity = 0;
   }
  }

}//CLASS