import time
import random
import audiocore
import audiobusio
import board
import os

# Define the file names of the sounds
files = ["unplug.wav", "plug.wav"]

# Initialize audio output
try:
    audio = audiobusio.I2SOut(board.GP0, board.GP1, board.GP2)
except Exception as e:
    print(f"Audio hardware initialization failed: {e}")
    audio = None

# Function to play sound
def play_sound(sound_file):
    if audio is None:
        print("Audio output not available.")
        return

    with open(sound_file, "rb") as wave_file:
        wave = audiocore.WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:  # Wait until the sound finishes playing
            time.sleep(0.1)

# Main loop
while True:
    # Choose a random file from the list
    sound_file = random.choice(files)
    
    # Play the chosen sound file
    print(f"Playing: {sound_file}")
    play_sound(sound_file)
    
    # Wait for a random amount of time (between 1 and 120 seconds)
    interval = random.randint(1, 120)
    print(f"Waiting for {interval} seconds...")
    time.sleep(interval)
