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

[RequireComponent(typeof(SpriteRenderer))]
public class LEDscript : MonoBehaviour
{

    // Populate this array in the Unity Inspector window
    // to contain the sprites you want to switch between.
    public Sprite OnSprite;
    public Sprite OffSprite;

    public bool IsOn = false;
    public bool IsFlashing = false;
    
    [Range(0, 5)]
    public float FlashingRate = 1;
    private float flashingTicks = 0;

    SpriteRenderer _spriteRenderer;

    // Use this for initialization
    void Start()
    {
        // Cache a reference to this component so we don't
        // need to look it up every time we want to use it.
        _spriteRenderer = GetComponent<SpriteRenderer>();
    }

    public void changeState(bool newState)
    {
        if (newState != IsOn)
        {
            IsOn = newState;
            _spriteRenderer.sprite = (IsOn ? OnSprite : OffSprite);
        }
    }

    private void Update()
    {
        flashingTicks += Time.deltaTime;
        
        if (IsFlashing && flashingTicks > FlashingRate)
        {
            changeState(!IsOn);
            flashingTicks = 0;
        }
    }

}
