import os
import random
import re
import time
import vlc
import config


def getFilesByRegex(folderToScan, regex):
    # We will firstly scan for folders, and then for inside those folders
    # for files. We will return a list of files that match the regex.
    compiledRegex = re.compile(regex)
    audioFiles = []
    for folder in os.listdir(folderToScan):
        if compiledRegex.match(folder):

            # We will now scan the folder for files
            for file in os.listdir(folderToScan + "/" + folder):
                if file.endswith(config.filesExtension):
                    audioFiles.append(folderToScan + "/" + folder + "/" + file)

    return audioFiles


def playSound(soundFile):
    if config.debug:
        print("Playing: " + soundFile)

    # We will play the sound file
    player = vlc.MediaPlayer(soundFile)
    player.audio_set_volume(config.volume)
    player.play()

    # We will wait for the sound to finish
    Ended = 6
    current_state = player.get_state()
    while current_state != Ended:
        current_state = player.get_state()

    if config.debug:
        print("Done playing: " + soundFile)


def getMusicIntroFiles(filesPath, musicName):
    regex = re.compile(config.musicIntro.replace("REPLACEMEWITHMSUICTITLE", musicName.upper()))
    audioFiles = []
    filesPath = filesPath + "/" + config.introFolder

    for file in os.listdir(filesPath):
        if regex.match(file):
            if file.endswith(config.filesExtension):
                audioFiles.append(filesPath + "/" + file)

    return audioFiles


def playSoundWithDelayedSecondSound(firstSound, secondSound):
    delay = random.randint(config.musicMinIntroDelay, config.musicMaxIntroDelay)

    if config.debug:
        print("Playing: " + firstSound + " and " + secondSound + " with a delay of " + str(delay) + " seconds")

    instances = [vlc.Instance(), vlc.Instance()]
    players = [instances[0].media_player_new(), instances[1].media_player_new()]

    # We set the first sound
    players[0].set_media(instances[0].media_new(firstSound))

    # We set the second sound
    players[1].set_media(instances[1].media_new(secondSound))

    # We will play the first sound
    players[0].audio_set_volume(config.volume)
    players[0].play()

    # We wait
    time.sleep(delay)

    # We will play the second sound
    players[1].play()

    # We lower the volume of the first sound
    # players[0].audio_set_volume(50)
    # players[1].audio_set_volume(100)

    # We will wait for the second sound to finish
    Ended = 6
    current_state = players[1].get_state()
    while current_state != Ended:
        current_state = players[1].get_state()

    if config.debug:
        print("Done playing Intro: " + secondSound)

    # We will now raise the volume of the first sound
    players[0].audio_set_volume(config.volume)

    # We will wait for the first sound to finish
    current_state = players[0].get_state()
    while current_state != Ended:
        current_state = players[0].get_state()

    if config.debug:
        print("Done playing Music: " + firstSound)