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

public class FadingPlatform : MonoBehaviour
{

private SpriteRenderer spriteRenderer;
private BoxCollider2D col;
private EdgeCollider2D edge;
public float fadeTime;
public float refractoryTime;
private bool gone;
private AudioSource audioSource; // Reference to the AudioSource component
public AudioClip fadeSound; // The sound clip to be played


    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        col = GetComponent<BoxCollider2D>();
        edge = GetComponent<EdgeCollider2D>();
        gone = false;
        audioSource = GetComponent<AudioSource>();
    }

    void Update()
    {
        if(gone) 
        {
            StartCoroutine(Switch());
        }
    }

    void OnTriggerEnter2D(Collider2D edge) {
        if(!gone){
            StartCoroutine(Switch());
        }
    }

    // Muss IEnumerator sein weil ich ansonsten den Timer nicht implementieren kann
    IEnumerator Switch(){

        if(gone)
        {
            yield return new WaitForSeconds (refractoryTime);
            spriteRenderer.enabled = true;
            col.enabled = true; 
            edge.enabled = true;
            gone = false;
        } else {
            audioSource.PlayOneShot(fadeSound);
            yield return new WaitForSeconds (fadeTime);
            spriteRenderer.enabled = false;
            col.enabled = false; 
            edge.enabled = false;
            gone = true;
            
        }
    }

}
