import os
import random
import config
import defs


class Radio:
    radioPath = ""
    adsPath = ""
    newsPath = ""
    radioName = ""
    ids = []
    monoSolos = []
    generals = []
    musics = []
    ads = []
    news = []

    def __init__(self, radioName):
        self.radioPath = os.getcwd() + "/" + radioName
        self.adsPath = os.getcwd() + "/radio-adverts"
        self.newsPath = os.getcwd() + "/radio-news"
        self.radioName = radioName
        print("Playing: " + self.radioName)

        self.ids = defs.getFilesByRegex(self.radioPath, config.idPattern)
        self.monoSolos = defs.getFilesByRegex(self.radioPath, config.monoSoloPattern)
        self.generals = defs.getFilesByRegex(self.radioPath, config.generalPattern)
        self.musics = defs.getFilesByRegex(self.radioPath, config.musicPatten)
        self.ads = defs.getFilesByRegex(self.adsPath, ".*")
        self.news = defs.getFilesByRegex(self.newsPath, ".*")

    def startRadio(self):
        while True:
            # Check if we will play an ad or news
            if random.random() < config.adsProbability:
                if config.debug:
                    print("Playing ads and news")
                self.playAdAndNews()

            if config.debug:
                print("Playing music")
            self.playMusic()

    def playMusic(self):
        music = random.choice(self.musics)
        musicName = music.split("/")[-1].split(".")[0]

        # We choose random pattern
        pattern = random.choice(config.musicPatterns).split(", ")

        if config.debug:
            print("Pattern: " + str(pattern))

        # We will now play the pattern
        for item in pattern:
            if item == "ID":
                defs.playSound(random.choice(self.ids))
            elif item == "GENERAL":
                defs.playSound(random.choice(self.generals))
            elif item == "MONO_SOLO":
                defs.playSound(random.choice(self.monoSolos))
            elif item == "MUSIC":
                introFiles = defs.getMusicIntroFiles(self.radioPath, musicName)
                if len(introFiles) > 0:
                    intro = random.choice(introFiles)
                    defs.playSoundWithDelayedSecondSound(music, intro)
                else:
                    defs.playSound(music)

    def playAdAndNews(self):
        # We choose random pattern
        pattern = random.choice(config.adsAndNewsPatterns).split(", ")

        if config.debug:
            print("Pattern: " + str(pattern))

        # We will now play the pattern
        for item in pattern:
            if item == "AD":
                defs.playSound(random.choice(self.ads))
            elif item == "NEWS":
                defs.playSound(random.choice(self.news))
            elif item == "MONO_SOLO":
                defs.playSound(random.choice(self.monoSolos))


radiosStations = ["non-stop-pop", "silverlake", "funk"]
chosenRadio = random.choice(radiosStations)

try:
    radio = Radio(chosenRadio)
    radio.startRadio()
except KeyboardInterrupt:
    print("Exiting...")
    exit(0)