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

public class GenericTestButtonScript : MonoBehaviour
{
    // The sprite to show when the button is toggled on
    public Sprite OnSprite;

    // The sprite to show when the button is toggled off
    public Sprite OffSprite;

    // Is the button state on or off
    public bool IsOn = false;

    // Is this a toggle switch (can be turned off and on at will)
    public bool IsToggle = true;

    // Timer until the button is toggled back on 
    public float ReEngageTime = 0;

    // Time the button has been off (for using with ReEngangeTime)
    private float offTimer = 0;

    // Used when polling for updates
    private bool stateHasChanged = false;

    // The renderer for showing on / off sprites
    SpriteRenderer _spriteRenderer;

    public AudioSource SoundEffect;

    void Start()
    {
        _spriteRenderer = GetComponent<SpriteRenderer>();
        _spriteRenderer.sprite = (IsOn ? OnSprite : OffSprite);
    }
    public void toggleOn()
    {
        changeState(true);
    }

    public void changeState(bool newState)
    {
        if (newState != IsOn)
        {
            IsOn = newState;
            _spriteRenderer.sprite = (IsOn ? OnSprite : OffSprite);
            stateHasChanged = true;
            SoundEffect.Play();
        }
    }
     public bool stateChanged()
    {
        bool hasStateCanged = stateHasChanged;
        stateHasChanged = false;
        return hasStateCanged;
    }
    private void OnMouseDown()
    {
        if (IsToggle || IsOn)
            changeState(!IsOn); 
    }

    private void FixedUpdate()
    {
     if (ReEngageTime != 0 && !IsOn)
        {
            offTimer += Time.deltaTime;

            if (offTimer > ReEngageTime)
            {
                offTimer = 0;
                changeState(!IsOn);
            }

        }
    }
}
